Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r210:84540aee69f5
Date: 2014-12-19 17:04 +0100
http://bitbucket.org/cffi/creflect/changeset/84540aee69f5/

Log:    fix

diff --git a/zeffir/builder.c b/zeffir/builder.c
--- a/zeffir/builder.c
+++ b/zeffir/builder.c
@@ -319,7 +319,6 @@
     CTypeDescrObject *ct;
     PyObject *name_obj;
     char extra_text[32];
-    Py_ssize_t length = (Py_ssize_t)len;
     Py_ssize_t arraysize;
 
     if (ctitem->ct_size < 0) {
@@ -328,10 +327,10 @@
         return NULL;
     }
 
-    if (length < 0)
+    if (len == (size_t)-1)
         sprintf(extra_text, "[]");
     else
-        sprintf(extra_text, "[%zd]", length);
+        sprintf(extra_text, "[%zu]", len);
 
     name_obj = combine_type_name(ctitem, extra_text);
     if (name_obj == NULL)
@@ -343,12 +342,13 @@
             goto done;
     }
 
-    if (length < 0) {
+    if (len == (size_t)-1) {
         arraysize = -1;
     }
     else {
-        arraysize = length * ctitem->ct_size;
-        if (length > 0 && (arraysize / length) != ctitem->ct_size) {
+        arraysize = ((Py_ssize_t)len) * ctitem->ct_size;
+        if ((Py_ssize_t)len < 0 ||
+                (len > 0 && (arraysize / (Py_ssize_t)len) != ctitem->ct_size)) 
{
             PyErr_SetString(PyExc_OverflowError,
                             "array size would overflow a Py_ssize_t");
             goto done;
@@ -366,20 +366,20 @@
         goto done;
 
     ct->ct_size = arraysize;
-    ct->ct_length = length;
+    ct->ct_length = (Py_ssize_t)len;
     ct->ct_flags = CT_ARRAY;
     ct->ct_itemdescr = ctitem;        Py_INCREF(ctitem);
     ct->ct_stuff = (PyObject *)ctptr; Py_INCREF(ctptr);
 
     if (cb != NULL) {
-        if (length < 0 && ctptr->ct_stuff == NULL) {
+        if (len == (size_t)-1 && ctptr->ct_stuff == NULL) {
             Py_INCREF(ct);
             ctptr->ct_stuff = (PyObject *)ct;
         }
         put_cached_type(get_types_dict(cb), name_obj, ct);
     }
     else {
-        assert(length < 0);
+        assert(len == (size_t)-1);
         assert(ctptr->ct_stuff == NULL);
         ctptr->ct_stuff = (PyObject *)ct;
     }
diff --git a/zeffir/test/test_c.py b/zeffir/test/test_c.py
--- a/zeffir/test/test_c.py
+++ b/zeffir/test/test_c.py
@@ -414,5 +414,8 @@
     p2 = ffi.typeof(" int [ 25 ] [ 42 ] ")
     assert repr(p2) == "<ctype 'int[25][42]'>"
     #
+    e = py.test.raises(ffi.error, ffi.typeof, "int[%d]" % (sys.maxsize ** 3))
+    assert str(e.value) == "at pos 4: number too large"
     py.test.raises(OverflowError, ffi.typeof, "int[%d]" % (sys.maxsize + 1))
+    py.test.raises(OverflowError, ffi.typeof, "int[%d]" % sys.maxsize)
     py.test.raises(OverflowError, ffi.typeof, "int[%d]" % (sys.maxsize // 3))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to