[PATCH] nc_bloaty: Code shrink in findline, use strchr instead.

Timo Teras timo.teras at iki.fi
Thu Jan 16 00:27:36 UTC 2020


On Wed, 15 Jan 2020 10:30:49 -0600
Martin Lewis <martin.lewis.x84 at gmail.com> wrote:

> Signed-off-by: Martin Lewis <martin.lewis.x84 at gmail.com>
> ---
>  networking/nc_bloaty.c | 21 ++++-----------------
>  1 file changed, 4 insertions(+), 17 deletions(-)
> 
> diff --git a/networking/nc_bloaty.c b/networking/nc_bloaty.c
> index 034e03d21..9848ea794 100644
> --- a/networking/nc_bloaty.c
> +++ b/networking/nc_bloaty.c
> @@ -237,24 +237,11 @@ static void arm(unsigned secs)
>   Not distinguishing \n vs \r\n for the nonce; it just works as is...
> */ static unsigned findline(char *buf, unsigned siz)
>  {
> -	char * p;
> -	int x;
> -	if (!buf)                        /* various sanity checks...
> */
> -		return 0;
> -	if (siz > BIGSIZ)
> +	char *p;
> +	if (!buf || siz > BIGSIZ)
>  		return 0;
> -	x = siz;
> -	for (p = buf; x > 0; x--) {
> -		if (*p == '\n') {
> -			x = (int) (p - buf);
> -			x++;                        /* 'sokay if it
> points just past the end! */ -Debug("findline returning %d", x);
> -			return x;
> -		}
> -		p++;
> -	} /* for */
> -Debug("findline returning whole thing: %d", siz);
> -	return siz;
> +	p = strchr(buf, '\n');

This can give incorrect results, or even crash in certain conditions.
strchr does not have the upper bound 'siz' argument.

memchr could work instead.

> +	return p ? (p - buf + 1) : siz;
>  } /* findline */
>  
>  /* doexec:



More information about the busybox mailing list