From 0c29f5d69f880b731533a7bc521b41e98f9b64e1 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Wed, 6 May 2026 12:36:56 -0400 Subject: [PATCH] Fix handling of CATCH() or FAIL() macros around functions that should return an (akerr_ErrorContext *) but return an invalid pointer (to something not in our exception array) --- CMakeLists.txt | 3 +++ src/error.c | 6 ++++++ tests/err_improper_closure.c | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 tests/err_improper_closure.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e0d1265..e1e40c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,9 +36,11 @@ target_compile_definitions(akerror add_executable(test_err_catch tests/err_catch.c) add_executable(test_err_cleanup tests/err_cleanup.c) add_executable(test_err_trace tests/err_trace.c) +add_executable(test_err_improper_closure tests/err_improper_closure.c) add_test(NAME err_catch COMMAND test_err_catch) add_test(NAME err_cleanup COMMAND test_err_cleanup) add_test(NAME err_trace COMMAND test_err_trace) +add_test(NAME err_improper_closure COMMAND test_err_improper_closure) # Specify include directories for the library's headers (if applicable) target_include_directories(akerror PUBLIC @@ -48,6 +50,7 @@ target_include_directories(akerror PUBLIC target_link_libraries(test_err_catch PRIVATE akerror) target_link_libraries(test_err_cleanup PRIVATE akerror) target_link_libraries(test_err_trace PRIVATE akerror) +target_link_libraries(test_err_improper_closure PRIVATE akerror) set(main_lib_dest "lib/my_library-${MY_LIBRARY_VERSION}") install(TARGETS akerror EXPORT akerror DESTINATION "lib/") diff --git a/src/error.c b/src/error.c index f68f10a..0837030 100644 --- a/src/error.c +++ b/src/error.c @@ -14,6 +14,12 @@ char __AKERR_ERROR_NAMES[AKERR_MAX_ERR_VALUE+1][AKERR_MAX_ERROR_NAME_LENGTH]; akerr_ErrorContext AKERR_ARRAY_ERROR[AKERR_MAX_ARRAY_ERROR]; +int akerr_valid_error_address(akerr_ErrorContext *ptr) +{ + // Is this within the memory region occupied by AKERR_ARRAY_ERROR? + return ((ptr >= &AKERR_ARRAY_ERROR[0]) && (ptr <= &AKERR_ARRAY_ERROR[AKERR_MAX_ARRAY_ERROR-1])); +} + void akerr_default_logger(const char *fmt, ...) { #if defined(AKERR_USE_STDLIB) && AKERR_USE_STDLIB == 1 diff --git a/tests/err_improper_closure.c b/tests/err_improper_closure.c new file mode 100644 index 0000000..1153736 --- /dev/null +++ b/tests/err_improper_closure.c @@ -0,0 +1,22 @@ +#include "akerror.h" +#include + +akerr_ErrorContext AKERR_NOIGNORE *improper_closure(void) +{ + PREPARE_ERROR(errctx); + ATTEMPT { + } CLEANUP { + } PROCESS(errctx) { + } FINISH(errctx, true); + fprintf(stderr, "Improperly returning from improper_closure\n"); +} + +int main(void) +{ + PREPARE_ERROR(errctx); + ATTEMPT { + CATCH(errctx, improper_closure()); + } CLEANUP { + } PROCESS(errctx) { + } FINISH_NORETURN(errctx); +}