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

From:
Martin <openbsd@academicsolutions.ch>
Subject:
Re: Small fix for got_path_strip_trailing_slashes
To:
Theo Buehler <tb@theobuehler.org>
Cc:
gameoftrees@openbsd.org
Date:
Sun, 12 Jan 2020 10:57:17 +0100

Download raw body.

Thread
On Sat, Jan 11, 2020 at 10:13:32PM +0100, Theo Buehler wrote:
> On Sat, Jan 11, 2020 at 10:05:40PM +0100, Martin wrote:
> > On Sat, Jan 11, 2020 at 09:44:48PM +0100, Theo Buehler wrote:
> > > 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';
> > 
> > Sure. Diff attached. Thanks for the feedback!
> 
> I should have made it more explicit, but I think you should change the
> int into a size_t since that's what strlen(3) returns. With that it's
> 
> ok tb.
> 

Sorry, I overlooked that. New diff attached, and again thanks!

Best,

Martin

diff --git a/lib/path.c b/lib/path.c
index fefe29cb..612f8eab 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -391,9 +391,10 @@ got_path_basename(char **s, const char *path)
 void
 got_path_strip_trailing_slashes(char *path)
 {
-	int x;
+	size_t x;
 
-	while (path[x = strlen(path) - 1] == '/')
+	x = strlen(path);
+	while (x-- > 0 && path[x] == '/')
 		path[x] = '\0';
 }