This sheet is past its review date. Deadlines or advice may have changed since 5 Nov 2026.
C and C++: how safe can you get without a rewrite?
Before your next release, build with the OpenSSF hardening flags and a bounds-checked standard library, and run tests and fuzzers under ASan and UBSan in CI. Write new components in a memory-safe language, and judge each change by whether it checks the size, owner and lifetime of memory that outside data reaches.
If you only do five things
Seven of the 2025 CWE Top 25 are memory-safety weaknesses1. Writing new code in memory-safe languages cut them from 76% of Android's vulnerabilities in 2019 to under 20% in 202523.
Fuzz every parser of outside data.
Give each file format, protocol and decoder a libFuzzer target, and run it on every pull request with ClusterFuzzLite under ASan, MSan and UBSan12.
Review a C or C++ pull request
Does a length, count or offset come from the input?
Check it against the bytes left and the destination's size before any copy, index or allocation.
Can a size calculation wrap?
Look for
*and+on sizes,intlengths and signed-to-unsigned casts. Ask for__builtin_mul_overflow14.Who owns each pointer, and can anything outlive it?
Check raw
new,deleteandfree, storedstring_viewandspanmembers, and callbacks that capturethisor&.Is every error code and union tag checked?
Ask for
[[nodiscard]]on functions that return errors and a tag check before each union read.Does it switch off a check?
Warning pragmas,
no_sanitize,_LIBCPP_HARDENING_MODE_NONE,[[indeterminate]]and/GS-need a second reviewer.
C and C++ traps and what to use instead
memcpy(buf, p, len)withlenfrom the input. OpenSSL copied an AEAD IV into a fixed-size stack buffer without a length check, before any authentication (CVE-2025-15467, high)15. Check each length against the destination and the bytes left;_FORTIFY_SOURCE=3catches it only if it knows the size4.n * sizeandlen + 1in size calculations. Unsigned arithmetic wraps, and signed overflow is undefined, so the compiler may delete your overflow check4. Integer overflows in Onigmo, Ruby's regex engine, could let crafted patterns run code on GitLab (CVE-2026-97229, exploited)1617. Use__builtin_mul_overflow14.- Error codes that let a decoder keep going. Brotli's decoder treated an invalid block switch as a need for more input and kept appending to an eight-byte buffer (CVE-2026-111497, exploited)1817. Treat format errors as final, bound every append and mark error returns
[[nodiscard]]. - Ownership an API leaves unclear. OpenSSL's
PKCS7_verify()freed a BIO its caller still owned, so the caller'sBIO_free()became a use after free (CVE-2026-45447, high)15. Own withstd::unique_ptr, don't keep astd::string_viewpast its owner, and in C document who frees what. - Reading a union member as the wrong type. OpenSSL read an
ASN1_TYPEunion member without checking its type (CVE-2026-22796)15, and CISA added type confusion bugs in V8 and Windows to its exploited list in 202617. Check the tag first; in C++ usestd::variant. printf(msg)and your own logging wrappers. OpenSSL passed a peer-supplied name as the format string toERR_raise_data(), soCN=%s%ncrashed CMP clients (CVE-2026-63073)15. Pass data through"%s", and mark wrappersformat(printf, m, n)to get printf's checks19.
Tools that catch it
- Sanitizers. ASan (about 2x slower), UBSan, MSan for uninitialized reads (every library instrumented, and no
-ftrivial-auto-var-init, which masks them) and TSan for data races, which can't run with ASan10204. MSVC has/fsanitize=address21. - Fuzzers. libFuzzer now gets only bug fixes22; ClusterFuzzLite runs it on pull requests and in batch jobs12.
- Warnings.
-Wall,-Wextra,-Wformat=2,-Wconversionand-Werror=format-security4. In C++, Clang's-Wunsafe-buffer-usageflags buffer access through raw pointers23. On MSVC,/sdlmakes uninitialized-variable warnings errors24.
What changed in 2026
- GCC 16 (30 April) zero-initializes locals under
-std=c++26and now defaults to-std=gnu++202526. - Clang 23 added
-Wlifetime-safety, which detects use after free within a function. Add it to CI27. - OpenSSL published 63 CVEs in 2026, against 6 in 2025; all three High ones are memory-safety bugs15. 3.0 and 3.6 are now out of support; 3.5 is supported until April 2030, 4.0 until May 202728.
- CISA added an exploited Brotli overflow on 27 October. Update every copy, vendored ones too, to 1.0.10, 1.1.1 or 1.2.12917.
What you can skip
- Waiting for C++26. It isn't final yet31, its library hardening standardizes checks the three major libraries already ship30, and
-ftrivial-auto-var-init=zeroinitializes locals today4. - Rewriting stable code at once. Vulnerabilities decay as code ages2: harden old code and rewrite the parsers that take outside data.
- Valgrind in CI. Sanitizers find similar bugs at a 2x to 4x slowdown, against up to 20x to 50x4.
Mistakes that cost the most
- Checks only debug builds get. GCC 15's libstdc++ enables its assertions by default only in unoptimized builds32, and
_FORTIFY_SOURCEneeds-O1or higher4. Set both in release builds. - Shipping sanitizer runtimes. Their environment-variable options can let attackers clobber root-owned files through setuid binaries. Only UBSan's minimal runtime or trap mode belongs in production411.
What this looks like for your code
New project
- Ask first whether it can be Rust or another memory-safe language; see the Rust sheet.
- If it must be C++, use
std::span,std::unique_ptrand-Wunsafe-buffer-usage23. - Start with the OpenSSF flags, a hardened library, sanitizer jobs and a fuzz target per parser.