Coverage Report

Created: 2023-06-07 06:59

/src/botan/build/include/botan/assert.h
Line
Count
Source (jump to first uncovered line)
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
14
namespace 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
2.37k
   do {                                                          \
31
2.37k
      if(!(expr))                                                \
32
2.37k
         Botan::throw_invalid_argument(msg, __func__, __FILE__); \
33
2.37k
   } 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
673k
   do {                                                                                 \
52
673k
      if(!(expr))                                                                       \
53
673k
         Botan::assertion_failure(#expr, assertion_made, __func__, __FILE__, __LINE__); \
54
673k
   } while(0)
55
56
/**
57
* Make an assertion
58
*/
59
#define BOTAN_ASSERT_NOMSG(expr)                                            \
60
4.65k
   do {                                                                     \
61
4.65k
      if(!(expr))                                                           \
62
4.65k
         Botan::assertion_failure(#expr, "", __func__, __FILE__, __LINE__); \
63
4.65k
   } 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
24.3k
   do {                                                                                          \
79
48.4k
      if((expr1) && !(expr2))                                                                    \
80
24.3k
         Botan::assertion_failure(#expr1 " implies " #expr2, msg, __func__, __FILE__, __LINE__); \
81
24.3k
   } 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
87.7k
      do {                          \
100
87.7k
      } 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
*/
110
template <typename T>
111
771k
void ignore_param(T&&) {}
void Botan::ignore_param<unsigned long&>(unsigned long&)
Line
Count
Source
111
762k
void ignore_param(T&&) {}
void Botan::ignore_param<unsigned long const&>(unsigned long const&)
Line
Count
Source
111
9.23k
void ignore_param(T&&) {}
Unexecuted instantiation: void Botan::ignore_param<void*&>(void*&)
112
113
template <typename... T>
114
771k
void ignore_params(T&&... args) {
115
771k
   (ignore_param(args), ...);
116
771k
}
void Botan::ignore_params<unsigned long&>(unsigned long&)
Line
Count
Source
114
762k
void ignore_params(T&&... args) {
115
762k
   (ignore_param(args), ...);
116
762k
}
Unexecuted instantiation: void Botan::ignore_params<unsigned long const&, unsigned long const&>(unsigned long const&, unsigned long const&)
void Botan::ignore_params<unsigned long const&>(unsigned long const&)
Line
Count
Source
114
9.23k
void ignore_params(T&&... args) {
115
9.23k
   (ignore_param(args), ...);
116
9.23k
}
Unexecuted instantiation: void Botan::ignore_params<void*&, unsigned long&>(void*&, unsigned long&)
117
118
771k
#define BOTAN_UNUSED Botan::ignore_params
119
120
/*
121
* Define Botan::unreachable()
122
*
123
* There is a pending WG21 proposal for `std::unreachable()`
124
*   http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0627r3.pdf
125
*/
126
0
[[noreturn]] BOTAN_FORCE_INLINE void unreachable() {
127
0
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_unreachable)
128
0
   __builtin_unreachable();
129
0
#elif defined(_MSC_VER)  // MSVC
130
0
   __assume(false);
131
0
#else
132
0
   return;  // undefined behaviour, just like the others...
133
0
#endif
134
0
}
135
136
}  // namespace Botan
137
138
#endif