diff options
author | Dimitri van Heesch <doxygen@gmail.com> | 2019-12-23 13:13:29 (GMT) |
---|---|---|
committer | Dimitri van Heesch <doxygen@gmail.com> | 2019-12-23 13:13:29 (GMT) |
commit | ecc4f838db18336c4afbc6dabb1a8edbc6d8bbd0 (patch) | |
tree | 0901e6b028ab4bb94c67398b6317bdffb24b18cb /src/portable.cpp | |
parent | 54ec927d886b895b1ec7bbe62c143b052970fdf6 (diff) | |
download | Doxygen-ecc4f838db18336c4afbc6dabb1a8edbc6d8bbd0.zip Doxygen-ecc4f838db18336c4afbc6dabb1a8edbc6d8bbd0.tar.gz Doxygen-ecc4f838db18336c4afbc6dabb1a8edbc6d8bbd0.tar.bz2 |
Fix for use of non portable strnstr function (part 2)
Diffstat (limited to 'src/portable.cpp')
-rw-r--r-- | src/portable.cpp | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/portable.cpp b/src/portable.cpp index 9927c45..3ee1081 100644 --- a/src/portable.cpp +++ b/src/portable.cpp @@ -476,13 +476,41 @@ void Portable::setShortDir(void) #endif } +/* Return the first occurrence of NEEDLE in HAYSTACK. */ +static const char * portable_memmem (const char *haystack, size_t haystack_len, + const char *needle, size_t needle_len) +{ + const char *const last_possible = haystack + haystack_len - needle_len; + + if (needle_len == 0) + // The first occurrence of the empty string should to occur at the beginning of the string. + { + return haystack; + } + + // Sanity check + if (haystack_len < needle_len) + { + return 0; + } + + for (const char *begin = haystack; begin <= last_possible; ++begin) + { + if (begin[0] == needle[0] && !memcmp(&begin[1], needle + 1, needle_len - 1)) + { + return begin; + } + } + + return 0; +} -char *Portable::strnstr(const char *haystack, const char *needle, size_t haystack_len) +const char *Portable::strnstr(const char *haystack, const char *needle, size_t haystack_len) { size_t needle_len = strnlen(needle, haystack_len); if (needle_len < haystack_len || !needle[needle_len]) { - char *x = static_cast<char*>(memmem(haystack, haystack_len, needle, needle_len)); + const char *x = portable_memmem(haystack, haystack_len, needle, needle_len); if (x && !memchr(haystack, 0, x - haystack)) { return x; |