diff options
author | Nick Mathewson <nickm@torproject.org> | 2014-04-07 23:03:04 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2014-04-07 23:03:04 -0400 |
commit | 595303fd1e1998e12c51901f1aea77311e973a44 (patch) | |
tree | 84b18b086564584369b02f7f735a0f98dd4679eb /src/common/compat.c | |
parent | 84df7d73c9f8892220184e301627d5bddb9c3f79 (diff) | |
parent | 6d9c332757c96e691ddeb024bd41035490a505d7 (diff) | |
download | tor-595303fd1e1998e12c51901f1aea77311e973a44.tar.gz tor-595303fd1e1998e12c51901f1aea77311e973a44.zip |
Merge remote-tracking branch 'public/bug10363_024_squashed'
Diffstat (limited to 'src/common/compat.c')
-rw-r--r-- | src/common/compat.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index 135f2c9af6..04c9d59235 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -560,21 +560,29 @@ tor_memmem(const void *_haystack, size_t hlen, #else /* This isn't as fast as the GLIBC implementation, but it doesn't need to * be. */ - const char *p, *end; + const char *p, *last_possible_start; const char *haystack = (const char*)_haystack; const char *needle = (const char*)_needle; char first; tor_assert(nlen); + if (nlen > hlen) + return NULL; + p = haystack; - end = haystack + hlen; + /* Last position at which the needle could start. */ + last_possible_start = haystack + hlen - nlen; first = *(const char*)needle; - while ((p = memchr(p, first, end-p))) { - if (p+nlen > end) - return NULL; + while ((p = memchr(p, first, last_possible_start + 1 - p))) { if (fast_memeq(p, needle, nlen)) return p; - ++p; + if (++p > last_possible_start) { + /* This comparison shouldn't be necessary, since if p was previously + * equal to last_possible_start, the next memchr call would be + * "memchr(p, first, 0)", which will return NULL. But it clarifies the + * logic. */ + return NULL; + } } return NULL; #endif |