Skip to content

Commit 856c69c

Browse files
committed
abort during infinite exception cycle
1 parent f3f79f8 commit 856c69c

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

api/expects

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@
3232
#endif
3333

3434
#include <os.hpp>
35+
#include <serial>
36+
37+
inline bool __expect_is_recursive() {
38+
#ifndef UNITTESTS
39+
static volatile bool in_failure = false;
40+
if (in_failure) return true;
41+
in_failure = true;
42+
return false;
43+
#else
44+
return false;
45+
#endif
46+
}
47+
3548
inline void __expect_emit_failure(std::string_view msg, std::string_view panic_text) {
3649
#ifndef UNITTESTS
3750

@@ -55,13 +68,36 @@ inline void __expect_emit_failure(std::string_view msg, std::string_view panic_t
5568
}
5669

5770
template <class... Args>
58-
inline void __expect_failf(const char *err_prefix, const char * /*cond*/, const char *file, int line, const char *func, std::format_string<Args...> fmt, Args&&... args){
71+
inline void __expect_failf(const char *err_prefix, const char * cond, const char *file, int line, const char *func, std::format_string<Args...> fmt, Args&&... args){
72+
if (__expect_is_recursive()) {
73+
kprint("Fatal Expects/Ensures recursion. (libc not initialized?)\n");
74+
75+
kprint("Condition was '");
76+
kprint(cond); kprint("' @ ");
77+
kprint(file); kprint(":");
78+
kprint(func); kprint(" with fmt=");
79+
kprint(fmt.get().data()); kprint("\n");
80+
81+
// os::shutdown();
82+
__arch_poweroff();
83+
}
5984
auto reason_msg = std::format(fmt, std::forward<Args>(args)...);
6085
auto error_msg = std::format("{}:{}:{}: {}: {}", file, line, func, err_prefix, reason_msg);
6186
__expect_emit_failure(error_msg, reason_msg);
6287
}
6388

6489
inline void __expect_failf(const char *err_prefix, const char *cond, const char *file, int line, const char *func){
90+
if (__expect_is_recursive()) {
91+
kprint("Fatal Expects/Ensures recursion (libc not initialized?)\n");
92+
93+
kprint("Condition was '");
94+
kprint(cond); kprint("' @ ");
95+
kprint(file); kprint(":");
96+
kprint(func); kprint("\n");
97+
98+
// os::shutdown();
99+
__arch_poweroff();
100+
}
65101
auto reason_msg = std::format("{}: {}", err_prefix, cond);
66102
auto error_msg = std::format("{}:{}:{}: {}", file, line, func, err_prefix);
67103
__expect_emit_failure(error_msg, reason_msg);

0 commit comments

Comments
 (0)