Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

Sanitizers: AddressSanitizer, UBSan and ThreadSanitizer

Find memory errors, undefined behavior and data races with AddressSanitizer, UBSan, ThreadSanitizer and MSan: flags, sample reports and safe code to compare.

A sanitizer is a compiler feature that adds checks to your program while it is built, so bugs that normally corrupt memory silently stop with a clear report: the kind of error, the source line, and often the line that caused it earlier. They find real bugs in real programs, and they are the fastest way to investigate a crash that only happens sometimes.

GCC and Clang provide several, selected with -fsanitize=. They slow your program down (about 2x for AddressSanitizer, 5-15x for ThreadSanitizer) and use more memory, so you use them in test and CI builds, not in production releases.

Terminal
g++ -std=c++23 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer main.cpp -o app
./app

Our online runner does not link the sanitizer runtimes, so the commands and reports on this page are read-only. The live examples below show the safe version of each bug, which is what the sanitizer pushes you toward.

Try it

C++ Checked access instead of undefined behavior

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ The race-free counter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The sanitizers

Flag Finds Typical cost
-fsanitize=address Out-of-bounds access on heap, stack and globals, use after free, use after return, double free, leaks (Linux, via LeakSanitizer). ~2x time, ~2-3x memory
-fsanitize=undefined Signed integer overflow, shifts out of range, null pointer use, misaligned access, bad bool/enum values, reaching unreachable, float-to-int overflow. Small
-fsanitize=thread Data races between threads, some lock-order inversions. ~5-15x time, ~5-10x memory
-fsanitize=memory (Clang only) Reads of uninitialized memory. ~3x time
-fsanitize=leak Memory that is still allocated at exit. Almost none

address and thread cannot be combined in one build. address and undefined are designed to work together.

What a report looks like

A program that reads past the end of a vector:

C++
#include <vector>
int main() {
    std::vector<int> v{1, 2, 3};
    return v[3];            // one past the end
}

prints something like:

TEXT
==1234==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000001fc ...
READ of size 4 at 0x6020000001fc thread T0
    #0 0x... in main main.cpp:4
...
0x6020000001fc is located 0 bytes after 12-byte region [0x6020000001f0,0x6020000001fc)
allocated by thread T0 here:
    #0 0x... in operator new(unsigned long)
    #1 0x... in std::vector<int>::vector(std::initializer_list<int>) ...

Read it from the top: the error kind, the faulting line, then where the memory came from. A data-race report from ThreadSanitizer names both conflicting accesses, with a stack for each.

Usage notes

  • Build with -g and -fno-omit-frame-pointer. Without them, reports show addresses instead of line numbers.
  • Use -O1. It keeps the checks fast while preserving readable stack traces. Optimizing too hard can hide or reshape bugs.
  • A clean run proves little. Sanitizers only check code paths that run. Combine them with good tests, fuzzing (-fsanitize=fuzzer in Clang) and varied inputs.
  • Build everything with the same flags. A library compiled without instrumentation hides bugs inside it, and instrumented and uninstrumented code can disagree in odd ways.
  • Make UBSan abort in tests. Set -fno-sanitize-recover=undefined, otherwise it prints and continues, and the failure is easy to miss.
  • Use environment options such as ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 to tune behavior without recompiling.
  • Run tests under each sanitizer in CI. One job for address,undefined and one for thread covers most memory, UB and concurrency bugs.
  • Standard library checks complement sanitizers. -D_GLIBCXX_ASSERTIONS (cheap) and -D_GLIBCXX_DEBUG (thorough, changes the ABI) check container bounds and iterator validity in libstdc++.
  • MSVC supports AddressSanitizer with /fsanitize=address. It does not provide the other sanitizers.