3 ms·
> I think he's absolutely right. The str* functions are just worse versions of the mem* functions. Per Todd C. Miller (creator of strlcpy and maintainer of sud
by throw0101a 4y ago
> I think he's absolutely right. The str* functions are just worse versions of the mem* functions.
Per Todd C. Miller (creator of strlcpy and maintainer of sudo) and Theo de Raadt (OpenBSD) in these 1999 (PostScript) slides, the simplest implementation of strlcpy() uses (can use) memcpy():
size_t strlcpy(char *dst, const char *src, size_t siz)
{
size_t n;
size_t slen = strlen(src);
if (siz) {
if ((n = MIN(slen, siz - 1)))
memcpy(dst, src, n);
dst[n] = ’\0’;
}
return(slen);
}
* https://www.openbsd.org/papers/strlcpy-slides.ps https://www.openbsd.org/papers/strlcpy-slides.ps
The current OpenBSD is currently different:
* https://github.com/openbsd/src/blob/master/lib/libc/string/strlcpy.c https://github.com/openbsd/src/blob/master/lib/libc/string/s...
* https://cvsweb.openbsd.org/src/lib/libc/string/strlcpy.c https://cvsweb.openbsd.org/src/lib/libc/string/strlcpy.c
One problem with memcpy() is that it returns (void *), so there is no way to know if you've truncated things. From the USENIX paper on strlcpy():
> The strlcpy() and strlcat() functions return the total length of the string they tried to create. For strlcpy() that is simply the length of the source; for strlcat() that means the length of the destination (before concatenation) plus the length of the source. To check for truncation, the programmer need only verify that the return value is less than the size parameter. Thus, if truncation has occurred, the number of bytes needed to store the entire string is now known and the programmer may allocate more space and re-copy the strings if he or she wishes.
* https://www.usenix.org/legacy/events/usenix99/full_papers/millert/millert.pdf https://www.usenix.org/legacy/events/usenix99/full_papers/mi...