strncat is insufficient

Stan Brown, Oak Road Systems browns at iccgcc.decnet.ab.com
Sun Aug 26 10:26:04 AEST 1990


In article <587 at array.UUCP>, colin at array.UUCP (Colin Plumb) writes:
> char *strlimcat(char *dst, char const *src, int maxlen)
> {
> 	int len;
> 
> 	len = strlen(dst);
> 	if (len < maxlen)
> 		strncpy(dst+len, src, maxlen-len);
> 	return dst;
> }

Caution:  This will not necessarily put a '\0' at the end of the string.

Can Karl, or someone else who knows, explain why strncpy was standardized
to copy n characters even at the expense of a zero byte; or why no
alternative that always terminates the string was provided.  This
implementation is a fertile source of bugs that seem to bite every C
programmer at least once (and not always early in a career!). 

Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A.         (216) 371-0043
The opinions expressed are mine. Mine alone!  Nobody else is responsible for
them or even endorses them--except my cat Dexter, and he signed the power of
attorney only under my threat to cut off his Cat Chow!



More information about the Comp.std.c mailing list