From 3fae5cafc21774ddb27f96d861bb1f404ef0f07c Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 13 Dec 2022 09:45:17 +0400 Subject: [PATCH] noreturn --- examples/assert.c | 28 ++++++++++++++++++++-------- examples/panic.c | 23 +++++++++++++---------- include/kernaux/assert.h | 9 +++++++-- src/assert.c | 6 +++++- tests/test_memmap.c | 15 ++++++++++++--- 5 files changed, 57 insertions(+), 24 deletions(-) diff --git a/examples/assert.c b/examples/assert.c index e7e6dd8..dab6752 100644 --- a/examples/assert.c +++ b/examples/assert.c @@ -2,9 +2,11 @@ #include #include +#include #include #include +static jmp_buf jmpbuf; static unsigned int count = 0; static const char *last_file = NULL; static int last_line = 0; @@ -19,34 +21,44 @@ static void assert_cb( last_file = file; last_line = line; last_str = str; + + longjmp(jmpbuf, 1); } int main() { + assert(setjmp(jmpbuf) == 0); + kernaux_assert_cb = assert_cb; - // cppcheck-suppress duplicateExpression - KERNAUX_ASSERT(1 == 1); + if (setjmp(jmpbuf) == 0) { + // cppcheck-suppress duplicateExpression + KERNAUX_ASSERT(1 == 1); + } assert(count == 0); assert(last_file == NULL); assert(last_line == 0); assert(last_str == NULL); - // cppcheck-suppress duplicateExpression - KERNAUX_ASSERT(1 != 1); + if (setjmp(jmpbuf) == 0) { + // cppcheck-suppress duplicateExpression + KERNAUX_ASSERT(1 != 1); + } assert(count == 1); assert(strcmp(last_file, __FILE__) == 0); - assert(last_line == __LINE__ - 4); + assert(last_line == __LINE__ - 5); assert(strcmp(last_str, "1 != 1") == 0); - // cppcheck-suppress staticStringCompare - KERNAUX_ASSERT(strcmp("qwe", "rty") == 0); + if (setjmp(jmpbuf) == 0) { + // cppcheck-suppress staticStringCompare + KERNAUX_ASSERT(strcmp("qwe", "rty") == 0); + } assert(count == 2); assert(strcmp(last_file, __FILE__) == 0); - assert(last_line == __LINE__ - 4); + assert(last_line == __LINE__ - 5); assert(strcmp(last_str, "strcmp(\"qwe\", \"rty\") == 0") == 0); return 0; diff --git a/examples/panic.c b/examples/panic.c index dc8878c..3bb76ba 100644 --- a/examples/panic.c +++ b/examples/panic.c @@ -2,9 +2,11 @@ #include #include +#include #include #include +static jmp_buf jmpbuf; static unsigned int count = 0; static const char *last_file = NULL; static int last_line = 0; @@ -19,30 +21,31 @@ static void assert_cb( last_file = file; last_line = line; last_str = str; + + longjmp(jmpbuf, 1); } int main() { - KERNAUX_PANIC("foo"); - - assert(count == 0); - assert(last_file == NULL); - assert(last_line == 0); - assert(last_str == NULL); + assert(setjmp(jmpbuf) == 0); kernaux_assert_cb = assert_cb; - KERNAUX_PANIC("bar"); + if (setjmp(jmpbuf) == 0) { + KERNAUX_PANIC("bar"); + } assert(count == 1); assert(strcmp(last_file, __FILE__) == 0); - assert(last_line == __LINE__ - 4); + assert(last_line == __LINE__ - 5); assert(strcmp(last_str, "bar") == 0); - KERNAUX_PANIC("car"); + if (setjmp(jmpbuf) == 0) { + KERNAUX_PANIC("car"); + } assert(count == 2); assert(strcmp(last_file, __FILE__) == 0); - assert(last_line == __LINE__ - 4); + assert(last_line == __LINE__ - 5); assert(strcmp(last_str, "car") == 0); } diff --git a/include/kernaux/assert.h b/include/kernaux/assert.h index 44435fb..3240305 100644 --- a/include/kernaux/assert.h +++ b/include/kernaux/assert.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + #ifdef KERNAUX_DISABLE_ASSERT #define KERNAUX_PANIC(msg) ((void)0) #define KERNAUX_ASSERT(cond) ((void)0) @@ -15,9 +17,12 @@ extern "C" { #define KERNAUX_NOTNULL(cond) KERNAUX_ASSERT(cond) -void kernaux_assert_do(const char *file, int line, const char *msg); +typedef void (*KernAux_Assert_Cb)(const char *file, int line, const char *msg); -extern void (*kernaux_assert_cb)(const char *file, int line, const char *msg); +extern KernAux_Assert_Cb kernaux_assert_cb; + +KERNAUX_NORETURN +void kernaux_assert_do(const char *file, int line, const char *msg); #ifdef __cplusplus } diff --git a/src/assert.c b/src/assert.c index 301e24a..766c686 100644 --- a/src/assert.c +++ b/src/assert.c @@ -6,7 +6,7 @@ #include -void (*kernaux_assert_cb)(const char *file, int line, const char *msg) = NULL; +KernAux_Assert_Cb kernaux_assert_cb = NULL; void kernaux_assert_do( const char *const file, @@ -14,4 +14,8 @@ void kernaux_assert_do( const char *const msg ) { if (kernaux_assert_cb) kernaux_assert_cb(file, line, msg); + + // Enforce noreturn. + volatile int x = 0; + for (;;) ++x; } diff --git a/tests/test_memmap.c b/tests/test_memmap.c index 2ddb9e3..c4aa9ec 100644 --- a/tests/test_memmap.c +++ b/tests/test_memmap.c @@ -9,15 +9,16 @@ #include #include +#include #include #include #include static KernAux_MemMap memmap; +static jmp_buf jmpbuf; static unsigned int assert_count_exp = 0; static unsigned int assert_count_ctr = 0; - static const char *assert_last_file = NULL; static void assert_cb( @@ -27,6 +28,8 @@ static void assert_cb( ) { ++assert_count_ctr; assert_last_file = file; + + longjmp(jmpbuf, 1); } static void before_assert() @@ -52,6 +55,8 @@ static void expect_assert() void test_main() { + assert(setjmp(jmpbuf) == 0); + kernaux_assert_cb = assert_cb; { @@ -71,7 +76,9 @@ void test_main() assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL); before_assert(); - assert(!KernAux_MemMap_finish(memmap)); + if (setjmp(jmpbuf) == 0) { + assert(!KernAux_MemMap_finish(memmap)); + } expect_assert(); } @@ -183,7 +190,9 @@ void test_main() assert(MEMMAP.entries[0].limit == 2); before_assert(); - assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL); + if (setjmp(jmpbuf) == 0) { + assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL); + } expect_assert(); }