If sub is empty, avoids an infinite loop.

Signed-off-by: Martin Lewis <[email protected]>
---
 libbb/replace.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libbb/replace.c b/libbb/replace.c
index a661d96..6183d3e 100644
--- a/libbb/replace.c
+++ b/libbb/replace.c
@@ -11,14 +11,18 @@
 #include "libbb.h"
 
 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;
        }
        return count;
 }
 
-- 
1.9.1

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to