C++

Reading an AddressSanitizer Heap-Buffer-Overflow — READ vs WRITE, Traced and Fixed

AddressSanitizer stops a program at the first memory access that leaves an allocation, and prints a report that names the bad access, the line that made it, and the allocation it escaped from. Most readers look at the first line, see heap-buffer-overflow, and start guessing. The report already contains the answer: the access line says whether the bug is a read or a write and how wide it is, the fault stack says which line ran, and the region line says how far past the end the access landed — far enough, in the common off-by-one case, to name the exact index. This guide takes one off-by-one write past a heap array and reads its report all the way down, then runs the same bug as a read, then fixes the bound.

Written November 19, 2026 · Tested with GCC 13.3.0 · 22 min read

By the end of this guide
  • The program: one write past the end
  • Building with the sanitizer
  • The WRITE report
  • Reading the report, line by line

What this guide is

A heap-buffer-overflow is an access that lands outside a heap allocation: past its end, in the programs in this guide. The examples here use GCC 13.3.0 and its AddressSanitizer runtime. The main case is the smallest version of the bug that shows up in real code: an array of four ints allocated with new, filled correctly, and then written at index 4 — one element past the last valid index, 3. The sanitizer catches that write, and the guide reads the resulting report in full: the access line, the fault stack, the line that locates the faulting address against the allocation, the allocation stack, and the shadow bytes at the end. A second program makes the same mistake as a read instead of a write, so the two report forms sit side by side and the difference between them is exactly one word. A third run moves the bad index further out, to 6, to show how the location line encodes the distance. The fix corrects the bound, and the fixed program runs to a normal exit under the same sanitizer build.

Nothing in this guide needs a debugger or a core dump. The report is the diagnosis; the work is learning which lines of it to trust and in what order.

The program: one write past the end

C++
#include <iostream>

int main() {
    int* values = new int[4];
    for (int i = 0; i < 4; ++i) {
        values[i] = i * 10;
    }
    values[4] = 99;
    std::cout << values[4] << "\n";
    delete[] values;
    return 0;
}

Read it as two halves. The first half is correct: new int[4] allocates four ints, and the loop fills indices 0 through 3 with 0, 10, 20, and 30. The second half steps outside. values[4] = 99; writes where a fifth element would be if the array had one. It does not. The valid indices of a four-element array are 0, 1, 2, and 3, and index 4 is the first address that belongs to something else — or, under the sanitizer, to nothing the program is allowed to touch. The line after the bad write reads the same index back and prints it, and the delete[] after that is written as if both had succeeded. Neither gets the chance: the write is where the run ends.

This shape — a correct fill loop followed by one access written against a different count — is why off-by-one overflows survive code review. Every line looks locally reasonable. The loop says 4, the allocation says 4, and the stray access says 4 as well, but it means it as an index, where the largest legal value is 3. The report below is what separates those two meanings.

Building with the sanitizer

AddressSanitizer is not a separate program that watches the binary from outside. It is instrumentation the compiler adds to the program itself: memory access instructions are instrumented to detect out-of-bounds and use-after-free bugs, and a runtime library checks each access against a record of which addresses are currently valid. That is why the sanitizer is a compile-time choice, made with one flag:

Command
$ g++ -std=c++20 -g -fsanitize=address -o overflow_write overflow_write.cpp

The -fsanitize=address flag does two jobs at once: it compiles the checks into the code and it links the sanitizer runtime into the binary. Leave it off either step and there is nothing to catch the access — the last-but-one section of this guide runs exactly that build, and the overflow sails through it. The -g flag is the other half of a useful report. The GCC documentation puts it plainly: when compiling with -fsanitize=address, you should also use -g to produce more meaningful output. Debug information is what lets the runtime print a file name and a line number next to each stack frame instead of a bare offset into the binary. A later section builds the same program without -g so the difference is visible rather than asserted: the overflow is still caught, the region line is identical, and the stacks lose every file and line reference your own code had.

One consequence of the instrumentation model is worth stating before the report: the sanitizer knows the size of every allocation because the runtime replaces the allocation functions and records each region as it is created, with poisoned redzones on either side that no legal access may touch. An access is checked before it happens. When the check fails, the access never completes — the value is not written, the read value is never produced — and the program stops at that instruction and prints the report. So a sanitizer report is not a description of corruption that already spread. It is a photograph of the first illegal access, taken before it landed.

