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

Macros

#define ntz(x)   a_ctz_l((x))
 

Typedefs

typedef unsigned long uint64_t
 
typedef int(* cmpfun) (const void *, const void *, void *)
 The qsort_s function sorts the given array pointed to by base in ascending order. More...
 

Functions

static int a_ctz_64 (uint64_t x)
 
static int a_ctz_l (unsigned long x)
 
static int pntz (size_t p[2])
 
static void cycle (size_t width, unsigned char *ar[], int n)
 
static void shl (size_t p[2], int n)
 
static void shr (size_t p[2], int n)
 
static void sift (unsigned char *head, size_t width, cmpfun cmp, int pshift, size_t lp[], void *ctx)
 
static void trinkle (unsigned char *head, size_t width, cmpfun cmp, size_t pp[2], int pshift, int trusty, size_t lp[], void *ctx)
 
static void qsort_musl (void *base, size_t nel, size_t width, cmpfun cmp, void *ctx)
 
EXPORT errno_t qsort_s (void *base, rsize_t nmemb, rsize_t size, int(*compar)(const void *k, const void *y, void *context), void *context)
 

Variables

static const char debruijn32 [32]
 

Macro Definition Documentation

◆ ntz

#define ntz (   x)    a_ctz_l((x))

Typedef Documentation

◆ uint64_t

typedef unsigned long uint64_t

◆ cmpfun

typedef int(* cmpfun) (const void *, const void *, void *)

The qsort_s function sorts the given array pointed to by base in ascending order.

The array contains nmemb elements of size bytes. Function pointed to by compar is used for object comparison.

Remarks
SPECIFIED IN
  • C11 standard (ISO/IEC 9899:2011): K.3.6.3.2 The qsort_s function (p: 609) http://en.cppreference.com/w/c/algorithm/qsort
  • ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments and system software interfaces, Extensions to the C Library, Part I: Bounds-checking interfaces
Parameters
[in]basepointer to the array to sort
[in]nmembnumber of elements in the array
[in]sizesize of each element in the array in bytes
[in]comparcomparison function which returns a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second and zero if the arguments are equal. key is passed as the first argument, an element from the array as the second. The signature of the comparison function should be equivalent to the following: int cmp(const void *a, const void *b, const void *context); The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.
[in]contextadditional information (e.g., collating sequence), passed to compar as the third argument
Precondition
Neither base nor compar shall not be a null pointer (unless nmemb is zero).
nmemb or size shall not be greater than RSIZE_MAX_MEM.

If comp indicates two elements as equivalent, their order in the resulting sorted array is unspecified.

Note
C11 uses RSIZE_MAX, not RSIZE_MAX_MEM.
Returns
zero on success, non-zero if a runtime constraints violation was detected.
Note
Despite the name, neither C nor POSIX standards require this function to be implemented using quicksort or make any complexity or stability guarantees. Unlike other bounds-checked functions, qsort_s does not treat arrays of zero size as a runtime constraint violation and instead returns successfully without altering the array (the other function that accepts arrays of zero size is bsearch_s). Until qsort_s, users of qsort often used global variables to pass additional context to the comparison function.

This function is available under windows with a different API, no return type, and is not available with safeclib.

Returns
Zero on success, an errno_t on errors.
Return values
EOKwhen operation is successful
-ESNULLPwhen base/compar is NULL pointer and nmemb > 0
-ESLEMAXwhen nmemb/size > RSIZE_MAX_MEM
See also
bsearch_s()

Function Documentation

◆ a_ctz_64()

static int a_ctz_64 ( uint64_t  x)
inlinestatic

◆ a_ctz_l()

static int a_ctz_l ( unsigned long  x)
inlinestatic

◆ pntz()

static int pntz ( size_t  p[2])
inlinestatic

◆ cycle()

static void cycle ( size_t  width,
unsigned char *  ar[],
int  n 
)
static

◆ shl()

static void shl ( size_t  p[2],
int  n 
)
inlinestatic

◆ shr()

static void shr ( size_t  p[2],
int  n 
)
inlinestatic

◆ sift()

static void sift ( unsigned char *  head,
size_t  width,
cmpfun  cmp,
int  pshift,
size_t  lp[],
void *  ctx 
)
static

◆ trinkle()

static void trinkle ( unsigned char *  head,
size_t  width,
cmpfun  cmp,
size_t  pp[2],
int  pshift,
int  trusty,
size_t  lp[],
void *  ctx 
)
static

◆ qsort_musl()

static void qsort_musl ( void *  base,
size_t  nel,
size_t  width,
cmpfun  cmp,
void *  ctx 
)
static

◆ qsort_s()

EXPORT errno_t qsort_s ( void *  base,
rsize_t  nmemb,
rsize_t  size,
int(*)(const void *k, const void *y, void *context)  compar,
void *  context 
)

Variable Documentation

◆ debruijn32

const char debruijn32[32]
static
Initial value:
= {
0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13,
31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14
}