Skip to content

Commit 3f4d39e

Browse files
author
mark.dickinson
committed
Fix undefined behaviour (left shift of negative value) in long_hash. Also,
rewrap a line of length > 79, and update comments. git-svn-id: http://svn.python.org/projects/python/trunk@68974 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 0ec6167 commit 3f4d39e

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

Objects/longobject.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@ long_compare(PyLongObject *a, PyLongObject *b)
19621962
static long
19631963
long_hash(PyLongObject *v)
19641964
{
1965-
long x;
1965+
unsigned long x;
19661966
Py_ssize_t i;
19671967
int sign;
19681968

@@ -1977,17 +1977,18 @@ long_hash(PyLongObject *v)
19771977
i = -(i);
19781978
}
19791979
#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
1980-
/* The following loop produces a C long x such that (unsigned long)x
1981-
is congruent to the absolute value of v modulo ULONG_MAX. The
1982-
resulting x is nonzero if and only if v is. */
1980+
/* The following loop produces a C long x such that x is congruent to
1981+
the absolute value of v modulo ULONG_MAX. The resulting x is
1982+
nonzero if and only if v is. */
19831983
while (--i >= 0) {
19841984
/* Force a native long #-bits (32 or 64) circular shift */
1985-
x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
1985+
x = ((x << PyLong_SHIFT) & ~PyLong_MASK) |
1986+
((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
19861987
x += v->ob_digit[i];
1987-
/* If the addition above overflowed (thinking of x as
1988-
unsigned), we compensate by incrementing. This preserves
1989-
the value modulo ULONG_MAX. */
1990-
if ((unsigned long)x < v->ob_digit[i])
1988+
/* If the addition above overflowed we compensate by
1989+
incrementing. This preserves the value modulo
1990+
ULONG_MAX. */
1991+
if (x < v->ob_digit[i])
19911992
x++;
19921993
}
19931994
#undef LONG_BIT_PyLong_SHIFT

0 commit comments

Comments
 (0)