The WRITE report

Run the instrumented program and it stops on the bad write:

Terminal
$ ./overflow_write
=================================================================
==35622==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000020 at pc 0x61213ffbf332 bp 0x7ffd8b0b1de0 sp 0x7ffd8b0b1dd0
WRITE of size 4 at 0x502000000020 thread T0
    #0 0x61213ffbf331 in main /home/hatch/workspace/slot4-verify/overflow_write.cpp:8
    #1 0x73579857e1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #2 0x73579857e28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x61213ffbf1a4 in _start (/home/hatch/workspace/slot4-verify/overflow_write+0x11a4) (BuildId: aa256668faf23d57d2c08a360f6ef3affade2da8)

0x502000000020 is located 0 bytes after 16-byte region [0x502000000010,0x502000000020)
allocated by thread T0 here:
    #0 0x735798bfa6c8 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:98
    #1 0x61213ffbf27e in main /home/hatch/workspace/slot4-verify/overflow_write.cpp:4
    #2 0x73579857e1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x73579857e28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #4 0x61213ffbf1a4 in _start (/home/hatch/workspace/slot4-verify/overflow_write+0x11a4) (BuildId: aa256668faf23d57d2c08a360f6ef3affade2da8)

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/hatch/workspace/slot4-verify/overflow_write.cpp:8 in main
Shadow bytes around the buggy address:
  0x501ffffffd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501ffffffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x502000000000: fa fa 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa
  0x502000000080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==35622==ABORTING

The process exits with status 1, and the 99 the program meant to print never appears: execution stopped at the write on line 8, two lines before the print statement. The report is printed to standard error, and it is long. Most of its length is the two stacks and the shadow dump. The diagnosis lives in five places, taken in order below.

Reading the report, line by line

The header: what kind of error, at what address

The first line after the rule names the error class and the address that was touched:

Output
==35622==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000020 at pc 0x61213ffbf332 bp 0x7ffd8b0b1de0 sp 0x7ffd8b0b1dd0

heap-buffer-overflow is the class: an access outside a heap allocation. It is not a segmentation fault and it is not a guess — the runtime knows the address 0x502000000020 sits in a poisoned redzone next to a live allocation, which is a stronger statement than the operating system can make about a wild pointer. The number before ERROR is the process ID, and the addresses on this line and throughout the report — the program counter, the stack pointers, the heap addresses themselves — change from run to run. They are the least portable part of the report. Everything structural in it — the error class, the access kind, the sizes, the file names and line numbers, the region size — is stable, and those are the parts worth reading closely.

The access line: WRITE of size 4

Output
WRITE of size 4 at 0x502000000020 thread T0

This is the line the guide's title points at. It says the faulting access was a write, and that it was 4 bytes wide. Four bytes is the size of one int on this platform, so the access was a single-element store, not a bulk copy: something wrote one int to an address outside the allocation. That already narrows the search before any stack is read. A write of size 4 at the boundary of an int array points at an element assignment, a single index — not a bulk copy gone wrong, and not a pointer increment gone wrong in a loop over a wider type. When you read a report of your own, this line is where you learn what shape of statement to look for: the access kind tells you whether the statement stored or loaded, and the size tells you the width of the thing it moved.

The fault stack: the line that ran

Directly under the access line is the stack of the faulting access:

Output
    #0 0x61213ffbf331 in main /home/hatch/workspace/slot4-verify/overflow_write.cpp:8

Frame #0 is the instruction that was checked and stopped: main, in overflow_write.cpp, at line 8. Line 8 of the program as printed above is values[4] = 99; — the stray write. The frames below it are the call chain that reached main — the C runtime's startup code — and for a single-function program they carry no diagnostic weight. In a larger program this stack is the part to read most carefully, because it is the actual path of execution: the top frame is the access itself, and each frame below is a caller, so the stack answers not just where the bad index was used but which call chain delivered it. Note what makes this frame readable at all: the file name and the :8. Those come from -g. The same report built without debug information is shown further down, and this frame is the part that degrades.

