Copy strings with "strcpy", not an idiom
km at cadre.UUCP
km at cadre.UUCP
Wed Mar 20 05:05:38 AEST 1985
>Unfortunately, the string copy routines in the standard library are
>neither safe nor convenient. strcpy is only usable if you KNOW that
>overrunning is impossible. strncpy is totally unusable because if
>overrunning occurs, it may not nul terminate the destination, and
>no indication is returned to allow detection of this event.
YES YES YES! What is worse is that overrunning may not cause a noticeable
effect on a given machine, and the problem is not realized until the
code is ported to a second machine. When we ported the Jove sources
from vax/unix to ibm-pc/ms-dos this exact problem occurred: an incorrect use
of strncpy() resulted in an overrun with no terminal '/0': this had no
noticeable effect on the vax, but repeatedly crashed the program on the pc.
The effects of the overrun were of course on data that was referenced
far away from the actual error in the code - making debugging exceedingly
difficult.
More information about the Comp.lang.c
mailing list