strcpy

Karl Heuer karl at haddock.ima.isc.com
Tue Jun 20 05:54:50 AEST 1989


In article <2013 at dataio.Data-IO.COM> bright at dataio.Data-IO.COM (Walter Bright) writes:
>In article <13674 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
>>	memmove(dest, src, strlen(src));
>
>	memmove(dest, src, strlen(src) + 1);

(I die.  My replacement reads my uncommented code and deletes a fragment he
doesn't understand.  Eventually the subroutine is sold to the government, and
the bug causes nuclear missles to be launched by accident.  The other side
retaliates, and all die.  O the embarrassment.)%

This is correct, of course; it only works to stop after strlen() if the
receiving buffer is known to have a null character in the appropriate spot.
Even if this were known information (e.g. if we're right-shifting a string in
a null-padded buffer), it's cheaper to use the +1 than to document why it
isn't necessary.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
________
% First person to identify the reference wins a defunct root password.



More information about the Comp.lang.c mailing list