Coverage Report

Created: 2024-06-28 06:19

/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/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
7.38M
   do {                                                          \
31
7.38M
      if(!(expr))                                                \
32
7.38M
         Botan::throw_invalid_argument(msg, __func__, __FILE__); \
33
7.38M
   } 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
583k
   do {                                                         \
43
583k
      if(!(expr))                                               \
44
583k
         Botan::throw_invalid_state(#expr, __func__, __FILE__); \
45
583k
   } while(0)
46
47
/**
48
* Make an assertion
49
*/
50
#define BOTAN_ASSERT(expr, assertion_made)                                              \
51
56.7M
   do {                                                                                 \
52
56.7M
      if(!(expr))                                                                       \
53
56.7M
         Botan::assertion_failure(#expr, assertion_made, __func__, __FILE__, __LINE__); \
54
56.7M
   } while(0)
55
56
/**
57
* Make an assertion
58
*/
59
#define BOTAN_ASSERT_NOMSG(expr)                                            \
60
13.4M
   do {                                                                     \
61
13.4M
      if(!(expr))                                                           \
62
13.4M
         Botan::assertion_failure(#expr, "", __func__, __FILE__, __LINE__); \
63
13.4M
   } while(0)
64
65
/**
66
* Assert that value1 == value2
67
*/
68
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)                                               \
69
781k
   do {                                                                                                \
70
781k
      if((expr1) != (expr2))                                                                           \
71
781k
         Botan::assertion_failure(#expr1 " == " #expr2, assertion_made, __func__, __FILE__, __LINE__); \
72
781k
   } 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
14.7M
   do {                                                                                          \
79
29.4M
      if((expr1) && !(expr2))                                                                    \
80
14.7M
         Botan::assertion_failure(#expr1 " implies " #expr2, msg, __func__, __FILE__, __LINE__); \
81
14.7M
   } while(0)
82
83
/**
84
* Assert that a pointer is not null
85
*/
86
#define BOTAN_ASSERT_NONNULL(ptr)                                                         \
87
93
   do {                                                                                   \
88
93
      if((ptr) == nullptr)                                                                \
89
93
         Botan::assertion_failure(#ptr " is not null", "", __func__, __FILE__, __LINE__); \
90
93
   } 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
35.5M
      do {                          \
100
35.5M
      } 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
36.2M
constexpr void ignore_param(T&&) {}
Unexecuted instantiation: void Botan::ignore_param<Botan::RandomNumberGenerator&>(Botan::RandomNumberGenerator&)
Unexecuted instantiation: void Botan::ignore_param<Botan::SCAN_Name const&>(Botan::SCAN_Name const&)
Unexecuted instantiation: 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> >&)
Unexecuted instantiation: void Botan::ignore_param<unsigned char const*&>(unsigned char const*&)
Unexecuted instantiation: void Botan::ignore_param<std::__1::span<unsigned char const, 18446744073709551615ul>&>(std::__1::span<unsigned char const, 18446744073709551615ul>&)
Unexecuted instantiation: void Botan::ignore_param<unsigned char&>(unsigned char&)
void Botan::ignore_param<unsigned long&>(unsigned long&)
Line
Count
Source
111
27.6M
constexpr void ignore_param(T&&) {}
void Botan::ignore_param<unsigned long const&>(unsigned long const&)
Line
Count
Source
111
5.45M
constexpr void ignore_param(T&&) {}
void Botan::ignore_param<unsigned long const*&>(unsigned long const*&)
Line
Count
Source
111
6.24k
constexpr void ignore_param(T&&) {}
void Botan::ignore_param<std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&>(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&)
Line
Count
Source
111
1.69M
constexpr void ignore_param(T&&) {}
void Botan::ignore_param<long&>(long&)
Line
Count
Source
111
1.44M
constexpr void ignore_param(T&&) {}
Unexecuted instantiation: void Botan::ignore_param<void*&>(void*&)
Unexecuted instantiation: void Botan::ignore_param<unsigned short const*&>(unsigned short const*&)
Unexecuted instantiation: void Botan::ignore_param<unsigned int const*&>(unsigned int const*&)
112
113
template <typename... T>
114
33.5M
constexpr void ignore_params(T&&... args) {
115
33.5M
   (ignore_param(args), ...);
116
33.5M
}
Unexecuted instantiation: void Botan::ignore_params<Botan::RandomNumberGenerator&>(Botan::RandomNumberGenerator&)
Unexecuted instantiation: void Botan::ignore_params<Botan::SCAN_Name const&>(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> >&)
Unexecuted instantiation: void Botan::ignore_params<unsigned char const*&>(unsigned char const*&)
Unexecuted instantiation: void Botan::ignore_params<std::__1::span<unsigned char const, 18446744073709551615ul>&>(std::__1::span<unsigned char const, 18446744073709551615ul>&)
Unexecuted instantiation: void Botan::ignore_params<unsigned char&>(unsigned char&)
void Botan::ignore_params<unsigned long&>(unsigned long&)
Line
Count
Source
114
27.6M
constexpr void ignore_params(T&&... args) {
115
27.6M
   (ignore_param(args), ...);
116
27.6M
}
void Botan::ignore_params<unsigned long const&>(unsigned long const&)
Line
Count
Source
114
38.4k
constexpr void ignore_params(T&&... args) {
115
38.4k
   (ignore_param(args), ...);
116
38.4k
}
void Botan::ignore_params<unsigned long const&, unsigned long const&>(unsigned long const&, unsigned long const&)
Line
Count
Source
114
2.71M
constexpr void ignore_params(T&&... args) {
115
2.71M
   (ignore_param(args), ...);
116
2.71M
}
void Botan::ignore_params<unsigned long const*&, unsigned long&>(unsigned long const*&, unsigned long&)
Line
Count
Source
114
6.24k
constexpr void ignore_params(T&&... args) {
115
6.24k
   (ignore_param(args), ...);
116
6.24k
}
void Botan::ignore_params<std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&>(std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> >&)
Line
Count
Source
114
1.69M
constexpr void ignore_params(T&&... args) {
115
1.69M
   (ignore_param(args), ...);
116
1.69M
}
void Botan::ignore_params<long&>(long&)
Line
Count
Source
114
1.44M
constexpr void ignore_params(T&&... args) {
115
1.44M
   (ignore_param(args), ...);
116
1.44M
}
Unexecuted instantiation: void Botan::ignore_params<unsigned char const*&, unsigned long&>(unsigned char const*&, unsigned long&)
Unexecuted instantiation: void Botan::ignore_params<unsigned char const*&, unsigned char const*&>(unsigned char const*&, unsigned char const*&)
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>&)
Unexecuted instantiation: void Botan::ignore_params<unsigned short const*&, unsigned long&>(unsigned short const*&, unsigned long&)
Unexecuted instantiation: void Botan::ignore_params<unsigned int const*&, unsigned long&>(unsigned int const*&, unsigned long&)
117
118
33.5M
#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
0
#define BOTAN_ASSERT_UNREACHABLE() Botan::assert_unreachable(__FILE__, __LINE__)
138
139
}  // namespace Botan
140
141
#endif