diff options
author | Martin Lewis <martin.lewis.x84@gmail.com> | 2019-09-15 18:51:30 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2019-10-09 14:39:41 +0200 |
commit | 7011eca83afc313098f9869eea36742d4506bc02 (patch) | |
tree | fc6d769828c261edb562a372d61c14142d5f32d0 | |
parent | dd4686128290b34d61becaaba88c54d5213f7aa5 (diff) | |
download | busybox-w32-7011eca83afc313098f9869eea36742d4506bc02.tar.gz busybox-w32-7011eca83afc313098f9869eea36742d4506bc02.tar.bz2 busybox-w32-7011eca83afc313098f9869eea36742d4506bc02.zip |
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>
-rw-r--r-- | libbb/replace.c | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/libbb/replace.c b/libbb/replace.c index a661d96e6..6183d3e6f 100644 --- a/libbb/replace.c +++ b/libbb/replace.c | |||
@@ -15,6 +15,10 @@ unsigned FAST_FUNC count_strstr(const char *str, const char *sub) | |||
15 | size_t sub_len = strlen(sub); | 15 | size_t sub_len = strlen(sub); |
16 | unsigned count = 0; | 16 | unsigned count = 0; |
17 | 17 | ||
18 | /* If sub is empty, avoid an infinite loop */ | ||
19 | if (sub_len == 0) | ||
20 | return strlen(str) + 1; | ||
21 | |||
18 | while ((str = strstr(str, sub)) != NULL) { | 22 | while ((str = strstr(str, sub)) != NULL) { |
19 | count++; | 23 | count++; |
20 | str += sub_len; | 24 | str += sub_len; |