strlcpy and strlcat are non-standard functions created as a replacement for frequently used incorrectly analogs of the standard library of the C programming language to copy the contents of a null-terminated string into a buffer of limited size with protection against buffer overflows .
strlcpy - safe replacement of the standard strcpy function; strlcat is a safe replacement for the standard strcat function.
strlcpy and strlcat have appeared and are supported in OpenBSD . Later they began to be used in some other operating systems. On systems that do not support strlcpy / strlcat , their artisanal implementation is possible, for example, from source code distributed under the BSD license .
Content
Description
The prototypes described in the header file string.h :
size_t strlcpy (char *dst, const char *src, size_t size);size_t strlcat (char *dst, const char *src, size_t size);
dstis a pointer to the destination buffer.srcis a pointer to the source string.size- the size of the destination buffer.
The strlcpy function copies no more than size - 1 characters from the src string to the dst buffer and is guaranteed to set the character to zero at the end of the string. strlcat does the same, however, copying is not to the beginning of dst , but in such a way as to continue the line pointed to by dst . In the case where dst points to a null character , the behavior of the functions is equivalent.
Return Value
strlcpy returns the size of the string at src . strlcat returns the total length of strings at src and dst addresses. The return value does not depend on whether it was possible to copy the string completely or not; this makes it easy to determine that the destination buffer is too small for the row to be copied.
Usage Example
#include <string.h>
#include <stdio.h> / * for printf () * /
char buf [ 10 ]; // buffer smaller than the string
int main ( void )
{ const char * str = "string pattern" ; // immutable array
size_t sz = 0 ;
buf [ 9 ] = '\ 0' ; // redundant initialization for debug printing
printf ( "string: \" % s \ "\ n \ n " , str );
printf ( "buffer before copying: \" % s \ "\ n " , buf );
sz = strlcpy ( buf , str , sizeof ( buf ));
if ( sz > = sizeof ( buf )) // example of determining line truncation
printf ( "truncation of a string from% zu to% lu characters! \ n " , sz , sizeof ( buf ) - 1 ) was detected ;
printf ( "buffer after copying: \" % s \ "\ n " , buf );
return 0 ;
}
Conclusion:
string: "sample string" buffer before copying: "" truncation of a string from 14 to 9 characters was detected! buffer after copying: "sample with"
(the line was truncated when copying to the size of the buffer - 9 characters + zero)
Links
- strlcpy and strlcat - consistent, safe, string copy and concatenation - Presentation of
strlcpyandstrlcatat the 1999 USENIX conference. - strlcpy (1) (English) - A guide to the
strlcpyandstrlcatfunctions on the OpenBSD website - strlcpy.c (English) - The source code for the
strlcpyfunction in the OpenBSD CVS repository.