strlcpy(3bsd)

πŸ“¦
version 0.12.2 Β· 3 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– strlcpy, strlcat β€” size-bounded string copying and concatenation πŸ’–

okay bestie, welcome to strlcpy(3bsd), a LOCAL manual page, and honestly? it's giving safety era. πŸŽ€ these two are the glow-up versions of the string functions your CS professor warned you about.


πŸ“š LIBRARY

Utility functions from BSD systems β€” that's libbsd, and you link it with -lbsd. ✨


✨ SYNOPSIS ✨

#include <string.h>
(See libbsd(7) for include usage.)

size_t
strlcpy(char *dst, const char *src, size_t size);

size_t
strlcat(char *dst, const char *src, size_t size);
🫢 tiny note from me to you: peep libbsd(7) for the whole include-usage situation, it's the plug.

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so. strlcpy() and strlcat() copy and concatenate strings respectively. that's the whole job. but the way they do it is so much better than the old guard β€” they're designed to be safer, more consistent, and less error-prone replacements for strncpy(3) and strncat(3). no cap. πŸ”₯

here's the difference and it's a big one: unlike those other functions, strlcpy() and strlcat() take the full size of the buffer (not just the length, bestie, the whole size) and they guarantee to NUL-terminate the result. πŸ’…

  • that guarantee holds as long as size is larger than 0
  • or, in the case of strlcat(), as long as there's at least one byte free in dst
😭 and pleaseeee remember: a byte for the NUL should be included in size. that's not optional, that's the vibe.

also note (this part matters): strlcpy() and strlcat() only operate on true β€œC” strings. which means:

  • for strlcpy(), src must be NUL-terminated
  • for strlcat(), both src and dst must be NUL-terminated

strlcpy() β€” copies up to size - 1 characters from the NUL-terminated string src to dst, NUL-terminating the result. πŸŽ€

strlcat() β€” appends the NUL-terminated string src to the end of dst. it will append at most size - strlen(dst) - 1 bytes, NUL-terminating the result. 🌸


⭐ RETURN VALUES ⭐

both functions return the total length of the string they tried to create. yes, tried. here's the breakdown:

  • for strlcpy() that means the length of src
  • for strlcat() that means the initial length of dst plus the length of src

and yeah, it may seem somewhat confusing at first, but it was done to make truncation detection simple. iykyk. 🧚

πŸ’€ however! heads up. if strlcat() traverses size characters without finding a NUL, the length of the string is considered to be size, and the destination string will not be NUL-terminated (since there was no space for the NUL). this keeps strlcat() from running off the end of a string. in practice this should not happen (as it means that either size is incorrect or that dst is not a proper β€œC” string). the check exists to prevent potential security problems in incorrect code. πŸ›‘οΈ

πŸŽ€ EXAMPLES πŸŽ€

the following code fragment illustrates the simple case:

char *s, *p, buf[BUFSIZ];

...

(void)strlcpy(buf, s, sizeof(buf));
(void)strlcat(buf, p, sizeof(buf));

to detect truncation, perhaps while building a pathname, something like the following might be used:

char *dir, *file, pname[MAXPATHLEN];

...

if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
        goto toolong;
if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
        goto toolong;

since it is known how many characters were copied the first time, things can be sped up a bit by using a copy instead of an append:

char *dir, *file, pname[MAXPATHLEN];
size_t n;

...

n = strlcpy(pname, dir, sizeof(pname));
if (n >= sizeof(pname))
        goto toolong;
if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
        goto toolong;
πŸ™ˆ however, one may question the validity of such optimizations, as they defeat the whole purpose of strlcpy() and strlcat(). as a matter of fact, the first version of this manual page got it wrong. iconic behaviour honestly. 😭

πŸ‘€ SEE ALSO πŸ‘€


🌈 HISTORY 🌈

the strlcpy() and strlcat() functions first appeared in OpenBSD 2.4, and made their appearance in NetBSD 1.4.3, FreeBSD 3.3 and glibc 2.38. absolute main character energy, they've been everywhere. πŸ‘‘


and that's the whole page, bestie πŸ’œ this one comes to you from <strong>Debian</strong>, dated <strong>January 7, 2024</strong>, under the <strong>strlcpy(3bsd)</strong> name β€” go forth and never overflow a buffer again. periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from