xref: /lighttpd1.4/src/ck.h (revision 0318ef7b)
1 /*
2  * ck - C11 Annex K wrappers  (selected functions; not complete)
3  *
4  * ck is also an abbreviation for "check".
5  * These are validating, checking functions.
6  *
7  * Copyright(c) 2016,2021 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
8  * License: BSD 3-clause (same as lighttpd)
9  */
10 #ifndef INCLUDED_CK_H
11 #define INCLUDED_CK_H
12 #ifndef __STDC_WANT_LIB_EXT1__ /*(enable C11 Annex K ext1 *_s functions)*/
13 #define __STDC_WANT_LIB_EXT1__ 1
14 #endif
15 #if defined(__APPLE__) && defined(__MACH__)
16 #ifndef _DARWIN_C_SOURCE
17 #define _DARWIN_C_SOURCE
18 #endif
19 #endif
20 #include "first.h"
21 #if defined(__FreeBSD__) || defined(__sun)
22 #ifndef _RSIZE_T_DEFINED /* expecting __EXT1_VISIBLE 1 and _RSIZE_T_DEFINED */
23 #define _RSIZE_T_DEFINED
24 typedef size_t rsize_t;
25 #endif
26 #include <errno.h>
27 #endif
28 
29 __BEGIN_DECLS
30 
31 
32 #ifndef RSIZE_MAX
33 #define RSIZE_MAX (SIZE_MAX >> 1)
34 typedef size_t rsize_t;
35 typedef int errno_t;
36 #endif
37 
38 
39 errno_t ck_getenv_s (size_t * restrict len, char * restrict value, rsize_t maxsize, const char * restrict name);
40 
41 /*(ck_memclear_s() is not from C11 Annex K
42  * ck_memclear_s() is similar to memset_s() using constant byte 0 for fill)*/
43 errno_t ck_memclear_s (void *s, rsize_t smax, rsize_t n);
44 
45 /*(ck_memzero() is not from C11 Annex K
46  * ck_memzero() is a convenience wrapper around ck_memclear_s())*/
47 static inline errno_t ck_memzero(void *s, rsize_t n);
ck_memzero(void * s,rsize_t n)48 static inline errno_t ck_memzero(void *s, rsize_t n) {
49     return ck_memclear_s(s, n, n);
50 }
51 
52 errno_t ck_strerror_s (char *s, rsize_t maxsize, errno_t errnum);
53 
54 /*(ck_memeq_const_time() is not from C11 Annex K)
55  * constant time memory compare for equality
56  * rounds to next multiple of 64 to avoid potentially leaking exact
57  * string lengths when subject to high precision timing attacks */
58 __attribute_nonnull__()
59 int ck_memeq_const_time (const void *a, size_t alen, const void *b, size_t blen);
60 
61 /*(ck_memeq_const_time_fixed_len() is not from C11 Annex K)
62  * constant time memory compare for equality for fixed len (e.g. digests)
63  * (padding not necessary for digests, which have fixed, defined lengths) */
64 __attribute_nonnull__()
65 int ck_memeq_const_time_fixed_len (const void *a, const void *b, size_t len);
66 
67 
68 /*(ck_malloc() is not from C11 Annex K)
69  * ck_malloc() performs malloc() on args and aborts if malloc() fails */
70 __attribute_malloc__
71 __attribute_returns_nonnull__
72 void * ck_malloc (size_t nbytes);
73 
74 /*(ck_calloc() is not from C11 Annex K)
75  * ck_calloc() performs calloc() on args and aborts if calloc() fails */
76 __attribute_malloc__
77 __attribute_returns_nonnull__
78 void * ck_calloc (size_t nmemb, size_t elt_sz);
79 
80 /*(ck_realloc_u32() is not from C11 Annex K)
81  * ck_realloc_u32() performs realloc() on *list or aborts
82  * extends *list with n used elements by x elements of elt_sz
83  * and ensures n + x <= UINT32_MAX */
84 __attribute_nonnull__()
85 __attribute_returns_nonnull__
86 void * ck_realloc_u32 (void **list, size_t n, size_t x, size_t elt_sz);
87 
88 
89 /*(ck_bt() is not from C11 Annex K)
90  * ck_bt() prints backtrace to stderr */
91 __attribute_cold__
92 __attribute_nonnull__()
93 void ck_bt(const char *filename, unsigned int line, const char *msg);
94 
95 /*(ck_bt_abort() is not from C11 Annex K)
96  * ck_bt_abort() prints backtrace to stderr and calls abort() */
97 __attribute_cold__
98 __attribute_nonnull__()
99 __attribute_noreturn__
100 void ck_bt_abort(const char *filename, unsigned int line, const char *msg);
101 
102 /*(ck_assert() and ck_assert_failed() are not from C11 Annex K)
103  * ck_assert() executes a runtime assertion test or aborts
104  * ck_assert() *is not* optimized away if defined(NDEBUG)
105  * (unlike standard assert(), which *is* optimized away if defined(NDEBUG)) */
106 __attribute_cold__
107 __attribute_nonnull__()
108 __attribute_noreturn__
109 void ck_assert_failed(const char *filename, unsigned int line, const char *msg);
110 
111 #define ck_assert(x) \
112         do { if (!(x)) ck_assert_failed(__FILE__, __LINE__, #x); } while (0)
113 
114 
115 __END_DECLS
116 
117 
118 #endif
119