Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 839d727

Browse files
committedJan 20, 2020
Fix unsafe cast in translate_c
Fixes #4250
1 parent c522699 commit 839d727

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed
 

‎src-self-hosted/clang.zig

+1
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ pub extern fn ZigClangQualType_isRestrictQualified(self: struct_ZigClangQualType
809809
pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) ZigClangTypeClass;
810810
pub extern fn ZigClangType_getPointeeType(self: ?*const struct_ZigClangType) struct_ZigClangQualType;
811811
pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool;
812+
pub extern fn ZigClangType_isConstantArrayType(self: ?*const struct_ZigClangType) bool;
812813
pub extern fn ZigClangType_isRecordType(self: ?*const struct_ZigClangType) bool;
813814
pub extern fn ZigClangType_isArrayType(self: ?*const struct_ZigClangType) bool;
814815
pub extern fn ZigClangType_isBooleanType(self: ?*const struct_ZigClangType) bool;

‎src-self-hosted/translate_c.zig

+2-1
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,8 @@ fn transInitListExprArray(
19821982
const arr_type = ZigClangType_getAsArrayTypeUnsafe(ty);
19831983
const child_qt = ZigClangArrayType_getElementType(arr_type);
19841984
const init_count = ZigClangInitListExpr_getNumInits(expr);
1985-
const const_arr_ty = @ptrCast(*const ZigClangConstantArrayType, ty);
1985+
assert(ZigClangType_isConstantArrayType(arr_type));
1986+
const const_arr_ty = @ptrCast(*const ZigClangConstantArrayType, arr_type);
19861987
const size_ap_int = ZigClangConstantArrayType_getSize(const_arr_ty);
19871988
const all_count = ZigClangAPInt_getLimitedValue(size_ap_int, math.maxInt(usize));
19881989
const leftover_count = all_count - init_count;

‎src/zig_clang.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,11 @@ bool ZigClangType_isRecordType(const ZigClangType *self) {
18771877
return casted->isRecordType();
18781878
}
18791879

1880+
bool ZigClangType_isConstantArrayType(const ZigClangType *self) {
1881+
auto casted = reinterpret_cast<const clang::Type *>(self);
1882+
return casted->isConstantArrayType();
1883+
}
1884+
18801885
const char *ZigClangType_getTypeClassName(const ZigClangType *self) {
18811886
auto casted = reinterpret_cast<const clang::Type *>(self);
18821887
return casted->getTypeClassName();

‎src/zig_clang.h

+1
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ ZIG_EXTERN_C bool ZigClangType_isBooleanType(const struct ZigClangType *self);
945945
ZIG_EXTERN_C bool ZigClangType_isVoidType(const struct ZigClangType *self);
946946
ZIG_EXTERN_C bool ZigClangType_isArrayType(const struct ZigClangType *self);
947947
ZIG_EXTERN_C bool ZigClangType_isRecordType(const struct ZigClangType *self);
948+
ZIG_EXTERN_C bool ZigClangType_isConstantArrayType(const ZigClangType *self);
948949
ZIG_EXTERN_C const char *ZigClangType_getTypeClassName(const struct ZigClangType *self);
949950
ZIG_EXTERN_C const struct ZigClangArrayType *ZigClangType_getAsArrayTypeUnsafe(const struct ZigClangType *self);
950951
ZIG_EXTERN_C const ZigClangRecordType *ZigClangType_getAsRecordType(const ZigClangType *self);

‎test/translate_c.zig

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@ const tests = @import("tests.zig");
22
const builtin = @import("builtin");
33

44
pub fn addCases(cases: *tests.TranslateCContext) void {
5+
cases.add("array initializer w/ typedef",
6+
\\typedef unsigned char uuid_t[16];
7+
\\static const uuid_t UUID_NULL __attribute__ ((unused)) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
8+
, &[_][]const u8{
9+
\\pub const uuid_t = [16]u8;
10+
\\pub const UUID_NULL: uuid_t = .{
11+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
12+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
13+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
14+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
15+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
16+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
17+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
18+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
19+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
20+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
21+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
22+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
23+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
24+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
25+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
26+
\\ @bitCast(u8, @truncate(i8, @as(c_int, 0))),
27+
\\};
28+
});
29+
530
cases.add("empty declaration",
631
\\;
732
, &[_][]const u8{""});

0 commit comments

Comments
 (0)
Please sign in to comment.