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

Functions

EXPORT int snprintf_s (char *restrict dest, rsize_t dmax, const char *restrict fmt,...)
 The truncating snprintf_s function composes a string with same test that would be printed if format was used on printf. More...
 

Function Documentation

◆ snprintf_s()

EXPORT int snprintf_s ( char *restrict  dest,
rsize_t  dmax,
const char *restrict  fmt,
  ... 
)

The truncating snprintf_s function composes a string with same test that would be printed if format was used on printf.

Instead of being printed, the content is stored in dest. More than dmax - 1 characters might be written, so this variant is unsafe! Always use sprintf_s instead. The resulting character string will be terminated with a null character, unless dmax is zero. If dmax is zero, nothing is written and dest may be a null pointer, however the return value (number of bytes that would be written) is still calculated and returned. Warning: Unlike the safe variant sprintf_s, snprintf_s does not guarantee that the buffer will be null-terminated unless the buffer size is zero.

Note
POSIX specifies that errno is set on error. However, the safeclib extended ES* errors do not set errno, only when the underlying system vsnprintf call fails, errno is set.
Remarks
SPECIFIED IN
Parameters
[out]destpointer to string that will be written into.
[in]dmaxrestricted maximum length of dest
[in]fmtformat-control string.
[in]...optional arguments
Precondition
Neither dest nor fmt shall be a null pointer.
dmax shall not be greater than RSIZE_MAX_STR.
dmax shall not equal zero.
dmax shall be greater than strnlen_s(dest, dmax).
fmt shall not contain the conversion specifier n.
None of the arguments corresponding to s is a null pointer. (not yet)
No encoding error shall occur.
Returns
Number of characters not including the terminating null character (which is always written as long as buffer is not a null pointer and dmax is not zero and not greater than RSIZE_MAX_STR), which would have been written to dest if dmax was ignored, or a negative value if a runtime constraints violation or an encoding error occurred.
Return values
-ESNULLPwhen dest/fmt is NULL pointer
-ESZEROLwhen dmax = 0
-ESLEMAXwhen dmax > RSIZE_MAX_STR
-EINVALwhen fmt contains n
See also
sprintf_s(), vsnprintf_s()