by Hello71 · 8y ago · 0 comments
> As with strcat(), the resulting string in dest is always null-terminated. > Therefore, the size of dest must be at least strlen(dest)+n+1.
// XXX: after this buf may not be null-terminated
strncat(buf, another_buffer, BUF_SIZE - strlen(buf));
This code is indeed bad, but not because the result won't be null-terminated, but because it's an off-by-one buffer overflow.Despite this misunderstanding, the proposed fix is correct:
// OK: ensure buf is null-terminated after concatenation
strncat(buf, another_buffer, BUF_SIZE - strlen(buf) - 1);