From: Ernst B. <e.b...@xe...> - 2006-09-13 19:27:42
|
On Wednesday 13 September 2006 21:18, Martin Hejl wrote: > >>>> + strncpy(thread_argv[0],name,strlen(thread_argv[0])); > >> > >> Thats why I used "strNcpy" with the original argv[0] length as limit. > >> It won't overwrite data it shouldn't, but will truncate the threads name > >> if its longer than the original binary name. > > > > silly me... I always mix up src and dst with strcpy :-) > > I didn't look at the context - but the thing that always bothered me > with (and kept me from using strncpy) is that if src is longer than > dest, the result is that dest is not terminated by \0 - unless I read > the manpage incorrectly: > > char *strncpy(char *restrict s1, const char *restrict s2, size_t n); > The strncpy() function shall copy not more than n bytes (bytes that > follow a null byte are not copied) from the array pointed to by > > s2 to the array pointed to by s1. > (...) > If there is no null byte in the first n bytes of the array pointed to > by s2, the result is not null-terminated > > Did I miss something? Well, usually that would be correct, but in this case, I'm overwriting an already NUL-terminated string with new data. strlen will return the length excluding the NUL char at the end of the original string, so, even in worst case, that NUL will stay where it is unmodified. /Ernst |