safec  3.2
Safe C Library - ISO TR24731 Bounds Checking Interface
strncpy_s.c File Reference
#include "safe_str_lib.h"
+ Include dependency graph for strncpy_s.c:

Functions

EXPORT errno_t strncpy_s (char *restrict dest, rsize_t dmax, const char *restrict src, rsize_t slen)
 The strncpy_s function copies not more than slen successive characters (characters that follow a null character are not copied) from the array pointed to by src to the array pointed to by dest. More...
 

Function Documentation

◆ strncpy_s()

EXPORT errno_t strncpy_s ( char *restrict  dest,
rsize_t  dmax,
const char *restrict  src,
rsize_t  slen 
)

The strncpy_s function copies not more than slen successive characters (characters that follow a null character are not copied) from the array pointed to by src to the array pointed to by dest.

If no null character was copied from src, then dest[n] is set to a null character.

All elements following the terminating null character (if any) written by strncpy_s in the array of dmax characters pointed to by dest take unspeciļ¬ed values when strncpy_s returns. With SAFECLIB_STR_NULL_SLACK defined the rest is cleared with NULL bytes.

Remarks
SPECIFIED IN
  • C11 standard (ISO/IEC 9899:2011): K.3.7.1.4 The strncpy_s function (p: 616-617) http://en.cppreference.com/w/c/string/byte/strncpy
  • ISO/IEC TR 24731-1, Programming languages, environments and system software interfaces, Extensions to the C Library, Part I: Bounds-checking interfaces
Parameters
[out]destpointer to string that will be replaced by src.
[in]dmaxrestricted maximum length of dest
[in]srcpointer to the string that will be copied to dest
[in]slenthe maximum number of characters to copy from src
Precondition
dmax shall not be zero.
Neither dmax nor slen shall be greater than RSIZE_MAX_STR.
If slen is either greater than or equal to dmax, then dmax should be more than strnlen_s(src,dmax) to avoid truncation.
Copying shall not take place between objects that overlap.
Note
C11 uses RSIZE_MAX, not RSIZE_MAX_STR.
Returns
If there is a runtime-constraint violation, then if dest is not a null pointer and dmax greater than RSIZE_MAX_STR, then strncpy_s nulls dest.
Return values
EOKsuccessful operation, the characters in src were copied to dest and the result is null terminated.
ESNULLPwhen dest/src is NULL pointer
ESZEROLwhen dmax = 0. Before C11 also with slen = 0
ESLEMAXwhen dmax/slen > RSIZE_MAX_STR
ESOVRLPwhen strings overlap
ESNOSPCwhen dest < src
See also
strcat_s(), strncat_s(), strcpy_s(), wcsncpy_s()