Coverage Report

Created: 2025-11-16 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/build/include/public/botan/assert.h
Line
Count
Source
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
14
BOTAN_FUTURE_INTERNAL_HEADER(assert.h)
15
16
namespace 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
920k
   do {                                                          \
36
920k
      /* NOLINTNEXTLINE(*-simplify-boolean-expr) */              \
37
920k
      if(!(expr)) {                                              \
38
2
         /* NOLINTNEXTLINE(bugprone-lambda-function-name) */     \
39
2
         Botan::throw_invalid_argument(msg, __func__, __FILE__); \
40
2
      }                                                          \
41
920k
   } 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
228k
   do {                                                         \
52
228k
      /* NOLINTNEXTLINE(*-simplify-boolean-expr) */             \
53
228k
      if(!(expr)) {                                             \
54
0
         /* NOLINTNEXTLINE(bugprone-lambda-function-name) */    \
55
0
         Botan::throw_invalid_state(#expr, __func__, __FILE__); \
56
0
      }                                                         \
57
228k
   } while(0)
58
59
/**
60
* Make an assertion
61
*/
62
#define BOTAN_ASSERT(expr, assertion_made)                                              \
63
   /* NOLINTNEXTLINE(*-avoid-do-while) */                                               \
64
2.64M
   do {                                                                                 \
65
2.64M
      /* NOLINTNEXTLINE(*-simplify-boolean-expr) */                                     \
66
2.64M
      if(!(expr)) {                                                                     \
67
0
         /* NOLINTNEXTLINE(bugprone-lambda-function-name) */                            \
68
0
         Botan::assertion_failure(#expr, assertion_made, __func__, __FILE__, __LINE__); \
69
0
      }                                                                                 \
70
2.64M
   } while(0)
71
72
/**
73
* Make an assertion
74
*/
75
#define BOTAN_ASSERT_NOMSG(expr)                                            \
76
   /* NOLINTNEXTLINE(*-avoid-do-while) */                                   \
77
2.75M
   do {                                                                     \
78
2.75M
      /* NOLINTNEXTLINE(*-simplify-boolean-expr) */                         \
79
2.75M
      if(!(expr)) {                                                         \
80
0
         /* NOLINTNEXTLINE(bugprone-lambda-function-name) */                \
81
0
         Botan::assertion_failure(#expr, "", __func__, __FILE__, __LINE__); \
82
0
      }                                                                     \
83
2.75M
   } 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
0
   do {                                                                                                \
91
0
      /* NOLINTNEXTLINE(*-simplify-boolean-expr) */                                                    \
92
0
      if((expr1) != (expr2)) {                                                                         \
93
0
         /* NOLINTNEXTLINE(bugprone-lambda-function-name) */                                           \
94
0
         Botan::assertion_failure(#expr1 " == " #expr2, assertion_made, __func__, __FILE__, __LINE__); \
95
0
      }                                                                                                \
96
0
   } 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
415k
   do {                                                                                          \
104
415k
      /* NOLINTNEXTLINE(*-simplify-boolean-expr) */                                              \
105
831k
      if((expr1) && !(expr2)) {                                                                  \
106
0
         /* NOLINTNEXTLINE(bugprone-lambda-function-name) */                                     \
107
0
         Botan::assertion_failure(#expr1 " implies " #expr2, msg, __func__, __FILE__, __LINE__); \
108
0
      }                                                                                          \
109
415k
   } 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
14.3k
   do {                                                                                   \
117
14.3k
      if((ptr) == nullptr) {                                                              \
118
0
         /* NOLINTNEXTLINE(bugprone-lambda-function-name) */                              \
119
0
         Botan::assertion_failure(#ptr " is not null", "", __func__, __FILE__, __LINE__); \
120
0
      }                                                                                   \
121
14.3k
   } 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
21.9M
      do { /* NOLINT(*-avoid-do-while) */ \
131
21.9M
      } 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
*/
141
template <typename... T>
142
10.4M
constexpr void ignore_params([[maybe_unused]] const T&... args) {}
Unexecuted instantiation: void Botan::ignore_params<Botan::SCAN_Name>(Botan::SCAN_Name const&)
Unexecuted instantiation: void Botan::ignore_params<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&)
Unexecuted instantiation: void Botan::ignore_params<std::__1::span<unsigned char const, 18446744073709551615ul> >(std::__1::span<unsigned char const, 18446744073709551615ul> const&)
void Botan::ignore_params<unsigned char const*, unsigned long>(unsigned char const* const&, unsigned long const&)
Line
Count
Source
142
1.03k
constexpr void ignore_params([[maybe_unused]] const T&... args) {}
void Botan::ignore_params<unsigned long const*, unsigned long>(unsigned long const* const&, unsigned long const&)
Line
Count
Source
142
10.4M
constexpr void ignore_params([[maybe_unused]] const T&... args) {}
void Botan::ignore_params<unsigned long>(unsigned long const&)
Line
Count
Source
142
1.04k
constexpr void ignore_params([[maybe_unused]] const T&... args) {}
Unexecuted instantiation: void Botan::ignore_params<std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> > >(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> > const&)
Unexecuted instantiation: void Botan::ignore_params<Botan::BigInt, Botan::BigInt>(Botan::BigInt const&, Botan::BigInt const&)
Unexecuted instantiation: void Botan::ignore_params<unsigned char const*, unsigned char const*>(unsigned char const* const&, unsigned char const* const&)
Unexecuted instantiation: void Botan::ignore_params<Botan::RandomNumberGenerator>(Botan::RandomNumberGenerator const&)
Unexecuted instantiation: void Botan::ignore_params<void*, unsigned long>(void* const&, unsigned long const&)
Unexecuted instantiation: void Botan::ignore_params<std::__1::span<unsigned char const, 18446744073709551615ul>, std::__1::span<unsigned char const, 18446744073709551615ul> >(std::__1::span<unsigned char const, 18446744073709551615ul> const&, std::__1::span<unsigned char const, 18446744073709551615ul> const&)
Unexecuted instantiation: void Botan::ignore_params<unsigned short const*, unsigned long>(unsigned short const* const&, unsigned long const&)
Unexecuted instantiation: void Botan::ignore_params<Botan::BigInt, Botan::BigInt, Botan::BigInt, Botan::BigInt, Botan::BigInt, Botan::BigInt>(Botan::BigInt const&, Botan::BigInt const&, Botan::BigInt const&, Botan::BigInt const&, Botan::BigInt const&, Botan::BigInt const&)
Unexecuted instantiation: void Botan::ignore_params<std::__1::function<void (std::__1::span<unsigned char, 18446744073709551615ul>)> >(std::__1::function<void (std::__1::span<unsigned char, 18446744073709551615ul>)> const&)
Unexecuted instantiation: void Botan::ignore_params<unsigned int const*, unsigned long>(unsigned int const* const&, unsigned long const&)
143
144
10.4M
#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
0
#define BOTAN_ASSERT_UNREACHABLE() Botan::assert_unreachable(__FILE__, __LINE__)
164
165
// NOLINTEND(*-macro-usage)
166
167
}  // namespace Botan
168
169
#endif