/src/botan/src/lib/utils/assert.cpp
Line | Count | Source (jump to first uncovered line) |
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 <sstream> |
13 | | |
14 | | #if defined(BOTAN_TERMINATE_ON_ASSERTS) |
15 | | #include <cstdlib> |
16 | | #include <iostream> |
17 | | #endif |
18 | | |
19 | | namespace Botan { |
20 | | |
21 | 0 | void throw_invalid_argument(const char* message, const char* func, const char* file) { |
22 | 0 | throw Invalid_Argument(fmt("{} in {}:{}", message, func, file)); |
23 | 0 | } |
24 | | |
25 | 0 | void throw_invalid_state(const char* expr, const char* func, const char* file) { |
26 | 0 | throw Invalid_State(fmt("Invalid state: expr {} was false in {}:{}", expr, func, file)); |
27 | 0 | } |
28 | | |
29 | 0 | void assertion_failure(const char* expr_str, const char* assertion_made, const char* func, const char* file, int line) { |
30 | 0 | std::ostringstream format; |
31 | |
|
32 | 0 | format << "False assertion "; |
33 | |
|
34 | 0 | if(assertion_made && assertion_made[0] != 0) { |
35 | 0 | format << "'" << assertion_made << "' (expression " << expr_str << ") "; |
36 | 0 | } else { |
37 | 0 | format << expr_str << " "; |
38 | 0 | } |
39 | |
|
40 | 0 | if(func) { |
41 | 0 | format << "in " << func << " "; |
42 | 0 | } |
43 | |
|
44 | 0 | format << "@" << file << ":" << line; |
45 | |
|
46 | | #if defined(BOTAN_TERMINATE_ON_ASSERTS) |
47 | | std::cerr << format.str() << '\n'; |
48 | | std::abort(); |
49 | | #else |
50 | 0 | throw Internal_Error(format.str()); |
51 | 0 | #endif |
52 | 0 | } |
53 | | |
54 | | } // namespace Botan |