C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
AddressSanitizer caught a read or write just outside a heap block — an array index or length that is off by one or more.
AddressSanitizer: heap-buffer-overflowheap-buffer-overflow on addressERROR: AddressSanitizer: heap-buffer-overflowSUMMARY: AddressSanitizer: heap-buffer-overflowis located 0 bytes to the right of Meaning
ASan places "red zones" around every heap allocation. The report gives the access type and size, the stack of the faulty access, and where the block was allocated. Typical sources are <= loop bounds, forgetting the trailing NUL of C strings, wrong memcpy lengths and std::vector::operator[] past size().
Common causes
- Loop
i <= nori < n + 1over an array of n elements - C string buffer without space for the terminating
\0 memcpy/memsetwith a wrong byte count (elements vs bytes)vector[index]beyond size or after reallocation- Using
malloc(strlen(s))instead ofstrlen(s) + 1
⚡ Quick fix
- Read the first stack frame in "READ/WRITE of size" — that is the bad access
- Compare it with the allocation stack ("allocated by thread T0 here")
- Fix the bound and re-run with ASan until clean
Detailed fix by platform
GCC / Clang
g++ -g -O1 -fsanitize=address -fno-omit-frame-pointer main.cpp -o app && ./appASAN_OPTIONS=symbolize=1:detect_leaks=1 ./app
CMake
target_compile_options(app PRIVATE -fsanitize=address -fno-omit-frame-pointer)andtarget_link_options(app PRIVATE -fsanitize=address)
Code examples
Off-by-one
#include <cstring>
#include <cstdlib>
int main() {
const char* s = "hello";
char* copy = static_cast<char*>(std::malloc(std::strlen(s))); // no room for '\0'
std::strcpy(copy, s); // heap-buffer-overflow WRITE of size 6
std::free(copy);
// fix: std::malloc(std::strlen(s) + 1)
}How to diagnose
- Access — READ or WRITE, and what size?
- Block — How big is the allocation?
- Bound — Which index or length is wrong?
🔧 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
- 0xC0000005 The application was unable to start correctly (0xc0000005) / Exception code 0xc0000005 access violation
- 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
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
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