chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

Eliminate uses of strncpy()

Open lassik opened this issue 2 years ago • 3 comments

Can be replaced with memcpy() or snprintf().

lassik avatar Aug 18 '21 17:08 lassik

Why?

ashinn avatar Aug 18 '21 20:08 ashinn

strncpy() doesn't guarantee null termination: https://man.openbsd.org/strncpy#EXAMPLES

If you know the length of your strings, you might as well use memcpy() directly. If you don't know, or don't want to check, snprintf() is safer, since it takes the buffer size and guarantees null termination.

strlcpy() is also safe, but it's non-standard.

lassik avatar Aug 19 '21 04:08 lassik

There's enough space allocated, the 0 byte will be copied. I'd allow a patch to switch everything to memcpy with a manual 0-byte setting, but I don't see the point of snprintf here (you're not checking the return value anyway).

ashinn avatar Aug 19 '21 04:08 ashinn