summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2012-02-21 02:44:56 (GMT)
committerBenjamin Peterson <benjamin@python.org>2012-02-21 02:44:56 (GMT)
commitaee9dfba4a9230f2832dd69d67e92f8e0490a163 (patch)
tree27a9896969ac7ff79dc75017cff121a077c3eb6e /Objects
parent34b345b8885e5db8ab6627c081ca86a8b78b6989 (diff)
parentb19fb2462eac776746f6cb40cc84b0587c83b9bc (diff)
downloadcpython-aee9dfba4a9230f2832dd69d67e92f8e0490a163.zip
cpython-aee9dfba4a9230f2832dd69d67e92f8e0490a163.tar.gz
cpython-aee9dfba4a9230f2832dd69d67e92f8e0490a163.tar.bz2
merge 2.6 with hash randomization fix
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bufferobject.c12
-rw-r--r--Objects/object.c2
-rw-r--r--Objects/stringobject.c12
-rw-r--r--Objects/unicodeobject.c12
4 files changed, 35 insertions, 3 deletions
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
index 0f7e763..c52f0bc 100644
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -334,10 +334,20 @@ buffer_hash(PyBufferObject *self)
return -1;
p = (unsigned char *) ptr;
len = size;
- x = *p << 7;
+ /*
+ We make the hash of the empty buffer be 0, rather than using
+ (prefix ^ suffix), since this slightly obfuscates the hash secret
+ */
+ if (len == 0) {
+ self->b_hash = 0;
+ return 0;
+ }
+ x = _Py_HashSecret.prefix;
+ x ^= *p << 7;
while (--len >= 0)
x = (1000003*x) ^ *p++;
x ^= size;
+ x ^= _Py_HashSecret.suffix;
if (x == -1)
x = -2;
self->b_hash = x;
diff --git a/Objects/object.c b/Objects/object.c
index 94a18d3..9303086 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1094,6 +1094,8 @@ PyObject_HashNotImplemented(PyObject *self)
return -1;
}
+_Py_HashSecret_t _Py_HashSecret;
+
long
PyObject_Hash(PyObject *v)
{
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index d6c4f77..49d1864 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -1265,11 +1265,21 @@ string_hash(PyStringObject *a)
if (a->ob_shash != -1)
return a->ob_shash;
len = Py_SIZE(a);
+ /*
+ 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) {
+ a->ob_shash = 0;
+ return 0;
+ }
p = (unsigned char *) a->ob_sval;
- x = *p << 7;
+ x = _Py_HashSecret.prefix;
+ x ^= *p << 7;
while (--len >= 0)
x = (1000003*x) ^ *p++;
x ^= Py_SIZE(a);
+ x ^= _Py_HashSecret.suffix;
if (x == -1)
x = -2;
a->ob_shash = x;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 84252ec..a65deff 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -6541,11 +6541,21 @@ unicode_hash(PyUnicodeObject *self)
if (self->hash != -1)
return self->hash;
len = PyUnicode_GET_SIZE(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) {
+ self->hash = 0;
+ return 0;
+ }
p = PyUnicode_AS_UNICODE(self);
- x = *p << 7;
+ x = _Py_HashSecret.prefix;
+ x ^= *p << 7;
while (--len >= 0)
x = (1000003*x) ^ *p++;
x ^= PyUnicode_GET_SIZE(self);
+ x ^= _Py_HashSecret.suffix;
if (x == -1)
x = -2;
self->hash = x;