cpp-ub-sanitizerslisted
Install: claude install-skill rustcircle/c101
# Undefined behavior and sanitizers in C and C++
UB means the compiler may assume it never happens. Optimisers exploit that, so symptoms appear
far from the cause, change with `-O` level, compiler and unrelated edits, and tests at `-O0`
stay green. The procedure: catalogue the suspects, run the right sanitizer at `-O1`/`-O2`,
triage the first report, fix the design, keep a regression test that runs under the sanitizer.
Deep material: [full UB catalogue with triggers and detectors](references/ub-catalogue.md) |
[sanitizer CI: presets, suppressions, MSan libc++, GitHub Actions](references/sanitizer-ci.md)
## 1. UB catalogue (top offenders per category)
| Category | UB | Typical trigger | Detector |
|---|---|---|---|
| memory | out-of-bounds read/write | `v[v.size()]`, index from input | ASan; hardened library modes |
| memory | use-after-free / double free / `new[]`-`delete` mismatch | reference or iterator kept across `push_back`; manual ownership | ASan |
| lifetime | use-after-return / use-after-scope | returning `&local`, `[&]` lambda or `string_view` outliving its scope | ASan (`detect_stack_use_after_return`), Clang `-Wdangling*` |
| lifetime | reading an object outside its lifetime | old pointer after placement-new, type-punned buffer | review; `std::launder`, C++23 `std::start_lifetime_as` |
| lifetime | uninitialised read | `int x; if (c) x = 1; return x;` | MSan; `-Wuninitialized`; `-ftrivial-auto-var-init=pattern` in tests; erroneous behavior in C++26 (still a bu