Debug C memory errors with sanitizers
8 steps · about 4 h 5 min · Intermediate
- Enable useful compiler warnings (reference, AddressSanitizer documentation, 30 min)
Catch suspicious conversions, declarations and control flow before runtime investigation.
- Classify memory-safety failures (guide, Wikipedia, 30 min)
Distinguish bounds, lifetime, initialization and ownership errors to guide the search.
- Build with AddressSanitizer (reference, Clang documentation, 35 min)
Instrument memory accesses and obtain a precise report near the first invalid operation.
- Check undefined behaviour (reference, Clang documentation, 30 min)
Expose invalid arithmetic, alignment and type operations that may otherwise appear intermittent.
- Read the failing call stack (guide, Wikipedia, 25 min)
Trace the bad operation back through callers and distinguish symptom from origin.
- Reduce the failing input (guide, Wikipedia, 30 min)
Remove unrelated paths until the ownership or boundary mistake is visible and repeatable.
- Make ownership explicit (guide, Wikipedia, 30 min)
Document who allocates, transfers and releases each resource even though C lacks automatic ownership.
- Keep a sanitizer regression test (reference, CMake documentation, 35 min)
Automate the smallest failing scenario so the corrected boundary remains protected.