replace: count_strstr - Handle an edge case where sub is empty

If sub is empty, avoids an infinite loop.

function                                             old     new   delta
count_strstr                                          45      63     +18

Signed-off-by: Martin Lewis <martin.lewis.x84@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Martin Lewis 2019-09-15 18:51:30 +02:00 committed by Denys Vlasenko
parent dd46861282
commit 7011eca83a

View File

@ -15,6 +15,10 @@ unsigned FAST_FUNC count_strstr(const char *str, const char *sub)
size_t sub_len = strlen(sub); size_t sub_len = strlen(sub);
unsigned count = 0; unsigned count = 0;
/* If sub is empty, avoid an infinite loop */
if (sub_len == 0)
return strlen(str) + 1;
while ((str = strstr(str, sub)) != NULL) { while ((str = strstr(str, sub)) != NULL) {
count++; count++;
str += sub_len; str += sub_len;