Botan 3.8.1
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/internal/fmt.h>
12#include <botan/internal/target_info.h>
13#include <sstream>
14
15#if defined(BOTAN_TERMINATE_ON_ASSERTS)
16 #include <cstdlib>
17 #include <iostream>
18#endif
19
20namespace Botan {
21
22void throw_invalid_argument(const char* message, const char* func, const char* file) {
23 throw Invalid_Argument(fmt("{} in {}:{}", message, func, file));
24}
25
26void throw_invalid_state(const char* expr, const char* func, const char* file) {
27 throw Invalid_State(fmt("Invalid state: expr {} was false in {}:{}", expr, func, file));
28}
29
30void assertion_failure(const char* expr_str, const char* assertion_made, const char* func, const char* file, int line) {
31 std::ostringstream format;
32
33 format << "False assertion ";
34
35 if(assertion_made && assertion_made[0] != 0) {
36 format << "'" << assertion_made << "' (expression " << expr_str << ") ";
37 } else {
38 format << expr_str << " ";
39 }
40
41 if(func) {
42 format << "in " << func << " ";
43 }
44
45 format << "@" << file << ":" << line;
46
47#if defined(BOTAN_TERMINATE_ON_ASSERTS)
48 std::cerr << format.str() << '\n';
49 std::abort();
50#else
51 throw Internal_Error(format.str());
52#endif
53}
54
55void assert_unreachable(const char* file, int line) {
56 const std::string msg = fmt("Codepath that was marked unreachable was reached @{}:{}", file, line);
57
58#if defined(BOTAN_TERMINATE_ON_ASSERTS)
59 std::cerr << msg << '\n';
60 std::abort();
61#else
62 throw Internal_Error(msg);
63#endif
64}
65
66} // namespace Botan
void assert_unreachable(const char *file, int line)
Definition assert.cpp:55
void throw_invalid_state(const char *expr, const char *func, const char *file)
Definition assert.cpp:26
void throw_invalid_argument(const char *message, const char *func, const char *file)
Definition assert.cpp:22
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:30