strncpy

Bill Poser poser at csli.Stanford.EDU
Thu Dec 21 21:09:27 AEST 1989


	Why is it that strncpy(3) does not null terminate
the string if the length of the source string is greater than
or equal to the length of the target buffer? I cannot think of
any circumstances in which this behaviour is desirable, and
always end up null terminating it myself. The only case in
which a non-null-terminated string might be useful would be
one in which a different representation for strings (e.g.
descriptors) is used, in which case the use of str(n)cpy would
not be appropriate anyhow. It seems to me that the behaviour
of strncpy provides: (a) a source of bugs for people who don't
bother to arrange null-termination themselves; (b) extra work for
those who take care; or, (c) just enough of a pain to be unsafe and
use strcpy(3).

	So I claim that strncpy is mis-designed and should
null-terminate. Any comments?

						Bill



More information about the Comp.lang.c mailing list