Fix critical bug in tau_realloc causing memory leaks and undefined behavior #58
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix critical bug in tau_realloc causing memory leaks and undefined behavior
🐛 Bug Analysis
The current implementation of
tau_realloccontains a critical logic error that causes memory leaks and potentially undefined behavior:The Problem
Memory Leak: When
realloc()fails:NULLptrremains allocated and validfree(NULL)which is a no-opSilent Failure: The function returns NULL to callers, but:
Real-world Impact: This bug affects critical paths:
✅ The Fix
📋 Design Rationale
Why Abort Instead of Continuing?
Memory allocation failure is catastrophic for a test framework:
Fail-fast philosophy:
No memory leak on abort:
Alternative approaches considered but rejected:
All alternatives lead to worse outcomes: data loss, crashes, or memory corruption.
🔧 Implementation Details
fprintf(stderr, ...)as stderr is always availableTAU_PRIu64macro for cross-compiler compatibility (MSVC vs GCC/Clang)tau_ulltotau_u64to match the format specifierTAU_ABORTmacro for consistent terminationnew_size > 0(realloc with size 0 is valid for freeing memory)✅ Testing Performed
🎯 Impact
This fix prevents:
The fail-fast approach with clear error messaging makes the framework more robust and debuggable.
📈 Before and After
Before (Buggy Behavior)
After (Fixed Behavior)
With this fix, you'll see a clear error message instead of a mysterious crash.