Botan 3.11.0
Crypto and TLS for C&
assert.cpp
Go to the documentation of this file.
1/*
2* Runtime assertion checking
3* (C) 2010,2012,2018 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/assert.h>
9
10#include <botan/exceptn.h>
11#include <botan/range_concepts.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/target_info.h>
14#include <sstream>
15
16#if defined(BOTAN_TERMINATE_ON_ASSERTS)
17 #include <cstdlib>
18 #include <iostream>
19#endif
20
21namespace Botan {
22
23void throw_invalid_argument(const char* message, const char* func, const char* file) {
24 throw Invalid_Argument(fmt("{} in {}:{}", message, func, file));
25}
26
27void throw_invalid_state(const char* expr, const char* func, const char* file) {
28 throw Invalid_State(fmt("Invalid state: expr {} was false in {}:{}", expr, func, file));
29}
30
31// Declared in concepts.h
33 throw Invalid_Argument("Memory regions did not have expected byte lengths");
34}
35
36void assertion_failure(const char* expr_str, const char* assertion_made, const char* func, const char* file, int line) {
37 std::ostringstream format;
38
39 format << "False assertion ";
40
41 if(assertion_made != nullptr && assertion_made[0] != 0) {
42 format << "'" << assertion_made << "' (expression " << expr_str << ") ";
43 } else {
44 format << expr_str << " ";
45 }
46
47 if(func != nullptr) {
48 format << "in " << func << " ";
49 }
50
51 format << "@" << file << ":" << line;
52
53#if defined(BOTAN_TERMINATE_ON_ASSERTS)
54 std::cerr << format.str() << '\n';
55 std::abort();
56#else
57 throw Internal_Error(format.str());
58#endif
59}
60
61void assert_unreachable(const char* file, int line) {
62 const std::string msg = fmt("Codepath that was marked unreachable was reached @{}:{}", file, line);
63
64#if defined(BOTAN_TERMINATE_ON_ASSERTS)
65 std::cerr << msg << '\n';
66 std::abort();
67#else
68 throw Internal_Error(msg);
69#endif
70}
71
72} // namespace Botan
void BOTAN_UNSTABLE_API memory_region_size_violation()
Definition assert.cpp:32
void assert_unreachable(const char *file, int line)
Definition assert.cpp:61
void throw_invalid_state(const char *expr, const char *func, const char *file)
Definition assert.cpp:27
void throw_invalid_argument(const char *message, const char *func, const char *file)
Definition assert.cpp:23
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
void assertion_failure(const char *expr_str, const char *assertion_made, const char *func, const char *file, int line)
Definition assert.cpp:36