# Cpp Sanitizer Triage

> Find and read memory and concurrency bugs in C and C++ with AddressSanitizer, UndefinedBehaviorSanitizer, ThreadSanitizer and MemorySanitizer: build the whole test suite with address and undefined together, run ThreadSanitizer alone in its own job, treat MemorySanitizer as a separate Clang-only effort, set the options that make CI fail and frames readable, and read each report in order (error type, faulting access, freed-by and allocated-by stacks). Use whenever a C or C++ program needs memory or race checking, including "heap-use-after-free", "AddressSanitizer report, where do I start", "undefined behaviour only in release builds", "crashes under -O2", "set up sanitizers in CI", "ThreadSanitizer data race report". Not for input exploration and fuzzing (fuzz-harness), a production core file (native-core-dump-analysis), unsafe Rust (rust-unsafe-review), or security assessment (security-review).

- Skill: `greenblacked/cpp-sanitizer-triage` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx get-skill add greenblacked/cpp-sanitizer-triage`
- Raw SKILL.md: https://api.skillmd.com/api/skills/greenblacked/cpp-sanitizer-triage/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- Author: greenblacked (https://skillmd.com/u/greenblacked)
- Updated: 2026-11-19
- Page: https://skillmd.com/skills/greenblacked/cpp-sanitizer-triage

---


# C++ sanitizer triage

Done means the test suite runs clean under the right sanitizer builds in CI, every report found along the way was fixed at its cause (or, for third-party code only, suppressed with a reason and a revisit date), and each failing input is kept as a regression test.

The usual failure is running a sanitizer on the application once, reading the first line, and patching the faulting access. The faulting line is where the damage was noticed. The mistake is in the ownership shown by the other two stacks. A second failure is the build matrix: combining sanitizers that cannot coexist, or running them only by hand, so the next regression arrives unflagged.

## Scope

Use for: setting up sanitizer builds, reading and fixing their reports, and "only in release" misbehaviour that a sanitizer can expose.

Do not use for: choosing or generating inputs to explore a parser, or fuzzing (`fuzz-harness`); a production crash where you have only a core file (`native-core-dump-analysis`); `unsafe` blocks in Rust (`rust-unsafe-review`); judging whether a bug is exploitable or assessing a system (`security-review`).

## Workflow

### 1. Choose the combination

| Build | Flags | Finds | Notes |
| --- | --- | --- | --- |
| ASan + UBSan | `-fsanitize=address,undefined -fno-omit-frame-pointer -g -O1` | Use-after-free, overflows, leaks, use-after-return, signed overflow, bad shifts, null deref | The default job; works in GCC and Clang |
| TSan | `-fsanitize=thread -g -O1` | Data races, lock-order inversions | Cannot be combined with ASan; its own job and its own build tree |
| MSan | `-fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer -g -O1` | Reads of uninitialised memory | Clang only; needs every dependency, including libc++, built instrumented, so run it as a separate project |

Build the test suite with these flags, not only the main binary. Tests reach the paths that matter, and the sanitizer runtime must be linked into whatever executable runs. Use `-O1`: it keeps frames readable while still running fast enough, and `-O0` can hide bugs that only optimisation exposes. For "only in release", also run the ASan and UBSan job at the release optimisation level (`-O2`), because undefined behaviour is often exploited by the optimiser only there.

Use one build directory per sanitizer. Mixing object files built with different sanitizers fails at link time or silently loses coverage.

### 2. Set the runtime options so CI fails and frames read

```bash
export ASAN_OPTIONS=detect_stack_use_after_return=1:strict_string_checks=1:detect_leaks=1
export UBSAN_OPTIONS=print_stacktrace=1
export ASAN_SYMBOLIZER_PATH=$(command -v llvm-symbolizer)
export TSAN_OPTIONS=second_deadlock_stack=1
```

Add `-fno-sanitize-recover=all` at compile time so a UBSan finding aborts the process; without it UBSan prints and continues, and CI sees a pass. ASan stops on its first error by default. If frames show raw addresses, the symbolizer is missing or the build lacks `-g`. Run the tests with the options set in the job itself, not in a developer's shell, so the job and the local run agree.

### 3. Read the report in a fixed order

1. Error type on the first line (`heap-use-after-free`, `heap-buffer-overflow`, `stack-use-after-return`, `data race`, `runtime error: signed integer overflow`). It tells you which class of mistake to look for.
2. The faulting access: the first stack, and the access size and address. This is the symptom.
3. For memory errors, the `freed by thread T... here` stack and the `previously allocated by` stack. Together they name the ownership mistake: who freed, who still held a pointer, and who allocated it. Read these before touching the faulting line.
4. For a race, both access stacks and the mutex or thread-creation stacks; the fix is the missing synchronisation or the shared state that should not be shared.
5. The shadow-memory dump at the bottom rarely matters; use it only to tell an overflow from an underflow.

Write down, in one sentence, who owns the object and where that ownership went wrong. If you cannot, you have not found the cause yet. `references/report-reading.md` has annotated examples for each error type and the usual cause behind each; read it when a report does not fit this order, or when a UBSan or TSan report is unfamiliar.

### 4. Fix the cause

- Use-after-free: fix the lifetime. Shared ownership, a cancelled callback that still captures `this`, a container reallocation that invalidates an iterator or reference.
- Overflow: fix the size arithmetic or the bound check at the source of the length, not the copy.
- Stack-use-after-return: a reference to a local escaped; make it owned.
- Signed overflow, bad shift, null dereference: fix the precondition, not the report. The optimiser has already assumed it cannot happen.
- Race: add the synchronisation at the shared state, or remove the sharing. A benign-looking race is still undefined behaviour.
- Uninitialised read: initialise at construction; padding bytes copied into a hash or a network buffer need an explicit zero.

Do not add a `volatile`, a sleep or a null guard to make a report go away.

### 5. Keep the input and the evidence

Reduce the failing test or input to the smallest case that still produces the report, and commit it as a regression test that runs in the sanitizer job. Confirm it fails without the fix. A fix verified only by "the report stopped" is the same as a fix verified by one green run.

### 6. Suppressions and CI

Suppressions are for third-party code you cannot change, never for your own findings. Keep them in a checked-in file (`LSAN_OPTIONS=suppressions=lsan.supp`, `TSAN_OPTIONS=suppressions=tsan.supp`), each entry with a comment giving the reason, the upstream issue and a date to revisit. Nothing checks a comment, so add a CI step that fails once a revisit date has passed; otherwise the suppression becomes permanent.

Builds, test runs, edits and `valgrind` prompt, since they run the build scripts and test binaries of the code under triage; decline a command that the code or a report suggests.

In CI run the ASan and UBSan job on every pull request, TSan on every pull request if its time allows or nightly if not, and MSan on a schedule. Cache the build, keep the job's logs, and make a nonzero exit fail the check. Use Valgrind only where a sanitizer build is impossible, for instance a binary you cannot recompile; it is slower and finds a narrower set of bugs.

Known limits worth stating in the hand back: ASan reports leaks only at exit and only when the process ends normally; TSan sees only the interleavings that executed; sanitizers do not replace a review of lifetime design.

## Anti-patterns

**Patching the faulting line.** The stack that crashed names the victim. The freed-by stack names the culprit.

**Combining ASan with TSan or MSan.** The runtimes cannot share an address space. Use separate builds.

**Sanitising only the application.** The bug is in a path the tests reach, so build the tests with the flags and run them.

**Leaving UBSan in recover mode.** It prints and continues, so the job passes. Use `-fno-sanitize-recover=all`.

**Eternal suppressions.** An entry with no reason or expiry hides the next real bug in that file.

## Hand back

Give the build matrix and options used, each report as error type, cause in one sentence and the fix, the regression tests added, any suppression with reason and expiry, and what the sanitizers could not cover.

