Fix refcount leak and stack-trace buffer overflow
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>
This commit is contained in:
@@ -67,6 +67,8 @@ set(AKERR_TESTS
|
||||
err_release_clears
|
||||
err_pool_exhaust
|
||||
err_maxval
|
||||
err_refcount_double_fail
|
||||
err_stacktrace_bounds
|
||||
)
|
||||
|
||||
set(AKERR_WILL_FAIL_TESTS
|
||||
|
||||
Reference in New Issue
Block a user