Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 561fbf1

Browse files
committed
SF bug #1054139: serious string hashing error in 2.4b1
_PyString_Resize() readied strings for mutation but did not invalidate the cached hash value.
1 parent 3ed2385 commit 561fbf1

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/test/string_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ def checkcall(self, object, methodname, *args):
8080
args = self.fixtype(args)
8181
getattr(object, methodname)(*args)
8282

83+
def test_hash(self):
84+
# SF bug 1054139: += optimization was not invalidating cached hash value
85+
a = self.type2test('DNSSEC')
86+
b = self.type2test('')
87+
for c in a:
88+
b += c
89+
hash(b)
90+
self.assertEqual(hash(a), hash(b))
91+
8392
def test_capitalize(self):
8493
self.checkequal(' hello ', ' hello ', 'capitalize')
8594
self.checkequal('Hello ', 'Hello ','capitalize')

Misc/NEWS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ License Version 2.
3232
Core and builtins
3333
-----------------
3434

35-
...
35+
- Bug #1054139 _PyString_Resize() now invalidates its cached hash value.
3636

3737
Extension Modules
3838
-----------------

Objects/stringobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3530,6 +3530,7 @@ _PyString_Resize(PyObject **pv, int newsize)
35303530
sv = (PyStringObject *) *pv;
35313531
sv->ob_size = newsize;
35323532
sv->ob_sval[newsize] = '\0';
3533+
sv->ob_shash = -1; /* invalidate cached hash value */
35333534
return 0;
35343535
}
35353536

0 commit comments

Comments
 (0)