strncpy() supposedly protects from buffer overflows. But if it prevents an overflow without null terminating, in all likelihood a subsequent string operation is going to overflow. So to protect against this I find myself doing:
strncpy( dest, src, LEN );
dest[LEN - 1] = '\0';
man strncpy gives:
The
strncpy()function is similar, except that not more thannbytes ofsrcare copied. Thus, if there is no null byte among the firstnbytes ofsrc, the result will not be null-terminated.
Without null terminating something seemingly innocent like:
   printf( "FOO: %s\n", dest );
...could crash.
Are there better, safer alternatives to strncpy()?
 
     
     
     
     
     
     
     
     
     
     
     
     
    