authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-12 00:51:06-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-12 00:51:06-05:00
log285e2f62ba0648d6d8e7ff64d1ee7d2900481e2f
treeca99c7ae5e4e29bb12a750b92a8d292b3e09a556
parent0abe6d668eb52aefa59c75a8d8f782f2372f8600
signaturelock-open Commit is signed but in an unrecognized format.

disallow C pointers to non-C-ABI-compatible element types

See #1059

4 files changed, 16 insertions(+), 2 deletions(-)

src/analyze.cpp+1-1
......@@ -1469,7 +1469,7 @@ static bool type_allowed_in_packed_struct(ZigType *type_entry) {
14691469 zig_unreachable();
14701470}
14711471
1472static bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry) {
1472bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry) {
14731473 switch (type_entry->id) {
14741474 case ZigTypeIdInvalid:
14751475 zig_unreachable();
src/analyze.hpp+1-1
......@@ -44,7 +44,7 @@ void find_libc_include_path(CodeGen *g);
4444void find_libc_lib_path(CodeGen *g);
4545
4646bool type_has_bits(ZigType *type_entry);
47
47bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry);
4848
4949ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *source_code);
5050
src/ir.cpp+4
......@@ -21145,6 +21145,10 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
2114521145 } else if (child_type->id == ZigTypeIdOpaque && instruction->ptr_len == PtrLenUnknown) {
2114621146 ir_add_error(ira, &instruction->base, buf_sprintf("unknown-length pointer to opaque"));
2114721147 return ira->codegen->invalid_instruction;
21148 } else if (instruction->ptr_len == PtrLenC && !type_allowed_in_extern(ira->codegen, child_type)) {
21149 ir_add_error(ira, &instruction->base,
21150 buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'", buf_ptr(&child_type->name)));
21151 return ira->codegen->invalid_instruction;
2114821152 }
2114921153
2115021154 uint32_t align_bytes;
test/compile_errors.zig+10
......@@ -1,6 +1,16 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.addTest(
5 "C pointer pointing to non C ABI compatible type",
6 \\const Foo = struct {};
7 \\export fn entry() [*c]Foo {
8 \\ return undefined;
9 \\}
10 ,
11 ".tmp_source.zig:2:19: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
12 );
13
414 cases.addTest(
515 "@truncate undefined value",
616 \\export fn entry() void {