diff Objects/unicodeobject.c @ 72464:698fa089ce70

Issue #1621: Fix undefined behaviour in bytes.__hash__, str.__hash__, tuple.__hash__, frozenset.__hash__ and set indexing operations.
author Mark Dickinson <mdickinson@enthought.com>
date Sat, 24 Sep 2011 18:18:40 +0100
parents 0b2660384d4e
children 8beaa9a37387
line wrap: on
line diff
--- a/Objects/unicodeobject.c	Sat Sep 24 16:24:56 2011 +0100
+++ b/Objects/unicodeobject.c	Sat Sep 24 18:18:40 2011 +0100
@@ -7721,22 +7721,22 @@
 }
 
 /* Believe it or not, this produces the same value for ASCII strings
-   as string_hash(). */
+   as bytes_hash(). */
 static Py_hash_t
 unicode_hash(PyUnicodeObject *self)
 {
     Py_ssize_t len;
     Py_UNICODE *p;
-    Py_hash_t x;
+    Py_uhash_t x;
 
     if (self->hash != -1)
         return self->hash;
     len = Py_SIZE(self);
     p = self->str;
-    x = *p << 7;
+    x = (Py_uhash_t)*p << 7;
     while (--len >= 0)
-        x = (1000003*x) ^ *p++;
-    x ^= Py_SIZE(self);
+        x = (1000003U*x) ^ (Py_uhash_t)*p++;
+    x ^= (Py_uhash_t)Py_SIZE(self);
     if (x == -1)
         x = -2;
     self->hash = x;