3 ms·
This is not news to experienced C developers. All it takes to avoid a missing NUL terminator is something like #define safer_strncpy(dest, src, n) \ do
by defined 9y ago
This is not news to experienced C developers.
All it takes to avoid a missing NUL terminator is something like
#define safer_strncpy(dest, src, n) \
do { \
if (n > 0) \
strncpy(dest, src, n)[n - 1] = '\0'; \
} while (0)
- jwilk 9y agoThis still appends null bytes to fill the whole buffer, which is almost never what you want. Use snprintf() or strncat(), as suggested by the article.
- defined 9y agoThat's not unsafe, which is what the article is about.
- torg 9y agoBut as an inexperienced C developer, I found it quite helpful.
- defined 9y ago> But as an inexperienced C developer, I found it quite helpful. Fair enough. I didn't mean to come across as arrogant; I just wasn't thinking about the audience. I'm happy you got value from it.