/src/botan/src/lib/utils/assert.cpp
Line | Count | Source |
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 | | |
20 | | namespace Botan { |
21 | | |
22 | 4.29k | void throw_invalid_argument(const char* message, const char* func, const char* file) { |
23 | 4.29k | throw Invalid_Argument(fmt("{} in {}:{}", message, func, file)); |
24 | 4.29k | } |
25 | | |
26 | 27 | void throw_invalid_state(const char* expr, const char* func, const char* file) { |
27 | 27 | throw Invalid_State(fmt("Invalid state: expr {} was false in {}:{}", expr, func, file)); |
28 | 27 | } |
29 | | |
30 | 101 | void assertion_failure(const char* expr_str, const char* assertion_made, const char* func, const char* file, int line) { |
31 | 101 | std::ostringstream format; |
32 | | |
33 | 101 | format << "False assertion "; |
34 | | |
35 | 101 | if(assertion_made != nullptr && assertion_made[0] != 0) { |
36 | 89 | format << "'" << assertion_made << "' (expression " << expr_str << ") "; |
37 | 89 | } else { |
38 | 12 | format << expr_str << " "; |
39 | 12 | } |
40 | | |
41 | 101 | if(func != nullptr) { |
42 | 101 | format << "in " << func << " "; |
43 | 101 | } |
44 | | |
45 | 101 | format << "@" << file << ":" << line; |
46 | | |
47 | | #if defined(BOTAN_TERMINATE_ON_ASSERTS) |
48 | | std::cerr << format.str() << '\n'; |
49 | | std::abort(); |
50 | | #else |
51 | 101 | throw Internal_Error(format.str()); |
52 | 101 | #endif |
53 | 101 | } |
54 | | |
55 | 0 | void assert_unreachable(const char* file, int line) { |
56 | 0 | 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 | 0 | throw Internal_Error(msg); |
63 | 0 | #endif |
64 | 0 | } |
65 | | |
66 | | } // namespace Botan |