wcslcat(), wcslcpy

size-bounded wide string copying and concatenation 

Function


SYNOPSIS

#include <wchar.h>

wchar_t *wcslcat(wchar_t *dest, const wchar_t *src, size_t size);

wchar_t *wcslcpy(wchar_t *dest, const wchar_t *src, size_t size);


DESCRIPTION

The wcslcpy() and wcslcat() functions copy and concatenate wide strings respectively. They are designed to be safer, more consistent, and less error prone replacements for wcsncpy() and wcsncat(). Unlike those functions, wcslcpy() and wcslcat() take the full size of the buffer (not just the length) and guarantee to NUL-terminate the result (as long as size is larger than 0 or, in the case of wcslcat(), as long as there is at least one byte free in dst). Note that a byte for the NUL should be included in size. Also note that wcslcpy() and wcslcat() only operate on true "C" wide strings. This means that for wcslcpy() src must be NUL-terminated and for wcslcat() both src and dst must be NUL-terminated.

The wcslcpy() function copies up to size - 1 characters from the NUL-terminated wcsing src to dst, NUL-terminating the result.

The wcslcat() function appends the NUL-terminated wcsing src to the end of dst. It will append at most size - wcslen(dst) - 1 bytes, NUL-terminating the result.

The source and destination wide strings should not overlap, as the behavior is undefined.


PARAMETERS

dst 

Is the null-terminated wcsing to be modified or appended.

src 

Is the null-terminated wcsing to be copied.

size 

Nothing will be written beyond dst+size-1.


RETURN VALUES

The wcslcpy() and wcslcat() functions return the total length of the wcsing they tried to create. For wcslcpy() that means the length of src. For wcslcat() 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 wcslcat() traverses size characters without finding a NUL, the length of the wcsing is considered to be size and the destination wcsing will not be NUL-terminated (since there was no space for the NUL). This keeps wcslcat() from running off the end of a wcsing. In practice this should not happen (as it means that either size is incorrect or that dst is not a proper "C" wcsing). The check exists to prevent potential security problems in incorrect code.


EXAMPLES

The following code fragment illuwcsates the simple case:

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

           ...

           (void)wcslcpy(buf, s, __arraycount(buf));
           (void)wcslcat(buf, p, __arraycount(buf));

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

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

           ...

           if (wcslcpy(pname, dir, __arraycount(pname)) >= __arraycount(pname))
                   goto toolong;
           if (wcslcat(pname, file, __arraycount(pname)) >= __arraycount(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

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

           ...

           n = wcslcpy(pname, dir, __arraycount(pname));
           if (n >= __arraycount(pname))
                   goto toolong;
           if (wcslcpy(pname + n, file, __arraycount(pname) - n) >= __arraycount(pname) - n)
                   goto toolong;

However, one may question the validity of such optimizations, as they defeat the whole purpose of wcslcpy() and wcslcat().


MULTITHREAD SAFETY LEVEL

MT-Safe.


PORTING ISSUES

None.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
snwprintf(), wcscat(), wcscpy(), wcsncat(), wcsncpy()


PTC MKS Toolkit 10.3 Documentation Build 39.