Most of a large C++ codebase's serious security bugs come from a short list of failures: an index one past the end, a reference kept after its object is gone, a cast that trusts the wrong field, a value nobody initialized, a race on a shared string. Large vendors keep reporting that memory-safety issues make up the majority of their high-severity vulnerabilities. The tools that prevent most of them already exist. Most teams just don't use them every day.
Memory-Safe C++ in Production shows you how to use them every day.
You won't get a lecture on best practices. You'll take one realistic service and harden it from start to finish. relay is a network-facing message gateway with an untrusted input boundary, shared state across threads, hot paths where every check has a cost, and third-party code nobody owns. It ships with eight seeded memory bugs, one or more for each bug class, plus two that aren't listed. Over thirteen chapters you'll find them, fix them so their whole class can't come back, and build the tooling that catches the next one before it reaches production.
Every chapter ends with the Safety Ledger, a running record of what relay now enforces, what detects each bug class, which defects are still open, and what each step cost in measured performance. You always know what changed, what it bought and what it cost.
What you'll learn
- Make spatial errors impossible to write. Use span-based interfaces, hardened libc++ and libstdc++ modes, C++26 contracts, Clang's Safe Buffers and checked parsing at the network boundary. Then measure the real cost of bounds checks, which is usually within noise, and fix the few that aren't.
- Control lifetimes. Deal with dangling views, iterator invalidation, escaping lambdas, coroutine lifetime traps, shared_ptr cycles and use-after-move. Back up the fixes with explicit ownership, generational handles, arenas and compiler lifetime annotations.
- Close the quiet bug classes. Cover type confusion, strict aliasing, unions versus std::variant, C++26 erroneous behavior, automatic variable initialization and checked integer arithmetic.
- Build a sanitizer-first workflow. Run ASan, UBSan, MSan, TSan and HWASan on every build. Put GWP-ASan in production. Add static analysis with custom clang-tidy checks, and coverage-guided, stateful and differential fuzzing that runs continuously.
- Harden what survives. Add stack protectors, _FORTIFY_SOURCE, control-flow integrity, shadow stacks, hardened allocators and ARM Memory Tagging, and verify all of it on the binary you actually ship.
- Migrate real codebases. Audit by exposure. Define a safe subset the build enforces. Apply refactoring patterns that remove whole bug classes. Build safe C++/Rust interop with cxx. Decide rewrite versus harden honestly, one component at a time.
- Run it as a program. Track metrics that can't be gamed, set up CI/CD safety gates and canary comparison, follow an incident runbook for memory corruption, and write a multi-year roadmap for a real organization.
What makes this book different
- Every claim can be checked. Costs are measured with a stated protocol and reported as ranges, never as one convenient number from one machine.
- Every technique is applied to real code that builds, runs and is tested under sanitizers. There is no pseudocode.
- It's honest about limits. Hardened C++ is not memory-safe by construction, and the book says exactly where the remaining risk lives and how to contain it.
- It covers the whole lifecycle, from the first build flag to production detection, incident response and the decision to rewrite.
Who it's for
The book is for engineers who ship and maintain production C++: application and systems developers, tech leads, security engineers and platform teams. You should know modern C++. You don't need prior experience with sanitizers, fuzzers or the memory model. Each tool is introduced from first principles before you rely on it.
Four reference appendices come with the book: a sanitizer and hardening flag reference, an undefined behavior catalog, a static analysis check reference and a memory safety audit checklist you can run on your own codebase tomorrow.