The region line: 0 bytes after a 16-byte region

After the fault stack, the report locates the faulting address against the allocation it escaped:

Output
0x502000000020 is located 0 bytes after 16-byte region [0x502000000010,0x502000000020)

This line does arithmetic, and it is worth doing the same arithmetic yourself. The region is 16 bytes: the allocation was four ints at 4 bytes each, and 4 × 4 = 16. The region runs from 0x502000000010 up to but not including 0x502000000020 — that is what the half-open bracket notation means, and it matches the valid indices exactly: the bytes of elements 0, 1, 2, and 3 occupy the addresses from the region's start through 0x50200000001f. The faulting address is 0x502000000020, the region's end, and the report calls it 0 bytes after the region: the first byte that is not part of the allocation. Divide the distance from the region's start by the element size — (0x20 − 0x10) / 4 = 4 — and the index falls out: the access was aimed at element 4. Zero bytes after the end of an array is the signature of the classic off-by-one. The access was not wild, and it was not far away; it was one element too far, which is why the program looked correct and why, without the sanitizer, it often appears to work.

The general rule this line teaches: the distance it reports, added to the region's size and divided by the element size, gives the index that was accessed. A report that says 0 bytes after a 16-byte region for a 4-byte element means index 4. The section after the READ variant pushes the index to 6 and shows the same line reporting 8 bytes after, which is (6 − 4) × 4 — the two extra elements, in bytes.

The allocated-by stack: where the region came from

The report then prints a second stack, headed allocated by thread T0 here:. Its top frame is the runtime's replacement allocation function, operator new[](unsigned long), and the frame below it is the line in your code that called it:

Output
    #1 0x61213ffbf27e in main /home/hatch/workspace/slot4-verify/overflow_write.cpp:4

Line 4 is int* values = new int[4];. So the report ties the two ends of the bug together in one document: the allocation that created a 16-byte region on line 4, and the access that stepped past it on line 8. In a real program these two lines are the pair to compare. The allocated-by stack tells you which allocation the address belonged to — which container, which buffer, which owner — and the fault stack tells you who touched it out of bounds. When the two are in different parts of a codebase, the bug is usually a size or a count that was computed in one place and assumed in another, and the report has just named both places.

The summary and the shadow bytes

The report closes with a one-line SUMMARY: that repeats the error class and the fault location — the line to paste into a bug report or a search box — and a dump of shadow bytes around the faulting address. The shadow is the runtime's validity map: one shadow byte describes every 8 bytes of program memory, with 00 meaning fully addressable and fa marking a poisoned heap redzone. The row that contains the fault is marked with =>:

Output
=>0x502000000000: fa fa 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa

Read it left to right: two poisoned shadow bytes for the redzone before the allocation, then 00 00 for the 16 addressable bytes of the region itself, then the bracketed [fa] — the shadow byte covering the faulting address — poisoned, as it should be, because the address sits in the redzone after the region. The shadow dump rarely changes the diagnosis the earlier lines already gave, but it confirms the geometry: the allocation ends exactly where the region line said it ends, and the byte the program reached for was the first poisoned one past it. The final line, ==35622==ABORTING, is the runtime ending the process after the report; the program does not continue past a failed check.

The same bug as a READ

Change the stray write into a stray read — same allocation, same fill loop, same index — and the program is:

C++
#include <iostream>

int main() {
    int* values = new int[4];
    for (int i = 0; i < 4; ++i) {
        values[i] = i * 10;
    }
    int extra = values[4];
    std::cout << extra << "\n";
    delete[] values;
    return 0;
}

Compiled with the same command and run, it produces this report:

Terminal
$ g++ -std=c++20 -g -fsanitize=address -o overflow_read overflow_read.cpp
$ ./overflow_read
=================================================================
==35642==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000020 at pc 0x604245c0132e bp 0x7ffe14291240 sp 0x7ffe14291230
READ of size 4 at 0x502000000020 thread T0
    #0 0x604245c0132d in main /home/hatch/workspace/slot4-verify/overflow_read.cpp:8
    #1 0x7af6765b01c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #2 0x7af6765b028a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x604245c011a4 in _start (/home/hatch/workspace/slot4-verify/overflow_read+0x11a4) (BuildId: 71f72dd85d505995ddb2616b2b74b1de84847a0e)

