Botan 3.9.0
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/**
60* Make an assertion
61*/
62#define BOTAN_ASSERT(expr, assertion_made) \
63 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
64 do { \
65 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
66 if(!(expr)) { \
67 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
68 Botan::assertion_failure(#expr, assertion_made, __func__, __FILE__, __LINE__); \
69 } \
70 } while(0)
71
72/**
73* Make an assertion
74*/
75#define BOTAN_ASSERT_NOMSG(expr) \
76 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
77 do { \
78 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
79 if(!(expr)) { \
80 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
81 Botan::assertion_failure(#expr, "", __func__, __FILE__, __LINE__); \
82 } \
83 } while(0)
84
85/**
86* Assert that value1 == value2
87*/
88#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made) \
89 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
90 do { \
91 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
92 if((expr1) != (expr2)) { \
93 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
94 Botan::assertion_failure(#expr1 " == " #expr2, assertion_made, __func__, __FILE__, __LINE__); \
95 } \
96 } while(0)
97
98/**
99* Assert that expr1 (if true) implies expr2 is also true
100*/
101#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg) \
102 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
103 do { \
104 /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \
105 if((expr1) && !(expr2)) { \
106 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
107 Botan::assertion_failure(#expr1 " implies " #expr2, msg, __func__, __FILE__, __LINE__); \
108 } \
109 } while(0)
110
111/**
112* Assert that a pointer is not null
113*/
114#define BOTAN_ASSERT_NONNULL(ptr) \
115 /* NOLINTNEXTLINE(*-avoid-do-while) */ \
116 do { \
117 if((ptr) == nullptr) { \
118 /* NOLINTNEXTLINE(bugprone-lambda-function-name) */ \
119 Botan::assertion_failure(#ptr " is not null", "", __func__, __FILE__, __LINE__); \
120 } \
121 } while(0)
122
123#if defined(BOTAN_ENABLE_DEBUG_ASSERTS)
124
125 #define BOTAN_DEBUG_ASSERT(expr) BOTAN_ASSERT_NOMSG(expr)
126
127#else
128
129 #define BOTAN_DEBUG_ASSERT(expr) \
130 do { /* NOLINT(*-avoid-do-while) */ \
131 } while(0)
132
133#endif
134
135/**
136* Mark variable as unused.
137*
138* Takes any number of arguments and marks all as unused, for instance
139* BOTAN_UNUSED(a); or BOTAN_UNUSED(x, y, z);
140*/
141template <typename... T>
142constexpr void ignore_params([[maybe_unused]] const T&... args) {}
143
144#define BOTAN_UNUSED Botan::ignore_params
145
146/*
147* Define Botan::assert_unreachable and BOTAN_ASSERT_UNREACHABLE
148*
149* This is intended to be used in the same situations as `std::unreachable()`;
150* a codepath that (should not) be reachable but where the compiler cannot
151* tell that it is unreachable.
152*
153* Unlike `std::unreachable()`, or equivalent compiler builtins like GCC's
154* `__builtin_unreachable`, this function is not UB. By default it will
155* throw an exception. If `BOTAN_TERMINATE_ON_ASSERTS` is defined, it will
156* instead print a message to stderr and abort.
157*
158* Due to this difference, and the fact that it is not inlined, calling
159* this is significantly more costly than using `std::unreachable`.
160*/
161[[noreturn]] void BOTAN_UNSTABLE_API assert_unreachable(const char* file, int line);
162
163#define BOTAN_ASSERT_UNREACHABLE() Botan::assert_unreachable(__FILE__, __LINE__)
164
165// NOLINTEND(*-macro-usage)
166
167} // namespace Botan
168
169#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition api.h:98
#define BOTAN_UNSTABLE_API
Definition api.h:34
constexpr void ignore_params(const T &... args)
Definition assert.h:142