C/C++: runtime error: signed integer overflow / null pointer / shift exponent too large (UndefinedBehaviorSanitizer)
UBSan detected code that triggers undefined behaviour (overflow, null dereference, out-of-bounds index, bad shift, misaligned access) — the program may appear to work and still be wrong.
runtime error: signed integer overflowruntime error: member access within null pointerruntime error: shift exponentruntime error: index out of boundsruntime error: load of misaligned address Meaning
Undefined behaviour lets the optimiser assume it never happens, so bugs surface only at -O2, on other compilers, or after a compiler upgrade. UBSan reports file:line:col: runtime error: <what>. Fix each report rather than silencing it.
Common causes
- Signed integer overflow (
INT_MAX + 1) - Null pointer dereference or member access through null
- Array index out of bounds or shift by a negative or too large count
- Misaligned access, invalid enum or bool value
- Division by zero or
INT_MIN / -1
⚡ Quick fix
- Compile with
-fsanitize=undefinedand run your tests - Use wider or unsigned types, or check ranges before arithmetic
- Add bounds and null checks
Detailed fix by platform
GCC / Clang
g++ -g -fsanitize=undefined -fno-sanitize-recover=all main.cppstops at the first report- Combine with address:
-fsanitize=address,undefined
CMake
target_compile_options(app PRIVATE -fsanitize=undefined)andtarget_link_options(app PRIVATE -fsanitize=undefined)
Code examples
Checked arithmetic
#include <climits>
#include <iostream>
int add_checked(int a, int b) {
if ((b > 0 && a > INT_MAX - b) || (b < 0 && a < INT_MIN - b)) {
throw std::overflow_error("int overflow"); // instead of undefined behaviour
}
return a + b;
}
int main() { std::cout << add_checked(INT_MAX, 1) << "\n"; }How to diagnose
- Message — Which kind of undefined behaviour?
- Line — file:line:col from the report?
- Inputs — Which values trigger it?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- Assertion failed C/C++: Assertion `x > 0' failed. Aborted (core dumped) — assert() condition was false
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026