add safety guard for ill defined user types
authorStefan Behnel <stefan_ml@behnel.de>
Sat, 9 Mar 2013 16:28:19 +0000 (17:28 +0100)
committerStefan Behnel <stefan_ml@behnel.de>
Sat, 9 Mar 2013 16:28:19 +0000 (17:28 +0100)
Cython/Compiler/PyrexTypes.py

index 557a77a..4cf07b5 100755 (executable)
@@ -1410,18 +1410,22 @@ static CYTHON_INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x
                 return (%(type)s)-1;
             }
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
-            switch (Py_SIZE(x)) {
-                case  0: return 0;
-                case  1: return (%(type)s) ((PyLongObject*)x)->ob_digit[0];
+            if (sizeof(digit) <= sizeof(%(type)s)) {
+                switch (Py_SIZE(x)) {
+                    case  0: return 0;
+                    case  1: return (%(type)s) ((PyLongObject*)x)->ob_digit[0];
+                }
             }
 #endif
             return (%(type)s)PyLong_AsUnsigned%(TypeName)s(x);
         } else {
 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
-            switch (Py_SIZE(x)) {
-                case  0: return 0;
-                case  1: return +(%(type)s) ((PyLongObject*)x)->ob_digit[0];
-                case -1: return -(%(type)s) ((PyLongObject*)x)->ob_digit[0];
+            if (sizeof(digit) <= sizeof(%(type)s)) {
+                switch (Py_SIZE(x)) {
+                    case  0: return 0;
+                    case  1: return +(%(type)s) ((PyLongObject*)x)->ob_digit[0];
+                    case -1: return -(%(type)s) ((PyLongObject*)x)->ob_digit[0];
+                }
             }
 #endif
             return (%(type)s)PyLong_As%(TypeName)s(x);