Commits

Armin Rigo committed 2f3f5a2

Cannot pass a struct with an array of length 0. We can't correctly
build the ffi struct in this case.

  • Participants
  • Parent commits b22bc09

Comments (0)

Files changed (2)

File c/_cffi_backend.c

                 flat *= ct->ct_length;
                 ct = ct->ct_itemdescr;
             }
-            assert(flat >= 0);
+            if (flat <= 0) {
+                PyErr_SetString(PyExc_NotImplementedError,
+                                "cannot pass as argument or return value "
+                                "a struct with a zero-length array");
+                return NULL;
+            }
             nflat += flat;
             cf = cf->cf_next;
         }
     for i in range(10):
         assert res.a[i] == p1.a[i] - p2.a[i]
 
+def test_cannot_pass_struct_with_array_of_length_0():
+    BInt = new_primitive_type("int")
+    BArray0 = new_array_type(new_pointer_type(BInt), 0)
+    BStruct = new_struct_type("foo")
+    complete_struct_or_union(BStruct, [('a', BArray0)])
+    py.test.raises(NotImplementedError, new_function_type,
+                   (BStruct,), BInt, False)
+    py.test.raises(NotImplementedError, new_function_type,
+                   (BInt,), BStruct, False)
+
 def test_call_function_9():
     BInt = new_primitive_type("int")
     BFunc9 = new_function_type((BInt,), BInt, True)    # vararg