From b616515ba0eb99f50413858b98747ce4738d4ab4 Mon Sep 17 00:00:00 2001 From: Dario Manesku Date: Sat, 22 Aug 2015 18:49:01 +0200 Subject: [PATCH] Fixing stristr(). It now uses strnlen() instead of strlen() in its impl. --- include/bx/string.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index 242ae12..7f9224a 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -113,24 +113,24 @@ namespace bx return NULL; } - /// Find substring in string. Case insensitive. Limit search to _size. + /// Find substring in string. Case insensitive. Limit search to _max. inline const char* stristr(const char* _str, const char* _find, size_t _max) { const char* ptr = _str; - const size_t total = strlen(_str); - size_t len = _max < total ? _max : total; + size_t stringLen = strnlen(_str, _max); + const size_t findLen = strlen(_find); - for (const size_t searchLen = strlen(_find); len >= searchLen; ++ptr, --len) + for (; stringLen >= findLen; ++ptr, --stringLen) { // Find start of the string. while (tolower(*ptr) != tolower(*_find) ) { ++ptr; - --len; + --stringLen; // Search pattern lenght can't be longer than the string. - if (searchLen > len) + if (findLen > stringLen) { return NULL; }