[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] replace: count_strstr - Handle an edge case where sub is empty
From:       Martin Lewis <martin.lewis.x84 () gmail ! com>
Date:       2019-09-15 16:51:30
Message-ID: 1568566290-60641-1-git-send-email-martin.lewis.x84 () gmail ! com
[Download RAW message or body]

If sub is empty, avoids an infinite loop.

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

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic