slibc
Functions | Variables
stdlib.h File Reference

Contains those _s functions that were added to C99's /usr/include/stdlib.h. More...

#include "./base_.h"
#include "./errno.h"
#include "./stddef.h"

Go to the source code of this file.

Functions

constraint_handler_t set_constraint_handler_s (constraint_handler_t handler)
 The set_constraint_handler function sets a user-specified constraint handler.
void abort_handler_s (const char *restrict msg, void *restrict ptr, errno_t error)
 The abort handler gives out an errormessage on stderr before it ends the program with the function abort.
void ignore_handler_s (const char *restrict msg, void *restrict ptr, errno_t error)
 the ignore_handler_s is used to ignore constraint violations it does nothing
errno_t getenv_s (size_t *restrict len, char *restrict value, rsize_t maxsize, const char *restrict name)
 The getenv_s function gets the value of an environment variable.
errno_t qsort_s (void *base, rsize_t nmemb, rsize_t size, int(*compar)(const void *x, const void *y, void *context), void *context)
 This function sorts an array in ascending order.
void * bsearch_s (const void *key, const void *base, rsize_t nmemb, rsize_t size, int(*compar)(const void *, const void *, void *context), void *context)
 This function performs a binary search of a sorted array.

Variables

SLIBC_BEGIN_DECLS typedef void(* constraint_handler_t )(const char *restrict msg, void *restrict ptr, errno_t error)
 Definition of callback-type constraint_handler_t.

Detailed Description

Contains those _s functions that were added to C99's /usr/include/stdlib.h.


Function Documentation

void* bsearch_s ( const void *  key,
const void *  base,
rsize_t  nmemb,
rsize_t  size,
int(*)(const void *, const void *, void *context)  compar,
void *  context 
)

This function performs a binary search of a sorted array.

The array (given by base, nmemb and size) is searched for a member that is the same as the object pointed to by key.

Runtime-constraints:
A runtime-constraint violation occurs if
  • nmemb or size is greater than RSIZE_MAX
  • if nmemb > 0 and (key, base or comparison are null pointers)
Parameters:
[in]keypoints to the object to be matched
[in]basepoints to the start of the array
[in]nmembspecifies the number of elements in the array
[in]sizegives the size of an individual array argument
[in]compara pointer to the comparison function
[in,out]contextmay hold a user-defined value or Null. This value will be supplied as an extra-argument to the comparison function.
Returns:
A pointer to a matching element of the array NULL if no match is found or there is a runtime-constraint violation
See also:
bsearch
errno_t getenv_s ( size_t *restrict  len,
char *restrict  value,
rsize_t  maxsize,
const char *restrict  name 
)

The getenv_s function gets the value of an environment variable.

Developers often make wrong assumptions about the size of environment variables. Since a local attacker can set environment variables, the environment has to be considered evil as every other input. The getenv_s functions forces the programmer to supply a maximum buffer size.

Runtime-constraints:
A runtime-constraint violation occurs if
  • name is a null pointer
  • maxsize is zero or greater than RSIZE_MAX
  • value shall not be a null pointer. In case of a runtime-constriant violation, the a non-zero len is dereferenced and set to zero.
Parameters:
[out]lenIf len is not Null, the integer pointed to by len will be set to the length of the variable's value (one needs a buffer with length + 1 for storing the string)
[out]valuecontains the environment variable's value on success
[in]maxsizethe size of the buffer pointed to by value
[in]namethe name of the environment variable to search for
Returns:
Zero when the enviroment variable was found and successfully stored in the buffer pointed to by value. Non-zero otherwise.
Usage Example:
            char env_value[255];
            getenv_s(NULL, env_value, sizeof(env_value), "PATH");
See also:
getenv
errno_t qsort_s ( void *  base,
rsize_t  nmemb,
rsize_t  size,
int(*)(const void *x, const void *y, void *context)  compar,
void *  context 
)

This function sorts an array in ascending order.

The array is sorted according to the specified comparison function. Besides performing several runtime-checks (see rcs), the only difference is that the comparison function accepts an additional argument for the caller's use.

Runtime-constraints:
A runtime-constraint violation occurs if
  • nmemb or size is greater than RSIZE_MAX
  • if nmemb > 0 and (base or comparison are null pointers)
Parameters:
[in,out]basepoints to the start of the array
[in]nmembspecifies the number of elements in the array
[in]sizegives the size of an individual array argument
[in]compara pointer to the comparison function
[in,out]contextmay hold a user-defined value or Null. This value will be supplied as an extra-argument to the comparison function.
Returns:
zero if there was no runtime-constraint violation. non-zero value otherwise.
See also:
qsort

The set_constraint_handler function sets a user-specified constraint handler.

The constraint handler is called whenever a runtime constraint violation is detected. If, for example, a user calls strcpy_s(NULL, 0, source) the currently set runtime constraint handler will be invoked. The constraint handler is specific to each thread.

If the application does not set its own constraint handler, the default constraint handler is called. In our implementation the default constraint handler is abort_handler_s.

Runtime-constraints:
There are no runtime-constraints.
Parameters:
[in]handlera pointer to the new handler function
Returns:
the previously registered handler
See also:
abort_handler_s, ignore_handler_s

Variable Documentation

SLIBC_BEGIN_DECLS typedef void(* constraint_handler_t)(const char *restrict msg, void *restrict ptr, errno_t error)

Definition of callback-type constraint_handler_t.

All SLIBC-functions invoke the constraint handler in case of a constraint violation.