Botan 3.6.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/compiler.h>
13
14namespace Botan {
15
16/**
17* Called when an assertion fails
18* Throws an Exception object
19*/
20[[noreturn]] void BOTAN_PUBLIC_API(2, 0)
21 assertion_failure(const char* expr_str, const char* assertion_made, const char* func, const char* file, int line);
22
23/**
24* Called when an invalid argument is used
25* Throws Invalid_Argument
26*/
27[[noreturn]] void BOTAN_UNSTABLE_API throw_invalid_argument(const char* message, const char* func, const char* file);
28
29#define BOTAN_ARG_CHECK(expr, msg) \
30 do { \
31 if(!(expr)) \
32 Botan::throw_invalid_argument(msg, __func__, __FILE__); \
33 } while(0)
34
35/**
36* Called when an invalid state is encountered
37* Throws Invalid_State
38*/
39[[noreturn]] void BOTAN_UNSTABLE_API throw_invalid_state(const char* message, const char* func, const char* file);
40
41#define BOTAN_STATE_CHECK(expr) \
42 do { \
43 if(!(expr)) \
44 Botan::throw_invalid_state(#expr, __func__, __FILE__); \
45 } while(0)
46
47/**
48* Make an assertion
49*/
50#define BOTAN_ASSERT(expr, assertion_made) \
51 do { \
52 if(!(expr)) \
53 Botan::assertion_failure(#expr, assertion_made, __func__, __FILE__, __LINE__); \
54 } while(0)
55
56/**
57* Make an assertion
58*/
59#define BOTAN_ASSERT_NOMSG(expr) \
60 do { \
61 if(!(expr)) \
62 Botan::assertion_failure(#expr, "", __func__, __FILE__, __LINE__); \
63 } while(0)
64
65/**
66* Assert that value1 == value2
67*/
68#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made) \
69 do { \
70 if((expr1) != (expr2)) \
71 Botan::assertion_failure(#expr1 " == " #expr2, assertion_made, __func__, __FILE__, __LINE__); \
72 } while(0)
73
74/**
75* Assert that expr1 (if true) implies expr2 is also true
76*/
77#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg) \
78 do { \
79 if((expr1) && !(expr2)) \
80 Botan::assertion_failure(#expr1 " implies " #expr2, msg, __func__, __FILE__, __LINE__); \
81 } while(0)
82
83/**
84* Assert that a pointer is not null
85*/
86#define BOTAN_ASSERT_NONNULL(ptr) \
87 do { \
88 if((ptr) == nullptr) \
89 Botan::assertion_failure(#ptr " is not null", "", __func__, __FILE__, __LINE__); \
90 } while(0)
91
92#if defined(BOTAN_ENABLE_DEBUG_ASSERTS)
93
94 #define BOTAN_DEBUG_ASSERT(expr) BOTAN_ASSERT_NOMSG(expr)
95
96#else
97
98 #define BOTAN_DEBUG_ASSERT(expr) \
99 do { \
100 } while(0)
101
102#endif
103
104/**
105* Mark variable as unused.
106*
107* Takes any number of arguments and marks all as unused, for instance
108* BOTAN_UNUSED(a); or BOTAN_UNUSED(x, y, z);
109*/
110template <typename T>
111constexpr void ignore_param(T&&) {}
112
113template <typename... T>
114constexpr void ignore_params(T&&... args) {
115 (ignore_param(args), ...);
116}
117
118#define BOTAN_UNUSED Botan::ignore_params
119
120/*
121* Define Botan::assert_unreachable and BOTAN_ASSERT_UNREACHABLE
122*
123* This is intended to be used in the same situations as `std::unreachable()`;
124* a codepath that (should not) be reachable but where the compiler cannot
125* tell that it is unreachable.
126*
127* Unlike `std::unreachable()`, or equivalent compiler builtins like GCC's
128* `__builtin_unreachable`, this function is not UB. By default it will
129* throw an exception. If `BOTAN_TERMINATE_ON_ASSERTS` is defined, it will
130* instead print a message to stderr and abort.
131*
132* Due to this difference, and the fact that it is not inlined, calling
133* this is significantly more costly than using `std::unreachable`.
134*/
135[[noreturn]] void BOTAN_UNSTABLE_API assert_unreachable(const char* file, int line);
136
137#define BOTAN_ASSERT_UNREACHABLE() Botan::assert_unreachable(__FILE__, __LINE__)
138
139} // namespace Botan
140
141#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_UNSTABLE_API
Definition compiler.h:44
FE_25519 T
Definition ge.cpp:34
void assert_unreachable(const char *file, int line)
Definition assert.cpp:54
void throw_invalid_state(const char *expr, const char *func, const char *file)
Definition assert.cpp:25
void throw_invalid_argument(const char *message, const char *func, const char *file)
Definition assert.cpp:21
constexpr void ignore_param(T &&)
Definition assert.h:111
constexpr void ignore_params(T &&... args)
Definition assert.h:114
void assertion_failure(const char *expr_str, const char *assertion_made, const char *func, const char *file, int line)
Definition assert.cpp:29