Index: Include/longintrepr.h =================================================================== --- Include/longintrepr.h (revision 69616) +++ Include/longintrepr.h (working copy) @@ -19,14 +19,13 @@ long_pow() requires that SHIFT be divisible by 5. */ typedef unsigned short digit; -typedef unsigned int wdigit; /* digit widened to parameter size */ #define BASE_TWODIGITS_TYPE long typedef unsigned BASE_TWODIGITS_TYPE twodigits; typedef BASE_TWODIGITS_TYPE stwodigits; /* signed variant of twodigits */ #define PyLong_SHIFT 15 #define PyLong_BASE ((digit)1 << PyLong_SHIFT) -#define PyLong_MASK ((int)(PyLong_BASE - 1)) +#define PyLong_MASK ((digit)(PyLong_BASE - 1)) /* b/w compatibility with Python 2.5 */ #define SHIFT PyLong_SHIFT Index: Objects/longobject.c =================================================================== --- Objects/longobject.c (revision 69616) +++ Objects/longobject.c (working copy) @@ -8,6 +8,7 @@ #include "longintrepr.h" #include +#include /* For long multiplication, use the O(N**2) school algorithm unless * both operands contain more than KARATSUBA_CUTOFF digits (this @@ -30,12 +31,6 @@ #define MAX(x, y) ((x) < (y) ? (y) : (x)) #define MIN(x, y) ((x) > (y) ? (y) : (x)) -/* Forward */ -static PyLongObject *long_normalize(PyLongObject *); -static PyLongObject *mul1(PyLongObject *, wdigit); -static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit); -static PyLongObject *divrem1(PyLongObject *, digit, digit *); - #define SIGCHECK(PyTryBlock) \ if (--_Py_Ticker < 0) { \ _Py_Ticker = _Py_CheckInterval; \ @@ -62,16 +57,18 @@ /* Allocate a new long int object with size digits. Return NULL and set exception if we run out of memory. */ +#define MAX_LONG_DIGITS \ + ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) + PyLongObject * _PyLong_New(Py_ssize_t size) { - if (size > PY_SSIZE_T_MAX) { - PyErr_NoMemory(); + if (size > MAX_LONG_DIGITS) { + PyErr_SetString(PyExc_OverflowError, + "too many digits in integer"); return NULL; } /* coverity[ampersand_in_size] */ - /* XXX(nnorwitz): This can overflow -- - PyObject_NEW_VAR / _PyObject_VAR_SIZE need to detect overflow */ return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); } @@ -197,8 +194,8 @@ return NULL; frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); for (i = ndig; --i >= 0; ) { - long bits = (long)frac; - v->ob_digit[i] = (digit) bits; + digit bits = (digit)frac; + v->ob_digit[i] = bits; frac = frac - (double)bits; frac = ldexp(frac, PyLong_SHIFT); } @@ -438,9 +435,9 @@ int incr; /* direction to move pstartbyte */ const unsigned char* pendbyte; /* MSB of bytes */ size_t numsignificantbytes; /* number of bytes that matter */ - size_t ndigits; /* number of Python long digits */ + Py_ssize_t ndigits; /* number of Python long digits */ PyLongObject* v; /* result */ - int idigit = 0; /* next free index in v->ob_digit */ + Py_ssize_t idigit = 0; /* next free index in v->ob_digit */ if (n == 0) return PyLong_FromLong(0L); @@ -485,10 +482,14 @@ /* How many Python long digits do we need? We have 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT bits, so it's the ceiling of the quotient. */ + /* catch overflow before it happens */ + if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { + PyErr_SetString(PyExc_OverflowError, + "byte array too long to convert to int"); + return NULL; + } ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; - if (ndigits > (size_t)INT_MAX) - return PyErr_NoMemory(); - v = _PyLong_New((int)ndigits); + v = _PyLong_New(ndigits); if (v == NULL) return NULL; @@ -517,7 +518,7 @@ accumbits += 8; if (accumbits >= PyLong_SHIFT) { /* There's enough to fill a Python digit. */ - assert(idigit < (int)ndigits); + assert(idigit < ndigits); v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); ++idigit; accum >>= PyLong_SHIFT; @@ -527,7 +528,7 @@ } assert(accumbits < PyLong_SHIFT); if (accumbits) { - assert(idigit < (int)ndigits); + assert(idigit < ndigits); v->ob_digit[idigit] = (digit)accum; ++idigit; } @@ -1124,19 +1125,11 @@ /* Multiply by a single digit, ignoring the sign. */ static PyLongObject * -mul1(PyLongObject *a, wdigit n) +mul1(PyLongObject *a, digit n) { - return muladd1(a, n, (digit)0); -} - -/* Multiply by a single digit and add a single digit, ignoring the sign. */ - -static PyLongObject * -muladd1(PyLongObject *a, wdigit n, wdigit extra) -{ Py_ssize_t size_a = ABS(Py_SIZE(a)); PyLongObject *z = _PyLong_New(size_a+1); - twodigits carry = extra; + twodigits carry = 0; Py_ssize_t i; if (z == NULL) @@ -1757,8 +1750,6 @@ static PyLongObject *x_divrem (PyLongObject *, PyLongObject *, PyLongObject **); static PyObject *long_long(PyObject *v); -static int long_divrem(PyLongObject *, PyLongObject *, - PyLongObject **, PyLongObject **); /* Long division with remainder, top-level routine */ @@ -1976,14 +1967,12 @@ sign = -1; i = -(i); } -#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT) /* The following loop produces a C unsigned long x such that x is congruent to the absolute value of v modulo ULONG_MAX. The resulting x is nonzero if and only if v is. */ while (--i >= 0) { /* Force a native long #-bits (32 or 64) circular shift */ - x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | - ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK); + x = (x >> (8*SIZEOF_LONG-PyLong_SHIFT)) | (x << PyLong_SHIFT); x += v->ob_digit[i]; /* If the addition above overflowed we compensate by incrementing. This preserves the value modulo @@ -1991,11 +1980,10 @@ if (x < v->ob_digit[i]) x++; } -#undef LONG_BIT_PyLong_SHIFT x = x * sign; - if (x == -1) - x = -2; - return x; + if (x == (unsigned long)-1) + x = (unsigned long)-2; + return (long)x; } @@ -3337,7 +3325,7 @@ /* Since PyLong_FromString doesn't have a length parameter, * check here for possible NULs in the string. */ char *string = PyString_AS_STRING(x); - if (strlen(string) != PyString_Size(x)) { + if (strlen(string) != (size_t)PyString_Size(x)) { /* create a repr() of the input string, * just like PyLong_FromString does. */ PyObject *srepr; @@ -3443,9 +3431,7 @@ { Py_ssize_t res; - res = v->ob_type->tp_basicsize; - if (v->ob_size != 0) - res += abs(v->ob_size) * sizeof(digit); + res = v->ob_type->tp_basicsize + ABS(Py_SIZE(v))*sizeof(digit); return PyInt_FromSsize_t(res); } @@ -3619,7 +3605,7 @@ PyObject_HEAD_INIT(&PyType_Type) 0, /* ob_size */ "long", /* tp_name */ - sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */ + offsetof(PyLongObject, ob_digit), /* tp_basicsize */ sizeof(digit), /* tp_itemsize */ long_dealloc, /* tp_dealloc */ 0, /* tp_print */ Index: Lib/test/test_sys.py =================================================================== --- Lib/test/test_sys.py (revision 69616) +++ Lib/test/test_sys.py (working copy) @@ -593,12 +593,12 @@ # listreverseiterator (list) check(reversed([]), size(h + 'lP')) # long - check(0L, size(vh + 'H') - self.H) - check(1L, size(vh + 'H')) - check(-1L, size(vh + 'H')) - check(32768L, size(vh + 'H') + self.H) - check(32768L*32768L-1, size(vh + 'H') + self.H) - check(32768L*32768L, size(vh + 'H') + 2*self.H) + check(0L, size(vh)) + check(1L, size(vh) + self.H) + check(-1L, size(vh) + self.H) + check(32768L, size(vh) + 2*self.H) + check(32768L*32768L-1, size(vh) + 2*self.H) + check(32768L*32768L, size(vh) + 3*self.H) # module check(unittest, size(h + 'P')) # None