Botan 3.14.1
Crypto and TLS for C&
assert.h
Go to the documentation of this file.
1/*
2* Runtime assertion checking
3* (C) 2010,2018 Jack Lloyd
4* 2017 Simon Warta (Kullo GmbH)
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_ASSERTION_CHECKING_H_
10#define BOTAN_ASSERTION_CHECKING_H_
11
12#include <botan/api.h>
13
15
16namespace Botan {
17
18// NOLINTBEGIN(*-macro-usage)
19
20/**
21* Called when an assertion fails
22* Throws an Exception object
23*/
24[[noreturn]] void BOTAN_PUBLIC_API(2, 0)
25 assertion_failure(const char* expr_str, const char* assertion_made, const char* func, const char* file, int line);
26
27/**
28* Called when an invalid argument is used
29* Throws Invalid_Argument
30*/
31[[noreturn]] void BOTAN_UNSTABLE_API throw_invalid_argument(const char* message, const char* func, const char* file);
32
33#define BOTAN_ARG_CHECK(expr, msg) \
34 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
35 do { \
36 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
37 if(!(expr)) { \
38 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
39 Botan::throw_invalid_argument(msg, __func__, __FILE__); \
40 } \
41 } while(0)
42
43/**
44* Called when an invalid state is encountered
45* Throws Invalid_State
46*/
47[[noreturn]] void BOTAN_UNSTABLE_API throw_invalid_state(const char* message, const char* func, const char* file);
48
49#define BOTAN_STATE_CHECK(expr) \
50 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
51 do { \
52 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
53 if(!(expr)) { \
54 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
55 Botan::throw_invalid_state(#expr, __func__, __FILE__); \
56 } \
57 } while(0)
58
59#define BOTAN_STATE_CHECK_MSG(expr, msg) \
60 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
61 do { \
62 if(!(expr)) \
63 Botan::throw_invalid_state(msg, __func__, __FILE__); \
64 } while(0)
65
66/**
67* Make an assertion
68*/
69#define BOTAN_ASSERT(expr, assertion_made) \
70 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
71 do { \
72 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
73 if(!(expr)) { \
74 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
75 Botan::assertion_failure(#expr, assertion_made, __func__, __FILE__, __LINE__); \
76 } \
77 } while(0)
78
79/**
80* Make an assertion
81*/
82#define BOTAN_ASSERT_NOMSG(expr) \
83 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
84 do { \
85 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
86 if(!(expr)) { \
87 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
88 Botan::assertion_failure(#expr, "", __func__, __FILE__, __LINE__); \
89 } \
90 } while(0)
91
92/**
93* Assert that value1 == value2
94*/
95#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made) \
96 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
97 do { \
98 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
99 if((expr1) != (expr2)) { \
100 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
101 Botan::assertion_failure(#expr1 " == " #expr2, assertion_made, __func__, __FILE__, __LINE__); \
102 } \
103 } while(0)
104
105/**
106* Assert that expr1 (if true) implies expr2 is also true
107*/
108#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg) \
109 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
110 do { \
111 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
112 if((expr1) && !(expr2)) { \
113 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
114 Botan::assertion_failure(#expr1 " implies " #expr2, msg, __func__, __FILE__, __LINE__); \
115 } \
116 } while(0)
117
118/**
119* Assert that a pointer is not null
120*/
121#define BOTAN_ASSERT_NONNULL(ptr) \
122 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
123 do { \
124 if((ptr) == nullptr) { \
125 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
126 Botan::assertion_failure(#ptr " is not null", "", __func__, __FILE__, __LINE__); \
127 } \
128 } while(0)
129
130#if defined(BOTAN_ENABLE_DEBUG_ASSERTS)
131
132 #define BOTAN_DEBUG_ASSERT(expr) BOTAN_ASSERT_NOMSG(expr)
133
134#else
135
136 #define BOTAN_DEBUG_ASSERT(expr) \
137 do { /* NOLINT(*-avoid-do-while) */ \
138 } while(0)
139
140#endif
141
142/**
143* Mark variable as unused.
144*
145* Takes any number of arguments and marks all as unused, for instance
146* BOTAN_UNUSED(a); or BOTAN_UNUSED(x, y, z);
147*/
148template <typename... T>
149constexpr void ignore_params([[maybe_unused]] const T&... args) {}
150
151#define BOTAN_UNUSED Botan::ignore_params
152
153/**
154* Define Botan::assert_unreachable and BOTAN_ASSERT_UNREACHABLE
155*
156* This is intended to be used in the same situations as `std::unreachable()`;
157* a codepath that (should not) be reachable but where the compiler cannot
158* tell that it is unreachable.
159*
160* Unlike `std::unreachable()`, or equivalent compiler builtins like GCC's
161* `__builtin_unreachable`, this function is not UB. By default it will
162* throw an exception. If `BOTAN_TERMINATE_ON_ASSERTS` is defined, it will
163* instead print a message to stderr and abort.
164*
165* Due to this difference, and the fact that it is not inlined, calling
166* this is significantly more costly than using `std::unreachable`.
167*
168* @param file the source file the call occurred in
169* @param line the source line the call occurred on
170*/
171[[noreturn]] void BOTAN_UNSTABLE_API assert_unreachable(const char* file, int line);
172
173#define BOTAN_ASSERT_UNREACHABLE() Botan::assert_unreachable(__FILE__, __LINE__)
174
175// NOLINTEND(*-macro-usage)
176
177} // namespace Botan
178
179#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition api.h:104
#define BOTAN_UNSTABLE_API
Definition api.h:34
constexpr void ignore_params(const T &... args)
Definition assert.h:149