Developer knowledge network ยท moderated exchange

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Knowledge indexLive
4Categories
919Threads
2.8KPosts
Discussion

Detecting subtle Undefined Behavior with AddressSanitizer (ASan) and UndefinedBehaviorSanitizer (UBSan) [StackOverflow Architecture Guide]

sanitizer_sam
UB Hunter
MEMBER
Rep: 119
Join Date: Apr 2021
Posts: 10
Thanks: 53
1 months ago ยท Jun 30, 2026 11:44 PM
#1

How to enable compiler sanitizers in GCC and Clang for zero-cost runtime bug discovery:

BASH
g++ -O1 -g -fsanitize=address,undefined,leak -fno-omit-frame-pointer main.cpp -o main

What Sanitizers Catch Instantly:

  • Out-of-bounds heap / stack / global array access
  • Use-After-Free and Double-Free errors
  • Signed integer overflow (UB in C/C++)
  • Null pointer dereferences and misaligned pointer casts
  • Memory leaks upon process exit with precise file and line numbers!
llvm_compiler_dev
LLVM & Clang Hacker
MEMBER
Rep: 139
Join Date: Jul 2018
Posts: 21
Thanks: 15
1 months ago ยท Jul 1, 2026 3:57 AM
#2

AddressSanitizer replaced 90% of our Valgrind usage. It runs at only ~2x slowdown compared to Valgrind's 20x slowdown, making it feasible to run during full test suites.

profiler_pat
Performance Hunter
MEMBER
Rep: 146
Join Date: Aug 2019
Posts: 33
Thanks: 31
1 months ago ยท Jul 1, 2026 2:51 PM
#3

Running UBSan on legacy codebases always exposes unexpected signed overflows in integer math routines.