0x502000000020 is located 0 bytes after 16-byte region [0x502000000010,0x502000000020)
allocated by thread T0 here:
    #0 0x7af676c2c6c8 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:98
    #1 0x604245c0127e in main /home/hatch/workspace/slot4-verify/overflow_read.cpp:4
    #2 0x7af6765b01c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x7af6765b028a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #4 0x604245c011a4 in _start (/home/hatch/workspace/slot4-verify/overflow_read+0x11a4) (BuildId: 71f72dd85d505995ddb2616b2b74b1de84847a0e)

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/hatch/workspace/slot4-verify/overflow_read.cpp:8 in main
Shadow bytes around the buggy address:
  0x501ffffffd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501ffffffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x502000000000: fa fa 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa
  0x502000000080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==35642==ABORTING

It also exits with status 1, and the value it meant to print never appears. Put the two reports next to each other and the comparison is short. The header is unchanged in kind: same error class, heap-buffer-overflow, because the class describes where the address sits, not what the access tried to do with it. The region line is identical word for word apart from the addresses: 0 bytes after the same 16-byte region, because the allocation and the index did not change. The allocated-by stack is the same allocation at the same line 4. The shadow row is the same shape, because the memory layout is the same.

What changes diagnostically is the access line:

Output
READ of size 4 at 0x502000000020 thread T0

READ where the first report said WRITE. The size is still 4 — the same element, loaded instead of stored. The fault stack still lands on line 8, which in this file is int extra = values[4];. Apart from the file name in the stacks — the read program is a separate file — that one word is the whole difference between the two reports, and it is a real diagnostic difference, not a cosmetic one. A WRITE overflow corrupts whatever lives past the allocation; in an unsanitized build it can damage other data silently and crash somewhere unrelated, much later. A READ overflow corrupts nothing; it consumes a value that was never the program's to read — a redzone's contents under the sanitizer, another object's bytes or heap bookkeeping without it — and every wrong result it produces flows from that one fabricated input. When you triage a report, the access line tells you which failure mode you are hunting: for a write, find the store and fix its index; for a read, find the load, fix its index, and then also ask what the program did with the values it read on the runs before the sanitizer was switched on, because those results were built on memory outside the array.

One more thing stays the same, and it is the point of running the read variant at all: the fix does not change. A read at index 4 and a write at index 4 are the same bound broken in the same place, and both are fixed by correcting the bound, below.

How far past the end: reading the distance

The region line's distance is in bytes, and converting it to an index is the fastest way to tell an off-by-one from a badly computed offset. Change only the index in the write program, from 4 to 6, and the top of the report becomes:

Terminal
$ g++ -std=c++20 -g -fsanitize=address -o overflow_far overflow_far.cpp
$ ./overflow_far
=================================================================
==35691==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000028 at pc 0x5be27410b332 bp 0x7fff61e688b0 sp 0x7fff61e688a0
WRITE of size 4 at 0x502000000028 thread T0
    #0 0x5be27410b331 in main /home/hatch/workspace/slot4-verify/overflow_far.cpp:8
    #1 0x7eca4c9a51c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #2 0x7eca4c9a528a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x5be27410b1a4 in _start (/home/hatch/workspace/slot4-verify/overflow_far+0x11a4) (BuildId: cef24a8902cc3985b966efa82cbc5f70cfeaee5c)

0x502000000028 is located 8 bytes after 16-byte region [0x502000000010,0x502000000020)

The faulting address moved from the region's end to 8 bytes past it, and the arithmetic names the index directly: the region holds 4 elements, the access landed (16 + 8) / 4 = 6 elements from the region's start, so the index was 6. The pattern generalizes to any element size. Distance 0 with an element size of 4 means the first invalid index. A distance equal to one element means the second invalid index. A large distance — hundreds of bytes past a small region — means the index was not off by one at all; it was computed from something unrelated, an uninitialized variable, a length in the wrong units, a count of bytes used as a count of elements. The region line separates those cases before any code is read: small distances are bound errors, large distances are computation errors, and they are fixed in different places.

What -g buys: the same report without line numbers

