AddressSanitizer: heap-buffer-overflow C/C++ Origin: Application & API Last updated 15 Oct 2026

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.

Exact message you may see AddressSanitizer: heap-buffer-overflowheap-buffer-overflow on addressERROR: AddressSanitizer: heap-buffer-overflowSUMMARY: AddressSanitizer: heap-buffer-overflowis located 0 bytes to the right of
Seen on: C/C++ GCC Clang

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 <= n or i < n + 1 over an array of n elements
  • C string buffer without space for the terminating \0
  • memcpy/memset with a wrong byte count (elements vs bytes)
  • vector[index] beyond size or after reallocation
  • Using malloc(strlen(s)) instead of strlen(s) + 1

⚡ Quick fix

  1. Read the first stack frame in "READ/WRITE of size" — that is the bad access
  2. Compare it with the allocation stack ("allocated by thread T0 here")
  3. Fix the bound and re-run with ASan until clean

Detailed fix by platform

GCC / Clang

  1. g++ -g -O1 -fsanitize=address -fno-omit-frame-pointer main.cpp -o app && ./app
  2. ASAN_OPTIONS=symbolize=1:detect_leaks=1 ./app

CMake

  1. target_compile_options(app PRIVATE -fsanitize=address -fno-omit-frame-pointer) and target_link_options(app PRIVATE -fsanitize=address)

Code examples

Off-by-one

cpp
#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

  1. Access — READ or WRITE, and what size?
  2. Block — How big is the allocation?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

0xC0000005 — The application was unable to start correctly (0xc0000005) / Exception code 0xc0000005 access violation (Windows)0xC0000005AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…Assertion failed — C/C++: Assertion `x > 0' failed. Aborted (core dumped) — assert() condition was false (C/C++)Assertion failedBus error — C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file (C/C++)Bus errorC2065 — MSVC: error C2065: 'x': undeclared identifier (C/C++)C2065Cannot allocate memory — fork: Cannot allocate memory / ENOMEM (Linux)Cannot allocate memorydouble free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…GLIBCXX not found — Linux: version `GLIBCXX_3.4.30' not found (required by ./app) — libstdc++.so.6 (C/C++)GLIBCXX not foundAddressSa…

Explore the map of this error Full error map →

🧠 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.