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

Functions

size_t strnlen (const char *, size_t)
 
EXPORT char * gets_s (char *restrict dest, rsize_t dmax)
 The gets_s function reads characters from stdin until a newline is found or end-of-file occurs. More...
 

Function Documentation

◆ strnlen()

size_t strnlen ( const char *  ,
size_t   
)

◆ gets_s()

EXPORT char* gets_s ( char *restrict  dest,
rsize_t  dmax 
)

The gets_s function reads characters from stdin until a newline is found or end-of-file occurs.

Writes only at most dmax characters into the array pointed to by str, and always writes the terminating null character.

Note that C11 allows only writing dmax-1 character. We need to work with the system fgets() passing it dmax+1. In any case, gets_s first finishes reading and discarding the characters from stdin until new-line character, end-of-file condition, or read error before calling the constraint handler. With SAFECLIB_STR_NULL_SLACK the rest of dmax is cleared with NULL bytes, without all elements following the terminating null character (if any) written by gets_s in the array of dmax characters pointed to by dest take unspeciļ¬ed values when gets_s returns.

Remarks
SPECIFIED IN
  • C11 standard (ISO/IEC 9899:2011): K.3.5.4.1 The gets_s function (p: 602-603) http://en.cppreference.com/w/c/io/gets
  • ISO/IEC TR 24731, Programming languages, environments and system software interfaces, Extensions to the C Library, Part I: Bounds-checking interfaces
Parameters
[out]destcharacter string to be written. If the resulting concatenated string is less than dmax, the remaining slack space is nulled.
[in]dmaxrestricted maximum length of the resulting dest, including the null. it may temp. write dmax+1, but always return max dmax.
Precondition
dest shall not be a null pointer
dmax shall not equal zero
dmax shall not be greater than RSIZE_MAX_STR
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 is greater than zero and not greater than RSIZE_MAX_STR, then gets_s nulls dest.
Return values
>0when successful operation, all the characters from src were appended to dest and the result in dest is null terminated.
0+ errno=ESNULLP when dest is a NULL pointer
0+ errno=ESZEROL when dmax = 0
0+ errno=ESLEMAX when dmax > RSIZE_MAX_STR
0+ errno=ESUNTERM endline or eof not encountered after storing dmax-1 characters to dest.
See also
scanf_s()