C++ Failure Atlas¶
A field guide to the failures that cost real debugging time. Each entry has a minimal broken program, the tool output it should produce, and the smallest repair that removes the failure.
This is not a list of "undefined behavior trivia". Every broken/fixed pair on
this page is compiled and executed by tools/verify_failure_claims.py in CI.
If a compiler or sanitizer stops producing the expected failure, the build
fails. The atlas is therefore a set of tested claims, not prose about what
"should" happen.
How to read an entry¶
- Symptom — what you see in a crash, hang, or wrong result.
- Broken — the smallest program that demonstrates the failure.
- Tool output — the marker the verifier expects in the sanitizer output.
- Repair — the smallest change that removes the failure.
- Practice — the related Arcade boss or lab.
Run one entry locally with:
The verifier uses CXX (default g++) and prints the compiler version it
actually tested. The CI job runs the same script, so the page cannot silently
drift away from the code.
1. Use after free¶
Symptom — a pointer still looks valid, but the object it points at has already been destroyed. The read often succeeds in a debug build and fails under load or with a different allocator.
Broken
#include <iostream>
int main() {
int* value = new int(42);
delete value;
std::cout << *value << '\n';
}
Tool output — heap-use-after-free
Repair
#include <iostream>
#include <memory>
int main() {
auto value = std::make_unique<int>(42);
std::cout << *value << '\n';
}
Why the repair works — the owner is explicit. The integer lives exactly as
long as the unique_ptr, and there is no second pointer that can outlive it.
Practice — Dangling Pointer boss.
2. Iterator invalidation¶
Symptom — an iterator was valid when it was created, then the container reallocated. Dereferencing it now points into freed storage.
Broken
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{1, 2, 3};
auto it = values.begin();
values.push_back(4);
std::cout << *it << '\n';
}
Tool output — attempt to dereference a singular iterator
Repair
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{1, 2, 3};
values.reserve(4);
auto it = values.begin();
values.push_back(4);
std::cout << *it << '\n';
}
Why the repair works — reserve allocates capacity for the fourth element
before the iterator is taken, so push_back does not reallocate. Reserving is a
promise about capacity, not a license to keep an iterator across an arbitrary
mutation; use an index or a fresh lookup when the size is not known.
Practice — Iterator Invalidation boss.
3. Data race¶
Symptom — the answer changes between runs; one thread's write is lost, or the program reports a race only when the sanitizer happens to observe both accesses.
Broken
#include <iostream>
#include <thread>
int counter = 0;
void increment() {
for (int i = 0; i < 100000; ++i) {
++counter;
}
}
int main() {
std::thread first(increment);
std::thread second(increment);
first.join();
second.join();
std::cout << counter << '\n';
}
Tool output — ThreadSanitizer: data race
Repair
#include <atomic>
#include <iostream>
#include <thread>
std::atomic<int> counter{0};
void increment() {
for (int i = 0; i < 100000; ++i) {
counter.fetch_add(1, std::memory_order_relaxed);
}
}
int main() {
std::thread first(increment);
std::thread second(increment);
first.join();
second.join();
std::cout << counter.load() << '\n';
}
Why the repair works — an atomic makes the read-modify-write one indivisible operation. This example needs only atomicity, so relaxed ordering is enough; it does not need to order other memory operations.
Practice — Data Race boss.
4. Double free¶
Symptom — the allocator sees the same block returned twice. The second
delete may corrupt the heap immediately or fail later in an unrelated call.
Broken
#include <iostream>
int main() {
int* value = new int(7);
delete value;
delete value;
std::cout << "unreachable\n";
}
Tool output — attempting double-free
Repair
#include <iostream>
#include <memory>
int main() {
auto value = std::make_unique<int>(7);
std::cout << *value << '\n';
}
Why the repair works — ownership is transferred to unique_ptr, which
destroys the object exactly once and does not expose a second delete.
Practice — Double Free boss.
5. Heap out-of-bounds¶
Symptom — an index writes one past the allocation. A small overrun can silently damage the next object; a large one crashes in a distant code path.
Broken
#include <iostream>
int main() {
int* values = new int[4]{};
values[4] = 9;
std::cout << values[3] << '\n';
delete[] values;
}
Tool output — heap-buffer-overflow
Repair
#include <iostream>
#include <vector>
int main() {
std::vector<int> values(4);
values.at(3) = 9;
std::cout << values[3] << '\n';
}
Why the repair works — .at() checks the index and throws instead of
writing through it. A std::vector also carries its size, so the valid range is
available at the call site.
Practice — Lifetime lab.
6. Dangling string_view¶
Symptom — a string_view is cheap because it does not own characters. That
is also its danger: destroying the owning string leaves the view pointing at
released storage.
Broken
#include <iostream>
#include <string>
#include <string_view>
int main() {
auto* text = new std::string("temporary");
std::string_view view = *text;
delete text;
std::cout << view << '\n';
}
Tool output — heap-use-after-free
Repair
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string text = "temporary";
std::string_view view = text;
std::cout << view << '\n';
}
Why the repair works — the string outlives the view. As a rule, a
string_view should be a function parameter or a temporary view whose owner is
obviously alive at every use.
Practice — Lifetime lab.
7. Deleting through a non-virtual base¶
Symptom — delete base calls the base destructor but not the derived one.
The derived resources leak, and the deallocation size can be wrong.
Broken
#include <iostream>
struct Base {
~Base() {}
};
struct Derived : Base {
int* values = new int[4];
~Derived() { delete[] values; }
};
int main() {
Base* base = new Derived;
delete base;
std::cout << "unreachable\n";
}
Tool output — new-delete-type-mismatch
Repair
#include <iostream>
#include <vector>
struct Base {
virtual ~Base() = default;
};
struct Derived : Base {
std::vector<int> values{4};
};
int main() {
Base* base = new Derived;
delete base;
std::cout << "destroyed\n";
}
Why the repair works — a virtual destructor dispatches to the most-derived
destructor. The repair also replaces the raw array with a vector, so the
derived object owns its storage without a manual delete[].
Practice — Ownership and lifetime.
8. Invalid downcast¶
Symptom — a static_cast asserts a type relationship that is not true.
The program reads or writes the wrong object representation.
Broken
#include <iostream>
struct Base {
virtual ~Base() = default;
};
struct A : Base {
int a = 1;
};
struct B : Base {
int b = 2;
};
int main() {
Base* base = new A;
auto* wrong = static_cast<B*>(base);
wrong->b = 3;
std::cout << wrong->b << '\n';
delete base;
}
Tool output — downcast of address
Repair
#include <iostream>
struct Base {
virtual ~Base() = default;
};
struct A : Base {
int a = 1;
};
struct B : Base {
int b = 2;
};
int main() {
Base* base = new A;
auto* wrong = dynamic_cast<B*>(base);
if (wrong != nullptr) {
wrong->b = 3;
}
std::cout << (wrong == nullptr ? "not a B" : "a B") << '\n';
delete base;
}
Why the repair works — dynamic_cast checks the runtime type and returns
nullptr when the object is not a B. The check makes the failure path
explicit instead of turning a bad assumption into memory corruption.
Practice — Ownership and lifetime.
Verify the whole atlas¶
From the repository root:
The script compiles every marked broken/fixed pair in a temporary directory, runs the broken program with the declared sanitizer, checks the expected marker, then runs the repaired program and requires a clean exit.
Scope¶
The atlas is deliberately small. It covers eight failure families that different tools can demonstrate deterministically on Linux with GCC. It is not a proof that a program is free of undefined behavior, and it is not a substitute for a sanitizer run against the real application.
If a compiler changes an error message, update the marker and the page together; if a failure stops reproducing, remove the entry instead of weakening the check.