• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 4834822395

28 Apr 2023 09:55PM CUT coverage: 92.132% (-0.01%) from 92.146%
4834822395

push

github

77584 of 84210 relevant lines covered (92.13%)

12129586.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/lib/utils/assert.h
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,
22
                     const char* assertion_made,
23
                     const char* func,
24
                     const char* file,
25
                     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,
32
                                                            const char* func,
33
                                                            const char* file);
34

35

36
#define BOTAN_ARG_CHECK(expr, msg)                                      \
37
   do { if(!(expr)) Botan::throw_invalid_argument(msg, __func__, __FILE__); } while(0)
38

39
/**
40
* Called when an invalid state is encountered
41
* Throws Invalid_State
42
*/
43
[[noreturn]] void BOTAN_UNSTABLE_API throw_invalid_state(const char* message,
44
                                                         const char* func,
45
                                                         const char* file);
46

47

48
#define BOTAN_STATE_CHECK(expr)                                     \
49
   do { if(!(expr)) Botan::throw_invalid_state(#expr, __func__, __FILE__); } while(0)
50

51
/**
52
* Make an assertion
53
*/
54
#define BOTAN_ASSERT(expr, assertion_made)                \
55
   do {                                                   \
56
      if(!(expr))                                         \
57
         Botan::assertion_failure(#expr,                  \
58
                                  assertion_made,         \
59
                                  __func__,               \
60
                                  __FILE__,               \
61
                                  __LINE__);              \
62
   } while(0)
63

64
/**
65
* Make an assertion
66
*/
67
#define BOTAN_ASSERT_NOMSG(expr)                          \
68
   do {                                                   \
69
      if(!(expr))                                         \
70
         Botan::assertion_failure(#expr,                  \
71
                                  "",                     \
72
                                  __func__,               \
73
                                  __FILE__,               \
74
                                  __LINE__);              \
75
   } while(0)
76

77
/**
78
* Assert that value1 == value2
79
*/
80
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)   \
81
   do {                                                    \
82
     if((expr1) != (expr2))                                \
83
       Botan::assertion_failure(#expr1 " == " #expr2,      \
84
                                assertion_made,            \
85
                                __func__,                  \
86
                                __FILE__,                  \
87
                                __LINE__);                 \
88
   } while(0)
89

90
/**
91
* Assert that expr1 (if true) implies expr2 is also true
92
*/
93
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)        \
94
   do {                                                    \
95
     if((expr1) && !(expr2))                               \
96
       Botan::assertion_failure(#expr1 " implies " #expr2, \
97
                                msg,                       \
98
                                __func__,                  \
99
                                __FILE__,                  \
100
                                __LINE__);                 \
101
   } while(0)
102

103
/**
104
* Assert that a pointer is not null
105
*/
106
#define BOTAN_ASSERT_NONNULL(ptr)                          \
107
   do {                                                    \
108
     if((ptr) == nullptr)                                  \
109
         Botan::assertion_failure(#ptr " is not null",     \
110
                                  "",                      \
111
                                  __func__,                \
112
                                  __FILE__,                \
113
                                  __LINE__);               \
114
   } while(0)
115

116
#if defined(BOTAN_ENABLE_DEBUG_ASSERTS)
117

118
#define BOTAN_DEBUG_ASSERT(expr) BOTAN_ASSERT_NOMSG(expr)
119

120
#else
121

122
#define BOTAN_DEBUG_ASSERT(expr) do {} while(0)
123

124
#endif
125

126
/**
127
* Mark variable as unused.
128
*
129
* Takes any number of arguments and marks all as unused, for instance
130
* BOTAN_UNUSED(a); or BOTAN_UNUSED(x, y, z);
131
*/
132
template<typename T>
133
void ignore_param(T&&) {}
134

135
template<typename... T>
136
void ignore_params(T&&... args)
743✔
137
   {
138
   (ignore_param(args) , ...);
139
   }
192✔
140

141
#define BOTAN_UNUSED Botan::ignore_params
142

143
/*
144
* Define Botan::unreachable()
145
*
146
* There is a pending WG21 proposal for `std::unreachable()`
147
*   http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0627r3.pdf
148
*/
149
[[noreturn]] BOTAN_FORCE_INLINE void unreachable()
×
150
   {
151
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_unreachable)
152
   __builtin_unreachable();
×
153
#elif defined(_MSC_VER) // MSVC
154
   __assume(false);
155
#else
156
   return; // undefined behaviour, just like the others...
157
#endif
158
   }
159

160
}
161

162
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc