Two memory-safety bugs in the macro core: 1. Refcount leak. ENSURE_ERROR_READY incremented refcount on every FAIL/SUCCEED rather than only when it acquired a fresh context from the pool. A function that FAILed a context more than once and then propagated arrived at its caller with refcount 2; the caller released once, leaking the slot. After AKERR_MAX_ARRAY_ERROR leaks the pool is exhausted and the library exit(1)s. Move the increment inside the acquisition branch. 2. Stack-trace overflow. Each appended frame passed the full buffer length to snprintf instead of the space remaining, and advanced the cursor by snprintf's would-be return value, so a trace that filled the buffer wrote past the end of stacktracebuf and ran the cursor out of bounds. Add AKERR_STACKTRACE_APPEND, which bounds the write to the remaining space and clamps the cursor advance. Regression tests err_refcount_double_fail and err_stacktrace_bounds fail against the old code (verified) and pass now. Full suite: 21/21, no warnings. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
147 lines
4.3 KiB
CMake
147 lines
4.3 KiB
CMake
cmake_minimum_required(VERSION 3.10)
|
|
project(akerror LANGUAGES C)
|
|
|
|
include(GNUInstallDirs)
|
|
include(CMakePackageConfigHelpers)
|
|
include(CTest)
|
|
|
|
set(AKERR_USE_STDLIB 1 CACHE BOOL "Use the C standard library")
|
|
set(akerror_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/akerror")
|
|
|
|
set(SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generrno.sh)
|
|
set(INFILE ${CMAKE_CURRENT_SOURCE_DIR}/include/akerror.tmpl.h)
|
|
|
|
set(GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
|
|
|
|
set(GENERATED_ERRNO_C ${GENERATED_DIR}/src/errno.c)
|
|
set(GENERATED_AKERROR_H ${GENERATED_DIR}/include/akerror.h)
|
|
|
|
add_custom_command(
|
|
OUTPUT ${GENERATED_ERRNO_C} ${GENERATED_AKERROR_H}
|
|
COMMAND ${CMAKE_COMMAND} -E make_directory ${GENERATED_DIR}
|
|
COMMAND /usr/bin/env bash
|
|
${SCRIPT}
|
|
${CMAKE_CURRENT_SOURCE_DIR}
|
|
${GENERATED_DIR}
|
|
DEPENDS ${SCRIPT} ${INFILE}
|
|
VERBATIM
|
|
)
|
|
|
|
add_library(akerror SHARED
|
|
src/error.c
|
|
${GENERATED_ERRNO_C}
|
|
)
|
|
|
|
target_include_directories(akerror PUBLIC
|
|
$<BUILD_INTERFACE:${GENERATED_DIR}/include>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
|
)
|
|
|
|
find_package(PkgConfig REQUIRED)
|
|
add_library(akerror::akerror ALIAS akerror)
|
|
|
|
target_compile_definitions(akerror
|
|
PUBLIC AKERR_USE_STDLIB=${AKERR_USE_STDLIB}
|
|
)
|
|
|
|
# Each test is one source file in tests/ built into test_<name> and registered
|
|
# as CTest <name>. Tests expected to abort (unhandled error / contract
|
|
# violation) go in AKERR_WILL_FAIL_TESTS; all others must exit 0.
|
|
set(AKERR_TESTS
|
|
err_catch
|
|
err_cleanup
|
|
err_trace
|
|
err_improper_closure
|
|
err_success
|
|
err_pool_refcount
|
|
err_handle_default
|
|
err_handle_group
|
|
err_handle_dispatch
|
|
err_pass
|
|
err_ignore
|
|
err_swallow
|
|
err_errno
|
|
err_break_variants
|
|
err_custom_handler
|
|
err_error_names
|
|
err_release_clears
|
|
err_pool_exhaust
|
|
err_maxval
|
|
err_refcount_double_fail
|
|
err_stacktrace_bounds
|
|
)
|
|
|
|
set(AKERR_WILL_FAIL_TESTS
|
|
err_trace
|
|
err_improper_closure
|
|
)
|
|
|
|
foreach(_test IN LISTS AKERR_TESTS)
|
|
add_executable(test_${_test} tests/${_test}.c)
|
|
target_include_directories(test_${_test} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
|
target_link_libraries(test_${_test} PRIVATE akerror)
|
|
add_test(NAME ${_test} COMMAND test_${_test})
|
|
endforeach()
|
|
|
|
# err_maxval parses the generated header to check AKERR_MAX_ERR_VALUE covers
|
|
# every AKERR_* code, so it needs the path to the header it was built against.
|
|
target_compile_definitions(test_err_maxval PRIVATE
|
|
AKERR_GENERATED_HEADER="${GENERATED_AKERROR_H}")
|
|
|
|
set_tests_properties(
|
|
${AKERR_WILL_FAIL_TESTS}
|
|
PROPERTIES WILL_FAIL TRUE
|
|
)
|
|
|
|
# Mutation testing: break the library in small ways and confirm the test suite
|
|
# notices. This is a meta-check on the tests themselves, so it is a manual
|
|
# target (it rebuilds and re-runs the whole suite many times), not a CTest test.
|
|
# cmake --build build --target mutation
|
|
find_package(Python3 COMPONENTS Interpreter)
|
|
if(Python3_FOUND)
|
|
add_custom_target(mutation
|
|
COMMAND ${Python3_EXECUTABLE}
|
|
${CMAKE_CURRENT_SOURCE_DIR}/scripts/mutation_test.py
|
|
--source-root ${CMAKE_CURRENT_SOURCE_DIR}
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
|
USES_TERMINAL
|
|
COMMENT "Running mutation tests (breaks the library, expects tests to fail)"
|
|
)
|
|
endif()
|
|
|
|
set(main_lib_dest "lib/my_library-${MY_LIBRARY_VERSION}")
|
|
install(TARGETS akerror
|
|
EXPORT akerrorTargets
|
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
|
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
|
)
|
|
|
|
install(FILES ${GENERATED_AKERROR_H} DESTINATION "include/")
|
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/akerror.pc DESTINATION "lib/pkgconfig/")
|
|
|
|
install(EXPORT akerrorTargets
|
|
FILE akerrorTargets.cmake
|
|
NAMESPACE akerror::
|
|
DESTINATION ${akerror_install_cmakedir}
|
|
)
|
|
|
|
configure_package_config_file(
|
|
cmake/akerror.cmake.in
|
|
"${CMAKE_CURRENT_BINARY_DIR}/akerrorConfig.cmake"
|
|
INSTALL_DESTINATION ${akerror_install_cmakedir}
|
|
)
|
|
|
|
install(FILES
|
|
"${CMAKE_CURRENT_BINARY_DIR}/akerrorConfig.cmake"
|
|
DESTINATION ${akerror_install_cmakedir}
|
|
)
|
|
|
|
# pkgconfig
|
|
set(prefix ${CMAKE_INSTALL_PREFIX})
|
|
set(exec_prefix "\${prefix}")
|
|
set(libdir "\${exec_prefix}/lib")
|
|
set(includedir "\${prefix}/include")
|
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/akerror.pc.in ${CMAKE_CURRENT_BINARY_DIR}/akerror.pc @ONLY)
|