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

Denys Vlasenko vda.linux at googlemail.com
Thu Oct 10 12:53:01 UTC 2019


Applied, thanks

On Sun, Sep 15, 2019 at 6:52 PM Martin Lewis <martin.lewis.x84 at gmail.com> wrote:
>
> 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
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox


More information about the busybox mailing list