[comp.os.minix] Bug in library routine strncpy

john@polyof.UUCP ( John Buck ) (05/14/87)

In article <3860@cae780.TEK.COM>, hubble@cae780.TEK.COM (Larry Hubble) writes:
> ...  I believe there is also a bug in the
> library routine strncpy -- it doesn't null terminate the string if the string
> to copy is really longer that "n" characters.  I have not verified or fixed
> this yet, but will post it when I get a chance to cure it.
> 

From STRING(3C)---
	"strncpy(s1, s2, n)
	char *s1, *s2;
	int n;

	Strncpy copies exactly n characters, truncating s2 or adding null
characters to s1 if necessary.  The result will not be null-terminated
if the length of s2 is n or more."

I.E. There is no bug with strncpy().

> The library routine getenv() has a bug in it.  If the environment variable you
> are requesting does not happen to be the first one in the environment table,
> getenv() goes into an infinite loop.
> 
> The fix is very simple.  The first executable line is:
> 
>      while ((p = *v) != NULL) {
> 
> It should be:
>     
>     while ((p = *v++) != NULL) {
> 
All the versions of getenv() I looked at (4.2bsd, system 5, system 3,
Version 7) did not have the indicated looping bug.  The pointer "p" was
incremented in the next line of code; apparently the MINIX people broke
this, or the poster did not read the code carefully.

John Buck
polyof!john