"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Theo Buehler <tb@theobuehler.org>
Subject:
Re: Small fix for got_path_strip_trailing_slashes
To:
Martin <openbsd@academicsolutions.ch>
Cc:
gameoftrees@openbsd.org
Date:
Sat, 11 Jan 2020 21:44:48 +0100

Download raw body.

Thread
On Sat, Jan 11, 2020 at 07:33:43PM +0100, Martin wrote:
> Hi there
> 
> If got init gets called with '/' as path,
> got_path_strip_trailing_slashes first zeroes the slash and in the next
> step reads path[-1] as strlen(path) is 0. This patch splits the
> condition to first check for a path length > 0.

It was this way already in the existing code, but seems a bit silly to
call strlen(3) repeatedly here.  After removing a trailing slash, we
know exactly how long the resulting path is. Why not:

	size_t x;

	x = strlen(path);
	while (x-- > 0 && path[x] == '/')
		path[x] = '\0';

> 
> Best,
> 
> Martin
> 
> diff --git a/lib/path.c b/lib/path.c
> index fefe29c..19eb091 100644
> --- a/lib/path.c
> +++ b/lib/path.c
> @@ -393,8 +393,8 @@ got_path_strip_trailing_slashes(char *path)
>  {
>  	int x;
>  
> -	while (path[x = strlen(path) - 1] == '/')
> -		path[x] = '\0';
> +	while ((x = strlen(path)) > 0 && path[x - 1] == '/')
> +		path[x - 1] = '\0';
>  }
>  
>  /* based on findprog() from usr.sbin/which/which.c */
>