Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r1750:2a81b5782307
Date: 2015-04-18 12:28 +0200
http://bitbucket.org/cffi/cffi/changeset/2a81b5782307/
Log: Check the 'field_size' of _cffi_field_s
diff --git a/new/realize_c_type.c b/new/realize_c_type.c
--- a/new/realize_c_type.c
+++ b/new/realize_c_type.c
@@ -451,6 +451,15 @@
return -1;
}
+ if (ctf->ct_size != fld->field_size) {
+ PyErr_Format(FFIError,
+ "%s field '%s' was declared in the cdef to be"
+ " %zd bytes, but is actually %zd bytes",
+ ct->ct_name, fld->name,
+ ctf->ct_size, fld->field_size);
+ return -1;
+ }
+
f = Py_BuildValue("(sOin)", fld->name, ctf,
(int)-1, (Py_ssize_t)fld->field_offset);
if (f == NULL) {
diff --git a/new/test_recompiler.py b/new/test_recompiler.py
--- a/new/test_recompiler.py
+++ b/new/test_recompiler.py
@@ -242,3 +242,13 @@
py.test.raises(IndexError, "lib.aa[41]")
py.test.raises(IndexError, "lib.bb[12]")
+def test_misdeclared_field_1():
+ ffi = FFI()
+ ffi.cdef("struct foo_s { int a[5]; };")
+ verify(ffi, 'test_misdeclared_field_1',
+ "struct foo_s { int a[6]; };")
+ assert ffi.sizeof("struct foo_s") == 24 # found by the actual C code
+ p = ffi.new("struct foo_s *")
+ e = py.test.raises(ffi.error, "p.a") # lazily build the fields and boom
+ assert str(e.value) == ("struct foo_s field 'a' was declared in the "
+ "cdef to be 20 bytes, but is actually 24 bytes")
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit