replace: count_strstr - Handle an edge case where sub is empty
authorMartin Lewis <martin.lewis.x84@gmail.com>
Sun, 15 Sep 2019 16:51:30 +0000 (18:51 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 9 Oct 2019 12:39:41 +0000 (14:39 +0200)
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>
libbb/replace.c

index a661d96e67359562c33a61aa6e923dfbe533bb8d..6183d3e6fa3aadb27c9ae9ed7f29c4c2e3754133 100644 (file)
@@ -15,6 +15,10 @@ unsigned FAST_FUNC count_strstr(const char *str, const char *sub)
        size_t sub_len = strlen(sub);
        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) {
                count++;
                str += sub_len;