The GCC advice to pair -fsanitize=address with -g is easy to test. Build the original write program with the sanitizer but without debug information:

Terminal
$ g++ -std=c++20 -fsanitize=address -o overflow_write_nog overflow_write.cpp
$ ./overflow_write_nog
=================================================================
==35662==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000020 at pc 0x614502625332 bp 0x7ffedb774cb0 sp 0x7ffedb774ca0
WRITE of size 4 at 0x502000000020 thread T0
    #0 0x614502625331 in main (/home/hatch/workspace/slot4-verify/overflow_write_nog+0x1331) (BuildId: 3fe0a29ee12eeb869c96a03d86a803aefc73370a)
    #1 0x7efe3a13c1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #2 0x7efe3a13c28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x6145026251a4 in _start (/home/hatch/workspace/slot4-verify/overflow_write_nog+0x11a4) (BuildId: 3fe0a29ee12eeb869c96a03d86a803aefc73370a)

0x502000000020 is located 0 bytes after 16-byte region [0x502000000010,0x502000000020)
allocated by thread T0 here:
    #0 0x7efe3a7b86c8 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:98
    #1 0x61450262527e in main (/home/hatch/workspace/slot4-verify/overflow_write_nog+0x127e) (BuildId: 3fe0a29ee12eeb869c96a03d86a803aefc73370a)
    #2 0x7efe3a13c1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x7efe3a13c28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #4 0x6145026251a4 in _start (/home/hatch/workspace/slot4-verify/overflow_write_nog+0x11a4) (BuildId: 3fe0a29ee12eeb869c96a03d86a803aefc73370a)

The overflow is still caught — detection does not depend on debug information, because the checks are compiled into the code and the region records live in the runtime. The region line is identical: 0 bytes after the same 16-byte region. What is gone is everything that tied the report to the source. Frame #0 is now main with an offset into the binary, overflow_write_nog+0x1331, and no file or line; the allocated-by stack's frame for your code is the same shape, an offset instead of overflow_write.cpp:4; and the SUMMARY: line repeats the offset form. The report still tells you the program wrote 4 bytes just past a 16-byte heap region, but it no longer tells you where. In a one-function program you could recover the line by disassembling the offset; in a real program, that recovery work is the difference between a report that is a diagnosis and a report that is a starting point for a debugging session. Build sanitizer binaries with -g. The cost is a larger binary; the benefit is that every frame in your own code arrives already resolved to the line that ran.

Without the sanitizer, there is no report

Build the original write program with neither flag and run it:

Terminal
$ g++ -std=c++20 -g -o overflow_write_plain overflow_write.cpp
$ ./overflow_write_plain
99

It prints 99 and exits with status 0. The write to index 4 landed somewhere — in heap space the allocator manages, past the four ints the program asked for — and nothing checked it, so the program read the value back, printed it, freed the array, and reported success. This is the run that makes the bug expensive in production: not a crash, but a plausible result. Writing past a heap allocation is undefined behavior, which means this outcome is one possibility among many, not a behavior to rely on: a different allocator, a different allocation size, a different optimization level, or a neighboring allocation with live data in the wrong place can turn the same line into corrupted state or a crash far from the cause. The sanitizer build exists to move that discovery to the first bad access, in a test run, with the line number attached. A program that passes its tests in a plain build has shown that its outputs looked right; a program that passes the same tests in a sanitizer build has shown that its accesses stayed inside its allocations while producing them. Those are different claims, and only the second one covers this bug class.

The fix: correct the bound

The bug is that an index of 4 was used against an allocation of 4 elements. The fix is to make every access respect the bound the allocation sets — indices 0 through 3 — and to write the program so the bound appears as one fact the accesses share, rather than a number repeated from memory at each use. The fixed program keeps the allocation and the fill loop, removes the stray write, and does its remaining work inside the same loop bound:

C++
#include <iostream>

int main() {
    int* values = new int[4];
    for (int i = 0; i < 4; ++i) {
        values[i] = i * 10;
    }
    int total = 0;
    for (int i = 0; i < 4; ++i) {
        total += values[i];
    }
    std::cout << total << "\n";
    delete[] values;
    return 0;
}

