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

Martin Lewis martin.lewis.x84 at gmail.com
Sun Sep 15 16:51:30 UTC 2019


If sub is empty, avoids an infinite loop.

Signed-off-by: Martin Lewis <martin.lewis.x84 at gmail.com>
---
 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



More information about the busybox mailing list