Quote:Original post by Nurgle
strncpy(temp->filename, filename, strlen(temp->filename));
This completely destroys the possibility of a buffer overflow. In your code above, it's redundant, but still a good habit to get into.
If you find yourself not doing the right thing out of habit, you could always hack together a quick macro...
#define strcpy(dest, src) strncpy(dest, src, strlen(dest));
This really isn't a good idea. If you don't know what the destination buffer currently contains, you can't expect strlen to return its size. Nothing guarantees the buffer is null-terminated (and you can't do destBuffer[destSize-1] = '\0', since there might be a '\0' somewhere else in the buffer, in which case strlen will return a value smaller than the buffer's length).
What you can do:
1. Use std::string.
2. Dynamically allocate the destination buffer (strlen(srcBuffer)+1), and then copy (both strcpy and strncpy will work fine in this case).
3. Use a fixed-size destination buffer, and use strncpy(dest, src, destSize-1). However, in this case, you might truncate some of the string you're trying to copy. Also, note that in this case you must manually set the last byte of the destination buffer to '\0', since strncpy might not do it if the source string is too long.