diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-15 11:32:41 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-15 11:32:41 (GMT) |
commit | b946af58978ebe29ff6149e8f31e0edc299e4c03 (patch) | |
tree | b2813d7563a56f7b2a83a2d6e6d8563621b97877 /Objects | |
parent | 45cb7f6529ce55f9d942fe116495d17f1084a86d (diff) | |
parent | 18ba40b9450d9e45b9581b7c9017f839808b56dc (diff) | |
download | cpython-b946af58978ebe29ff6149e8f31e0edc299e4c03.zip cpython-b946af58978ebe29ff6149e8f31e0edc299e4c03.tar.gz cpython-b946af58978ebe29ff6149e8f31e0edc299e4c03.tar.bz2 |
Check for NULL before the pointer aligning in fastsearch_memchr_1char.
There is no guarantee that NULL is aligned.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/stringlib/fastsearch.h | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/Objects/stringlib/fastsearch.h b/Objects/stringlib/fastsearch.h index ecf885e..55ac77d 100644 --- a/Objects/stringlib/fastsearch.h +++ b/Objects/stringlib/fastsearch.h @@ -38,25 +38,18 @@ STRINGLIB(fastsearch_memchr_1char)(const STRINGLIB_CHAR* s, Py_ssize_t n, STRINGLIB_CHAR ch, unsigned char needle, Py_ssize_t maxcount, int mode) { - void *candidate; - const STRINGLIB_CHAR *found; - -#define DO_MEMCHR(memchr, s, needle, nchars) do { \ - candidate = memchr((const void *) (s), (needle), (nchars) * sizeof(STRINGLIB_CHAR)); \ - found = (const STRINGLIB_CHAR *) _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR)); \ - } while (0) - if (mode == FAST_SEARCH) { const STRINGLIB_CHAR *ptr = s; const STRINGLIB_CHAR *e = s + n; while (ptr < e) { - DO_MEMCHR(memchr, ptr, needle, e - ptr); - if (found == NULL) + void *candidate = memchr((const void *) ptr, needle, (e - ptr) * sizeof(STRINGLIB_CHAR)); + if (candidate == NULL) return -1; - if (sizeof(STRINGLIB_CHAR) == 1 || *found == ch) - return (found - s); + ptr = (const STRINGLIB_CHAR *) _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR)); + if (sizeof(STRINGLIB_CHAR) == 1 || *ptr == ch) + return (ptr - s); /* False positive */ - ptr = found + 1; + ptr++; } return -1; } @@ -66,9 +59,11 @@ STRINGLIB(fastsearch_memchr_1char)(const STRINGLIB_CHAR* s, Py_ssize_t n, faster than our hand-written loop in FASTSEARCH below */ else if (mode == FAST_RSEARCH) { while (n > 0) { - DO_MEMCHR(memrchr, s, needle, n); - if (found == NULL) + const STRINGLIB_CHAR *found; + void *candidate = memrchr((const void *) s, needle, n * sizeof(STRINGLIB_CHAR)); + if (candidate == NULL) return -1; + found = (const STRINGLIB_CHAR *) _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR)); n = found - s; if (sizeof(STRINGLIB_CHAR) == 1 || *found == ch) return n; |