Questions tagged [strlcpy]

Non-standard C function originating in OpenBSD: `strlcpy` copies null-terminated string to non-overlapping buffer of size `n`. Returns source length `s` (excluding terminating \0). Unless `n` == 0, the destination string is always null-terminated. If `s` + 1 > `n`, the destination copy is truncated. Use this tag for questions about the function's usage, implementation and relation to other string functions, such as `strcpy` or `strncpy`.

strlcpy is a popular strcpy replacement originating in OpenBSD, where it is available in the <string.h> standard header. On other systems, it is available through libbsd. Many projects have their custom implementation of strlcpy, including the Linux kernel.

Unlike strncpy, it always terminates the string, and if the buffer is longer than needed, it does not waste time overwriting the excess space with zeros.

Wikipedia says:

The most popular [according to GitHub search] replacement [of strcpy and strcat] are the strlcat and strlcpy functions, which appeared in OpenBSD 2.4 in December, 1998. These functions always write one NUL to the destination buffer, truncating the result if necessary, and return the size of buffer that would be needed, which allows detection of the truncation and provides a size for creating a new buffer that will not truncate. They have been criticized on the basis of allegedly being inefficient and encouraging the use of C strings (instead of some superior alternative form of string). Consequently, they have not been included in the GNU C library (used by software on Linux), although they are implemented in the C libraries for OpenBSD, FreeBSD, NetBSD, Solaris, OS X, and QNX, as well as in alternative C libraries for Linux, such as musl. The lack of GNU C library support has not stopped various software authors from using it and bundling a replacement, among other SDL, GLib, ffmpeg, rsync, and even internally in the Linux kernel. Open source implementations for these functions are available.

Related tags

Sources

7 questions
87
votes
8 answers

Why are strlcpy and strlcat considered insecure?

I understand that strlcpy and strlcat were designed as secure replacements for strncpy and strncat. However, some people are still of the opinion that they are insecure, and simply cause a different type of problem. Can someone give an example of…
Anonymous
  • 871
  • 1
  • 8
  • 4
3
votes
2 answers

What defines strscpy in C?

From this video on youtube by Kees Cook from linux.conf.au 2019 he mentions strscpy as being preferred but generally doing what users want (less NUL-padding). However, he doesn't say what defines this (spec or header), Slide from the video, I…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
2
votes
3 answers

strlcpy: source and destination points to the same object

I am just beginning to understand strlcpy. size_t strlcpy(char *destination, const char *source, size_t size); My hypothetical question is: What if the destination and source point to the same object? Example: char destination[100]; const…
1
vote
3 answers

Why does this implementation of 'strncpy' work?

I have to rewrite for an assignment a function that mimics the behavior of the strncpy, after many trials and error, some external help, here's the final code : 15 char *ft_strncpy(char *dest, char *src, unsigned int n) 16 { 17 unsigned…
Jib
  • 45
  • 1
  • 5
1
vote
3 answers

Why unsigned int and not a void for strlcat and strlcpy?

I do not understand why do we have to return an unsigned int for strlcat and strlcpy, why do we need that ? it's not the aim of the function. Thanks for your responses
Patrick Resal
  • 115
  • 2
  • 11
0
votes
2 answers

What is the purpose of strlcpy and what was in the first version of it's manual page?

By reading the man of strlcpy I met example: 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 =…
Andrei
  • 55
  • 6
0
votes
2 answers

problem on My own implemntation of strlcpy?

Hello i just faced a problem when testing an implementation of strlcpy long unsigned int ft_strlcpy(char *dst, const char *src, unsigned int len) { unsigned int l; unsigned int i; i = 0; while (i < len - 1) { if (*src != '\0') …