2 ms·
They wrote: // XXX: after this buf may not be null-terminated strncat(buf, another_buffer, BUF_SIZE - strlen(buf)); This code is indeed bad, but not becau
by jwilk 8y ago
They wrote:
// 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);