[Toybox] [PATCH] strncat bug in mountpoint.c

Rob Landley rob at landley.net
Tue Nov 25 22:23:41 PST 2014


On 11/25/14 19:14, enh wrote:
> strncat is a pain to use because the last argument is the remaining
> space in the buffer, not the buffer size. sadly glibc doesn't have
> strlcat...

I can probably add it to lib/lib.c. (I first implemented strlcpy() under
turbo C in 1991. I learned C in 1990. I thought I'd come up with a
clever name for it and everything...) I believe I didn't earlier to
avoid conflicts with libraries it _is_ in. (MacOS X? Ah, and it's in
uClibc as well.)

So I'd need portability.h magic for using the library version instead of
mine, except

> diff --git a/toys/other/mountpoint.c b/toys/other/mountpoint.c
> index 29b8ae6..6124129 100644
> --- a/toys/other/mountpoint.c
> +++ b/toys/other/mountpoint.c
> @@ -43,7 +43,7 @@ void mountpoint_main(void)
>      if (!quiet) printf("%s: not a directory\n", toybuf);
>      return;
>    }
> -  strncat(toybuf, "/..", sizeof(toybuf));
> +  strncat(toybuf, "/..", sizeof(toybuf)-strlen(toybuf)-1);

That's just craptacular. _And_ it memsets the rest of the buffer with
zeroes.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/strncpy.html

I use snprintf() a lot. (Well, ok, I was using sprintf("%*s", len, str)
a lot but that's characters not bytes so if you're not in the C locale
it doesn't limit right.)

I think I need to audit the entire codebase to remove strncat and
strncpy both. But not right now, I'm falling asleep...

Rob

 1416983021.0


More information about the Toybox mailing list