From: Theo Buehler Subject: Re: Small fix for got_path_strip_trailing_slashes To: Martin Cc: gameoftrees@openbsd.org Date: Sat, 11 Jan 2020 22:13:32 +0100 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. > > Best, > > Martin > > diff --git a/lib/path.c b/lib/path.c > index fefe29cb..1eeee50e 100644 > --- a/lib/path.c > +++ b/lib/path.c > @@ -393,7 +393,8 @@ got_path_strip_trailing_slashes(char *path) > { > int x; > > - while (path[x = strlen(path) - 1] == '/') > + x = strlen(path); > + while (x-- > 0 && path[x] == '/') > path[x] = '\0'; > } >