Jump to content

strlcpy

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Black Walnut (talk | contribs) at 04:29, 20 November 2010 (Usage). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.


In computer programming, the strlcpy function is intended to replace the function strcpy and provide a simpler and more robust and secure interface than strncpy. It is designed to copy the contents of a string from a source string to a destination string. It is almost always accompanied by the strlcat function which provides a similar alternative to strncat.

These are not C standard library functions, but are available in several Unix operating systems, including BSD, Mac OS X, Solaris and IRIX, with notable exception of Linux.

Usage

   size_t strlcpy(char *destination, const char *source, size_t size);
   size_t strlcat(char *destination, const char *source, size_t size);

strlcpy offers two features that are designed to help software developers avoid problems. Like strncpy, the function takes the destination's size as a parameter, avoiding buffer overflows if the size parameter is correct. If this size is greater than zero, a NUL byte is always written to the destination, so the resulting string is always NUL-terminated (even if the source string was truncated to fit). For performance reasons, strlcpy, unlike strncpy, does not fill with zeros any unused space in the destination buffer.[1] Additionally, strlcpy counts and returns the length of the entire source string. This can be compared to the destination buffer's size, to check for truncation. For example:

   if (strlcpy(dest, source, dest_len) >= dest_len)
       errx(1, "String too long");

strlcat is equivalent to doing strcat into a buffer large enough to hold the result and then doing a strlcpy of that buffer to the destination.

History

strlcpy and strlcat were developed by Todd C. Miller and Theo de Raadt and first implemented in OpenBSD version 2.4. It has subsequently been adopted by a number of operating systems including FreeBSD (from version 3.3), Solaris and Mac OS X. Many application packages and libraries include their own copies of these functions, including glib, rsync and the Linux kernel itself.

Criticism

GNU C Library maintainer Ulrich Drepper is among the critics of the strlcpy and strlcat functions;[2] consequently these functions have not been added to glibc. Drepper argues that strlcpy and strlcat make truncation errors easier for a programmer to ignore and thus can introduce more bugs than they remove.[2] His concern with possible truncation when using any string function involving static allocation is shared by others.[3]

Other criticism is that the functions are non-standard, and there are implementation differences between the BSD and Solaris implementations (the return value of strlcat when there is no nul in the destination buffer differs).[4]

References

  1. ^ Todd C. Miller (1999). "strlcpy and strlcat - consistent, safe, string copy and concatenation". USENIX '99. {{cite web}}: Unknown parameter |coauthors= ignored (|author= suggested) (help)
  2. ^ a b libc-alpha mailing list, selected messages from 8 August 2000 thread: 53, 60, 61
  3. ^ Antill, James. Security with string APIs: Security relevant things to look for in a string library API
  4. ^ Antill, James. Security with string APIs