Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r1397:9ba268ca7739
Date: 2013-11-09 09:27 +0100
http://bitbucket.org/cffi/cffi/changeset/9ba268ca7739/

Log:    Fix a segfault

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -2112,23 +2112,21 @@
         CDataObject *cdv = (CDataObject *)v;
         CDataObject *cdw = (CDataObject *)w;
         CTypeDescrObject *ct = cdw->c_type;
-        Py_ssize_t diff;
-        Py_ssize_t itemsize;
+        Py_ssize_t diff, itemsize;
 
         if (ct->ct_flags & CT_ARRAY)     /* ptr_to_T - array_of_T: ok */
             ct = (CTypeDescrObject *)ct->ct_stuff;
 
-        itemsize = ct->ct_itemdescr->ct_size;
-        if (ct->ct_flags & CT_IS_VOID_PTR)
-            itemsize = 1;
-
         if (ct != cdv->c_type || !(ct->ct_flags & CT_POINTER) ||
-                (itemsize <= 0)) {
+                (ct->ct_itemdescr->ct_size <= 0 &&
+                 !(ct->ct_flags & CT_IS_VOID_PTR))) {
             PyErr_Format(PyExc_TypeError,
                          "cannot subtract cdata '%s' and cdata '%s'",
                          cdv->c_type->ct_name, ct->ct_name);
             return NULL;
         }
+        itemsize = ct->ct_itemdescr->ct_size;
+        if (itemsize <= 0) itemsize = 1;
         diff = (cdv->c_data - cdw->c_data) / itemsize;
 #if PY_MAJOR_VERSION < 3
         return PyInt_FromSsize_t(diff);
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -3117,6 +3117,8 @@
     q = cast(new_pointer_type(new_primitive_type("char")), 100000)
     py.test.raises(TypeError, "p - q")
     py.test.raises(TypeError, "q - p")
+    py.test.raises(TypeError, "p + cast(new_primitive_type('int'), 42)")
+    py.test.raises(TypeError, "p - cast(new_primitive_type('int'), 42)")
 
 
 def test_version():
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to