From: Georg Brandl <georg@python.org>
Date: Mon, 20 Feb 2012 23:50:13 +0000 (+0100)
Subject: Forgot the "empty string -> hash == 0" special case for strings.
X-Git-Tag: v3.3.0a1~118
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=16fa2a10970a92cba1ee1249ed7bcf7d2c131051;p=python

Forgot the "empty string -> hash == 0" special case for strings.
---

diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 716ca3f26a..75e9923f0d 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -11219,6 +11219,14 @@ unicode_hash(PyObject *self)
     if (PyUnicode_READY(self) == -1)
         return -1;
     len = PyUnicode_GET_LENGTH(self);
+    /*
+      We make the hash of the empty string be 0, rather than using
+      (prefix ^ suffix), since this slightly obfuscates the hash secret
+    */
+    if (len == 0) {
+        _PyUnicode_HASH(self) = 0;
+        return 0;
+    }
 
     /* The hash function as a macro, gets expanded three times below. */
 #define HASH(P)                                            \