Compiled the same way as the first program and run under the sanitizer:

Terminal
$ g++ -std=c++20 -g -fsanitize=address -o fixed fixed.cpp
$ ./fixed
60

It prints 60 — 0 + 10 + 20 + 30 — and exits with status 0, with no report. A clean sanitizer run is quiet: the absence of a report, with a normal exit and the expected output, is the pass condition. Two things about the fix are worth more than the diff. First, the fix is at the access, not at the allocation: the program needed four elements, and enlarging the allocation to five would have silenced this report while leaving the logic — an index the data never justified — in place, waiting for the next index to step past the new end. Grow an allocation only when the program genuinely needs the extra element. Second, the read variant is fixed by the same correction, because it was the same bound: any version of this program that keeps every index below 4 is correct under both report forms, and any version that lets an index reach 4 is wrong under both, whether that index is read or written.

In code larger than this example, the same fix usually takes one of three shapes. Keep the count in one place and derive every bound from it, so the allocation and the loops cannot drift apart when the count changes. Prefer a container that knows its own size — a std::vector or a std::array — and loop over its size() or with a range-based loop, so the bound travels with the object instead of living in a separate variable. And where an index arrives from outside the function — a parameter, a parsed value, user input — check it against the size at the point of use and handle the rejection there, because a bound that is only maintained by convention at every call site will eventually be broken by one of them.

Reading any heap-buffer-overflow report

The same reading order works on reports from larger programs, where the stacks are longer and the region is one of thousands:

Report line What it tells you Where to go in the code
heap-buffer-overflow in the header The address sits outside a heap allocation, in a redzone The allocation named by the allocated-by stack
READ of size N / WRITE of size N Whether the access loaded or stored, and the width of one access A statement moving an N-byte value at the fault line
Fault stack frame #0 The exact line whose access was stopped That line's index or pointer arithmetic
located K bytes after an M-byte region Distance past the end; (M + K) / element size is the index accessed The bound the fault line used, compared with the allocation size
Allocated-by stack Which allocation the address belonged to, and the line that created it Where the region's size was decided
Shadow row with [fa] bracketed The faulting byte is poisoned redzone, confirming the geometry Confirmation only; the lines above already carry the diagnosis
  • Start at the access line, not the header: READ versus WRITE and the access size shape the search before any stack is read.
  • Take the index from the region line's arithmetic and compare it with the index expression on the fault line; when they agree, you have the right line and the wrong bound.
  • Compare the fault line with the allocated-by line: if the size was decided in one function and the access made in another, look for the count that traveled between them — that hand-off is where the two stopped agreeing.
  • Treat a small distance past the end as a bound error and a large one as a computation error; they are fixed in different places.
  • Fix the access or its bound, not the report: enlarging the allocation, padding the region, or suppressing the finding all leave the index wrong.
  • Re-run the same sanitizer build after the fix; the pass condition is a normal exit, the expected output, and no report.
  • Sanitizer report decoder — paste a sanitizer report and get its parts labeled: the access line, the stacks, and the region line this guide reads by hand.
  • C++ Arrays and std::array — the array types behind this example, including the fixed-size std::array, whose size is part of the type.
  • C++ Pointers and References — what a pointer like values actually holds, and why indexing it is address arithmetic against an allocation with a fixed size.

Sources

  • GCC, Instrumentation Options — -fsanitize=address enables AddressSanitizer, a fast memory error detector; memory access instructions are instrumented to detect out-of-bounds and use-after-free bugs, and when compiling with -fsanitize=address you should also use -g to produce more meaningful output.
  • AddressSanitizer wiki, AddressSanitizer — AddressSanitizer is a memory error detector for C and C++ that finds heap buffer overflows among its listed error classes, and consists of a compiler instrumentation module and a runtime library.
  • AddressSanitizer wiki, Heap out-of-bounds example — a worked heap-buffer-overflow report of the same form as this guide's: the access line, the fault stack, the line locating the address against its region, the allocated-by stack, and the shadow bytes.
  • AddressSanitizer wiki, AddressSanitizerAlgorithm — how the runtime maps 8 bytes of application memory to 1 byte of shadow memory, and how poisoning a shadow byte marks memory the program may not access.

Keep Exploring