Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r2118:1283fea71028
Date: 2015-05-28 10:54 +0200
http://bitbucket.org/cffi/cffi/changeset/1283fea71028/

Log:    Issue 198: corrupted constant of type 'struct'

        Thanks Lisandro!

diff --git a/c/lib_obj.c b/c/lib_obj.c
--- a/c/lib_obj.c
+++ b/c/lib_obj.c
@@ -260,7 +260,17 @@
 
         assert(g->address);
         assert(ct->ct_size > 0);
-        data = alloca(ct->ct_size);
+        /* xxx the few bytes of memory we allocate here leak, but it's
+           a minor concern because it should only occur for
+           OP_CONSTANT.  There is one per real non-integer C constant
+           in a CFFI C extension module.  CPython never unloads its C
+           extension modules anyway.  Note that we used to do alloca(),
+           but see issue #198. */
+        data = PyMem_Malloc(ct->ct_size);
+        if (data == NULL) {
+            PyErr_NoMemory();
+            return NULL;
+        }
         ((void(*)(char*))g->address)(data);
         x = convert_to_object(data, ct);
         Py_DECREF(ct);
diff --git a/testing/cffi1/test_recompiler.py b/testing/cffi1/test_recompiler.py
--- a/testing/cffi1/test_recompiler.py
+++ b/testing/cffi1/test_recompiler.py
@@ -823,3 +823,21 @@
     assert addr(0xABC05) == 47
     assert isinstance(addr, ffi.CData)
     assert ffi.typeof(addr) == ffi.typeof("long(*)(long)")
+
+def test_issue198():
+    ffi = FFI()
+    ffi.cdef("""
+        typedef struct{...;} opaque_t;
+        const opaque_t CONSTANT;
+        int toint(opaque_t);
+    """)
+    lib = verify(ffi, 'test_issue198', """
+        typedef int opaque_t;
+        #define CONSTANT ((opaque_t)42)
+        static int toint(opaque_t o) { return o; }
+    """)
+    def random_stuff():
+        pass
+    assert lib.toint(lib.CONSTANT) == 42
+    random_stuff()
+    assert lib.toint(lib.CONSTANT) == 42
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to