Learning center

Which stack in an AddressSanitizer report points at the bug?

Read the free stack before the access stack: a use-after-free is usually fixed where the memory was released or where a pointer outlived it. Dismiss a report only after a fully instrumented rebuild no longer produces it.

Read the three stacks in order#

An AddressSanitizer (ASan) heap report has up to three stack traces. The first shows the access that touched bad memory. For a use-after-free it is followed by the stack that freed the memory, then the stack that allocated it. The access line names the symptom. The code that needs changing is usually in the second stack, or in the code between the second and the first that kept a pointer across the free.

The program below keeps a reference to a vector element and then adds another row. It is a constructed example, built with Ubuntu clang 18.1.3 and GCC 13's libstdc++:

#include <cstdio>
#include <string>
#include <vector>

struct Row { std::string name; int score; };

class Table {
 public:
  const Row& add(std::string name, int score) {
    rows_.push_back({std::move(name), score});
    return rows_.back();
  }
 private:
  std::vector<Row> rows_;
};

int main() {
  Table t;
  const Row& first = t.add("alice", 10);
  t.add("bob", 20);                 // may reallocate rows_
  std::printf("%d\n", first.score); // line 21
}

Built without ASan, by clang at -O1 or GCC at -O0, it prints 10 and exits 0, so an ordinary test passes. Built with clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer cache.cc, it stops with this report (addresses and template names shortened, standard library frames trimmed):

ERROR: AddressSanitizer: heap-use-after-free on address 0x... at pc 0x...
READ of size 4 at 0x... thread T0
    #0 0x... in main cache.cc:21:29

0x... is located 32 bytes inside of 40-byte region [0x...,0x...)
freed by thread T0 here:
    #0 0x... in operator delete(void*)
    #4 0x... in std::vector<Row>::_M_realloc_insert<Row>(...) vector.tcc:519:7
    #6 0x... in std::vector<Row>::push_back(Row&&) stl_vector.h:1299:9
    #7 0x... in Table::add(std::string, int) cache.cc:10:11
    #8 0x... in main cache.cc:20:5

previously allocated by thread T0 here:
    #0 0x... in operator new(unsigned long)
    #4 0x... in std::vector<Row>::_M_realloc_insert<Row>(...) vector.tcc:459:33
    #7 0x... in Table::add(std::string, int) cache.cc:10:11
    #8 0x... in main cache.cc:19:24

Line 21 only reads a field. The free stack shows push_back reallocating the vector's storage during the second add call, on line 20, which released the 40-byte block that the first add call allocated on line 19. The line "32 bytes inside of 40-byte region" places the read at score, the last field of a Row. The defect is the API: add returns a reference whose lifetime ends at the next add. Changing line 21 cannot fix that. Returning an index or a copy, or storing rows in a container whose elements do not move, can.

Two habits keep this reading reliable. Build with -g and -fno-omit-frame-pointer, as the clang documentation recommends, or the free and allocation stacks lose frames. And keep the first report: ASan "exits on the first detected error" by design, because "once a memory corruption occurs, the program is in an inconsistent state, which could lead to confusing results and potentially misleading subsequent reports."

Treat a report as true until a rebuild says otherwise#

The same documentation sets the default: "AddressSanitizer is not expected to produce false positives. If you see one, look again; most likely it is a true positive!" Heap-use-after-free, heap-buffer-overflow and double-free reports come from the runtime's own bookkeeping, which its replacement malloc and free update on every call, so they rarely mislead in a fully instrumented program.

The known exception is a program that is only partly instrumented. The sanitizer project's container-overflow page describes it: "If a part of the application is built with asan and another part is not instrumented, and both parts use e.g. instrumented std::vector, asan may report non-existent container overflow." GCC's libstdc++ makes the same requirement for its annotations, enabled by _GLIBCXX_SANITIZE_VECTOR: they "must be present on all vector operations or none, so this macro must be defined to the same value for all translation units that create, destroy or modify vectors." libstdc++ manual, GCC 13.3.0

This constructed pair reproduces that report with GCC 13.3.0. fill.cc stands in for a library built without ASan:

// fill.cc, built with: g++ -O1 -g -c fill.cc
#include <vector>
void fill(std::vector<long>& v) { v.push_back(42); }

// main.cc, built with: g++ -O1 -g -fsanitize=address -D_GLIBCXX_SANITIZE_VECTOR -c main.cc
#include <cstdio>
#include <vector>
void fill(std::vector<long>& v);
int main() {
  std::vector<long> v;
  v.reserve(4);
  fill(v);
  std::printf("%zu %ld\n", v.size(), v[0]);  // line 8
}

Linked together, the program reports container-overflow on line 8 and exits 1, although v holds one element and v[0] is in bounds. The instrumented reserve marked the spare capacity as poisoned; the uninstrumented push_back wrote an element without updating that marking. Two runs settle it. With ASAN_OPTIONS=detect_container_overflow=0 the same binary prints 1 42, which only shows that the container check produced the report, since that option silences real container overflows too. Rebuilt from both files with ASan and the macro, it prints 1 42 with no report. A report that disappears in the fully instrumented rebuild is the mixed-build false positive; one that survives that rebuild is a real bug.

The disconfirming evidence is narrow. It applies to container-overflow reports, not to use-after-free or buffer-overflow reports on ordinary heap blocks, and it needs a build where some code that touches the container was compiled without ASan. A prebuilt library, a vendored static archive or a plugin is the usual source. If every object in the link was built with the same sanitizer flags, keep the report.

Decide from what the rebuild shows#

Report and buildEvidence to collectDecision
Use-after-free or double-free, all code instrumentedThe free stack and every path that keeps a pointer past itFix the ownership or lifetime at the free site or the API that hands out the pointer
Heap or stack buffer overflow, all code instrumentedThe size in "located N bytes ... of M-byte region" and the index that produced the accessFix the bound; add the failing input as a test
Container-overflow, some code not instrumentedA run with detect_container_overflow=0 and a fully instrumented rebuildDismiss only if the rebuild is clean; record which objects were uninstrumented
Report inside a library you did not buildThe library frames and whether its own sanitizer build reproduces itReport it upstream; suppress that library alone, as the documentation's suppression file allows, and only for external code
Report missing its free or allocation stackA rebuild with -g and -fno-omit-frame-pointerCollect the stacks before deciding

A clean ASan run has its own limit: it covers only the paths the test executed, and the runtime holds freed memory in a quarantine only "during some period of time", so a stale pointer used after its block is reused can go unreported. AddressSanitizer algorithm When a finding came from an automated reviewer rather than a sanitizer, the guide to validating an AI review finding covers the different evidence it needs. When you dismiss a report, record the rebuild that cleared it, as the guide to dismissed findings describes, so the next person who sees the same report gets the same answer.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading