]> granicus.if.org Git - python/commitdiff
Fix undefined behaviour (left shift of negative value) in long_hash. Also,
authorMark Dickinson <dickinsm@gmail.com>
Mon, 26 Jan 2009 21:36:30 +0000 (21:36 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Mon, 26 Jan 2009 21:36:30 +0000 (21:36 +0000)
rewrap a line of length > 79, and update comments.

Objects/longobject.c

index 0a320e6bffc0142e835ad3a0c33a4685909d6429..fdb5664af85ad923b2602117d345413af79a5134 100644 (file)
@@ -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