Commit 663e665843

LemonBoy <thatlemon@gmail.com>
2019-09-18 08:54:59
Prevent crash with invalid extern type
Fixes #3240
1 parent db988f4
Changed files (2)
src/analyze.cpp
@@ -1606,6 +1606,10 @@ Error type_allowed_in_extern(CodeGen *g, ZigType *type_entry, bool *result) {
                 *result = false;
                 return ErrorNone;
             }
+            if (!type_is_nonnull_ptr(child_type)) {
+                *result = false;
+                return ErrorNone;
+            }
             return type_allowed_in_extern(g, child_type, result);
         }
         case ZigTypeIdEnum:
test/compile_errors.zig
@@ -15,6 +15,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
         "tmp.zig:5:9: error: duplicate switch value",
     );
 
+    cases.add(
+        "invalid optional type in extern struct",
+        \\const stroo = extern struct {
+        \\    moo: ?[*c]u8,
+        \\};
+        \\export fn testf(fluff: *stroo) void {}
+    ,
+        "tmp.zig:2:5: error: extern structs cannot contain fields of type '?[*c]u8'",
+    );
+
     cases.add(
         "attempt to negate a non-integer, non-float or non-vector type",
         \\fn foo() anyerror!u32 {