Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2370)

Side by Side Diff: Objects/unicodeobject.c

Issue 13703: Hash collision security issue
Patch Set: Created 1 year, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Objects/object.c ('k') | PCbuild/pythoncore.vcproj » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 2
3 Unicode implementation based on original code by Fredrik Lundh, 3 Unicode implementation based on original code by Fredrik Lundh,
4 modified by Marc-Andre Lemburg <mal@lemburg.com>. 4 modified by Marc-Andre Lemburg <mal@lemburg.com>.
5 5
6 Major speed upgrades to the method implementations at the Reykjavik 6 Major speed upgrades to the method implementations at the Reykjavik
7 NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. 7 NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke.
8 8
9 Copyright (c) Corporation for National Research Initiatives. 9 Copyright (c) Corporation for National Research Initiatives.
10 10
(...skipping 11223 matching lines...) Expand 10 before | Expand all | Expand 10 after
11234 } 11234 }
11235 } 11235 }
11236 x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); 11236 x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self);
11237 11237
11238 if (x == -1) 11238 if (x == -1)
11239 x = -2; 11239 x = -2;
11240 _PyUnicode_HASH(self) = x; 11240 _PyUnicode_HASH(self) = x;
11241 return x; 11241 return x;
11242 } 11242 }
11243 #undef HASH 11243 #undef HASH
11244
11245 Py_hash_t
11246 _PyUnicode_RandomizedHash(PyObject *self)
11247 {
11248 Py_ssize_t len;
11249 Py_uhash_t x;
11250
11251 if (PyUnicode_READY(self) == -1)
11252 return -1;
11253 len = PyUnicode_GET_LENGTH(self);
11254 if (len == 0) {
11255 _PyUnicode_HASH(self) = 0;
11256 return 0;
11257 }
11258
11259 /* Alternate definition of the hash function as a macro to above */
11260 #define HASH(P) \
11261 x = _Py_HashSecret.prefix ^ (Py_uhash_t)*P << 7; \
11262 while (--len >= 0) \
11263 x = (_PyHASH_MULTIPLIER*x) ^ (Py_uhash_t)*P++;
11264
11265 switch (PyUnicode_KIND(self)) {
11266 case PyUnicode_1BYTE_KIND: {
11267 const unsigned char *c = PyUnicode_1BYTE_DATA(self);
11268 HASH(c);
11269 break;
11270 }
11271 case PyUnicode_2BYTE_KIND: {
11272 const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self);
11273 HASH(s);
11274 break;
11275 }
11276 default: {
11277 Py_UCS4 *l;
11278 assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND &&
11279 "Impossible switch case in unicode_hash");
11280 l = PyUnicode_4BYTE_DATA(self);
11281 HASH(l);
11282 break;
11283 }
11284 }
11285 x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self);
11286 x ^= _Py_HashSecret.suffix;
11287
11288 if (x == -1)
11289 x = -2;
11290 /* we do *not* update _PyUnicode_HASH(self) here */
11291 return x;
11292 }
11293 #undef HASH
11294
11244 11295
11245 PyDoc_STRVAR(index__doc__, 11296 PyDoc_STRVAR(index__doc__,
11246 "S.index(sub[, start[, end]]) -> int\n\ 11297 "S.index(sub[, start[, end]]) -> int\n\
11247 \n\ 11298 \n\
11248 Like S.find() but raise ValueError when the substring is not found."); 11299 Like S.find() but raise ValueError when the substring is not found.");
11249 11300
11250 static PyObject * 11301 static PyObject *
11251 unicode_index(PyObject *self, PyObject *args) 11302 unicode_index(PyObject *self, PyObject *args)
11252 { 11303 {
11253 Py_ssize_t result; 11304 Py_ssize_t result;
(...skipping 3202 matching lines...) Expand 10 before | Expand all | Expand 10 after
14456 PyMODINIT_FUNC 14507 PyMODINIT_FUNC
14457 PyInit__string(void) 14508 PyInit__string(void)
14458 { 14509 {
14459 return PyModule_Create(&_string_module); 14510 return PyModule_Create(&_string_module);
14460 } 14511 }
14461 14512
14462 14513
14463 #ifdef __cplusplus 14514 #ifdef __cplusplus
14464 } 14515 }
14465 #endif 14516 #endif
OLDNEW
« no previous file with comments | « Objects/object.c ('k') | PCbuild/pythoncore.vcproj » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7