SF bug #1054139: serious string hashing error in 2.4b1

_PyString_Resize() readied strings for mutation but did not invalidate
the cached hash value.
This commit is contained in:
Raymond Hettinger 2004-10-26 01:52:37 +00:00
parent 3ed238503d
commit 561fbf138d
3 changed files with 11 additions and 1 deletions

View file

@ -80,6 +80,15 @@ class CommonTest(unittest.TestCase):
args = self.fixtype(args) args = self.fixtype(args)
getattr(object, methodname)(*args) getattr(object, methodname)(*args)
def test_hash(self):
# SF bug 1054139: += optimization was not invalidating cached hash value
a = self.type2test('DNSSEC')
b = self.type2test('')
for c in a:
b += c
hash(b)
self.assertEqual(hash(a), hash(b))
def test_capitalize(self): def test_capitalize(self):
self.checkequal(' hello ', ' hello ', 'capitalize') self.checkequal(' hello ', ' hello ', 'capitalize')
self.checkequal('Hello ', 'Hello ','capitalize') self.checkequal('Hello ', 'Hello ','capitalize')

View file

@ -32,7 +32,7 @@ License Version 2.
Core and builtins Core and builtins
----------------- -----------------
... - Bug #1054139 _PyString_Resize() now invalidates its cached hash value.
Extension Modules Extension Modules
----------------- -----------------

View file

@ -3530,6 +3530,7 @@ _PyString_Resize(PyObject **pv, int newsize)
sv = (PyStringObject *) *pv; sv = (PyStringObject *) *pv;
sv->ob_size = newsize; sv->ob_size = newsize;
sv->ob_sval[newsize] = '\0'; sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0; return 0;
} }