1

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

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Patrick Resal
  • 115
  • 2
  • 11
  • alright, I understand that is useful ! – Patrick Resal Aug 17 '18 at 20:48
  • **What** are `strlcpy` and `strlcat`? The question is tagged C. Are they functions you wrote in code we cannot see? – Weather Vane Aug 17 '18 at 21:11
  • 2
    @WeatherVane: come on, a simple search for these function names locates everything there is to know about them. The question is tagged `c`, not `std-c`. – chqrlie Aug 17 '18 at 22:28
  • This was in the Close Vote queue for being too broad. I don't think it is too broad and it is not a bad question. I think it is an interesting question because the BSD functions were some of the earliest attempts to provider safer string functions. With that said I imagine there's a duplicate somewhere that explains the "consistent interface" which includes a usable return value so developers can detect truncations and errors. Unfortunately I have not been able to locate a similar question. – jww Aug 20 '18 at 05:39
  • @PatrickResal: you can accept one of the answers by clicking on the grey checkmark below its score. – chqrlie Apr 13 '19 at 12:11

3 Answers3

6

The aim of these functions is to move strings (in some way), and a very frequent question by the programmer when you do that is "how many chars were moved?".

Since it already knows this information, it is no more work for it to return this information. Otherwise the programmer would have to do some expensive strlen()'s before and/or after.

Also, if they fail due to a too small buffer, they give you the number of characters they would need, so you can detect the truncation, and potentially reallocate the buffer.

Max
  • 10,701
  • 2
  • 24
  • 48
3

strlcpy and strlcat are non standard functions available on some BSD versions of Unix that perform a safe version of strcpy and strcat with truncation. They do not actually return an unsigned int, but a size_t, which is the type returned by sizeof and may be different from unsigned int.

The functions are declared in <string.h> with these prototypes:

size_t strlcpy(char *dst, const char *src, size_t size);
size_t strlcat(char *dst, const char *src, size_t size);
  • dst is a pointer to the destination array. This array must contain a valid C string for strlcat, it can be NULL if size is 0.
  • src must be a valid pointer to a C string.
  • size is the size in bytes of the destination array.

The functions perform a string copy or concatenation, similar to strcpy and strcat, but do not write beyond the end of the destination array (size). They null terminate the destination array unless size is 0 or dst points to an array without a null terminator in the first size bytes for strlcat.

Both functions return the length in bytes of the resulting string if the destination array was long enough. This permits easy detection of truncation.

Here is an example:

char dest[10];
size_t len = strlcpy(dest, "This is a random string", sizeof dest);
if (len >= sizeof dest) {
    /* Truncation occurred. You could ignore it, issue a diagnostic,
       or reallocate the destination array to at least `len+1` bytes */
    printf("Truncation occurred\n");
}

So here is the answer to your question: the return value is useful if the programmer wants to detect truncation, otherwise it may be safely ignored.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
2

Synopsis of strlcpy() and strlcat()

#include <string.h>
size_t
strlcpy(char *dst, const char *src, size_t size);

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

The strlcpy() and strlcat() functions return the total length of the string they tried to create. For strlcpy() that means the length of src. For strlcat() that means the initial length of dst plus the length of src. While this may seem somewhat confusing it was done to make truncation detection simple.

Note however, that 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.

LocalHost
  • 910
  • 3
  • 8
  • 24