Coverage Report

Created: 2025-03-09 06:52

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