[comp.os.minix] bug in 1.5.0 ansi strncpy

ghelmer@DSUVAX.uucp (Guy Helmer) (02/02/90)

In article <792@dftsrv.gsfc.nasa.gov>, stailey@iris613.gsfc.nasa.gov (Ken Stailey) writes:
> The 1.5.0 strncpy will copy 9 bytes into an eight byte array when invoked
> as strncpy(dest, source, 8); where dest is declared char dest[8];
> It copies & tests to see if it should copy at the same time. I.e.
> if (count < 0 && (*dest++ == *source++) != '\0').

This looks like the line should be changed to:

if (count <= 0 && (*dest++ == *source++) != '\0')

to fix the problem of writing too many characters to dest.
C guarantees that code to the right of the && will not be
executed if the "count <= 0" fails.  (At least K&R guarantee it.)
I haven't created the 1.5.0 sources yet, so I can't test it.

> INET stailey@iris613.gsfc.nasa.gov
> UUCP {backbone}!dftsrv!iris613!stailey


-- 
Guy Helmer                              ...!uunet!loft386!dsuvax!ghelmer
Dakota State University Computing Services           helmer@sdnet.bitnet
Software Engineering: "'How to program if you cannot.'" - Dijkstra