diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2009-01-26 21:36:30 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2009-01-26 21:36:30 (GMT) |
commit | 6ffa4a2a7d6c1e1149783d68bba5c05f835c2d61 (patch) | |
tree | 7843a290e481aa58fe9a597aebc5676e0755c5e6 /Objects | |
parent | 0c2430beda789335d70a4db34c02b32ed65e23bb (diff) | |
download | cpython-6ffa4a2a7d6c1e1149783d68bba5c05f835c2d61.zip cpython-6ffa4a2a7d6c1e1149783d68bba5c05f835c2d61.tar.gz cpython-6ffa4a2a7d6c1e1149783d68bba5c05f835c2d61.tar.bz2 |
Fix undefined behaviour (left shift of negative value) in long_hash. Also,
rewrap a line of length > 79, and update comments.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/longobject.c | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index 0a320e6..fdb5664 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -1962,7 +1962,7 @@ long_compare(PyLongObject *a, PyLongObject *b) static long long_hash(PyLongObject *v) { - long x; + unsigned long x; Py_ssize_t i; int sign; @@ -1977,17 +1977,18 @@ long_hash(PyLongObject *v) i = -(i); } #define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT) - /* The following loop produces a C long x such that (unsigned long)x - is congruent to the absolute value of v modulo ULONG_MAX. The - resulting x is nonzero if and only if v is. */ + /* The following loop produces a C 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 << PyLong_SHIFT) & ~PyLong_MASK) | + ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK); x += v->ob_digit[i]; - /* If the addition above overflowed (thinking of x as - unsigned), we compensate by incrementing. This preserves - the value modulo ULONG_MAX. */ - if ((unsigned long)x < v->ob_digit[i]) + /* If the addition above overflowed we compensate by + incrementing. This preserves the value modulo + ULONG_MAX. */ + if (x < v->ob_digit[i]) x++; } #undef LONG_BIT_PyLong_SHIFT |