strncpy, strncpy_s
From cppreference.com
Defined in header <string.h>
|
||
(1) | ||
char *strncpy( char *dest, const char *src, size_t count ); |
(until C99) | |
char *strncpy( char *restrict dest, const char *restrict src, size_t count ); |
(since C99) | |
errno_t strncpy_s(char *restrict dest, rsize_t destsz, const char *restrict src, rsize_t count); |
(2) | (since C11) |
1) Copies at most
count
characters of the character array pointed to by src
(including the terminating null character, but not any of the characters that follow the null character) to character array pointed to by dest
. If
count
is reached before the entire array src
was copied, the resulting character array is not null-terminated. If, after copying the terminating null character from
src
, count
is not reached, additional null characters are written to dest
until the total of count
characters have been written. The behavior is undefined if the character arrays overlap, if either
dest
or src
is not a pointer to a character array (including if dest
or src
is a null pointer), if the size of the array pointed to by dest
is less than count
, or if the size of the array pointed to by src
is less than count
and it does not contain a null character.2) Same as (1), except that the function does not continue writing zeroes into the destination array to pad up to
count
, it stops after writing the terminating null character (if there was no null in the source, it writes one at dest[count] and then stops). Also, the following errors are detected at runtime and call the currently installed constraint handler function:
-
src
ordest
is a null pointer -
destsz
orcount
is zero or greater than RSIZE_MAX -
count
is greater or equaldestsz
, butdestsz
is less or equal strnlen_s(src, count), in other words, truncation would occur - overlap would occur between the source and the destination strings
-
The behavior is undefined if the size of the character array pointed to by
dest
< strnlen_s(src, destsz) <= destsz
; in other words, an erroneous value of destsz
does not expose the impending buffer overflow. The behavior is undefined if the size of the character array pointed to by src
< strnlen_s(src, count) < destsz
; in other words, an erroneous value of count
does not expose the impending buffer overflow.- As all bounds-checked functions,
strncpy_s
is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before includingstring.h
.
Parameters
dest | - | pointer to the character array to copy to |
src | - | pointer to the character array to copy from |
count | - | maximum number of characters to copy |
destsz | - | the size of the destination buffer |
Return value
1) returns a copy of
dest
2) returns zero on success, returns non-zero on error. Also, on error, writes zero to dest[0] (unless
dest
is a null pointer or destsz
is zero or greater than RSIZE_MAX) and may clobber the rest of the destination array with unspecified values.Notes
As corrected by the post-C11 DR 468, strncpy_s
, unlike strcpy_s, is only allowed to clobber the remainder of the destination array if an error occurs.
Unlike strncpy
, strncpy_s
does not pad the destination array with zeroes, This is a common source of errors when converting existing code to the bounds-checked version.
Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation for strncpy_s
, it is possible to get the truncating behavior by specifying count
equal to the size of the destination array minus one: it will copy the first count
bytes and append the null terminator as always: strncpy_s(dst, sizeof dst, src, (sizeof dst)-1);