Lesson
Capstone: three bugs, three different symptoms
This tiny cache copies keys and values, evicts the oldest entry when full, and can dump its contents into a caller's buffer. It hides three independent bugs:
- a leak
- a use-after-free
- an off-by-one in a bounds check
Each one is found by a different signal: a failing test, the sanitizer, or a hidden test.
Your mission
Work like a tech lead. One bug per instruction. After each patch, run the tests and the sanitizer again and read what changed. Do not move on until the report is different, and do not accept a patch you cannot explain.
What to watch for
- Each slot owns two allocations.
cache_getreturns a pointer that is only valid until the nextcache_put. What if the caller passes it straight back in?snprintfreturns the length it would have written. What does it mean when that equals the space left?