Files
tai/research/safe-cpp-practices/cpp-core-guidelines-bounds-gsl.md
T
retoorandClaude Opus 4.8 79ae6f27da cpp-safety: safe-C++ playbook, fix a second UB, and a sanitizer net
Response to incident WA-2026-10-27 (the std::vector max_size() crash). Deep
research archived under research/safe-cpp-practices (8 Tier 1, 37 Tier 2, 2
conclusions) on production-safe C++ that keeps development speed: OpenSSF
compiler hardening, the C++ Core Guidelines bounds profile, integer-overflow
practice, libFuzzer/sanitizer CI, the sanitizer overhead ladder, clang-tidy
ratchets.

The research prescribed an ASan+UBSan+_GLIBCXX_ASSERTIONS build of the parsers.
Standing it up immediately caught a second, independent undefined-behavior bug
that two manual reviews had missed: a protobuf varint loop that shifted by up
to 70 bits (shift >= 64 is UB) on malformed input. Fixed and guarded by a
deterministic test and the sanitizer net.

- src/whatsapp/wa_proto.cpp: guard the wire-0 varint shift (the UB).
- tests/whatsapp_test.cpp: add an overlong-varint hostile case.
- tools/san-whatsapp.sh: the standing sanitizer net for detz::wa parsers.
- WaClient frame capture: a frame that fails to decode/dispatch has its exact
  bytes recorded to <home>/whatsapp/last-error.log (bounded), so the still
  unreproduced live <iq> failure becomes a one-paste deterministic fix. Wired
  from the wizard and the whatsapp_send tool.
- docs/cpp-safety.md: the project-wide playbook and design decisions (four
  layers: overflow-safe checked-reader parsing, cheap release hardening, the
  sanitizer test net, capture). docs/whatsapp.md section 13.6 records the
  follow-up and the honest open item. AGENTS.md makes it a standing rule;
  cap.md and mcp.md inherit it for their wire parsers.

Verified: build, whatsapp_test (incl. hostile+fuzz), make lint, and
tools/san-whatsapp.sh all green; research audit PASS.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-27 19:03:32 +01:00

2.2 KiB

C++ Core Guidelines: bounds safety, GSL, narrow_cast, not_null

Metadata

Field Value
Institute Molodetz Institute — Research & Retrieval Division
Subject safe-cpp-practices
Document type Source Dossier
Tier 1
Source title C++ Core Guidelines: bounds safety, GSL, narrow_cast, not_null
Source URL http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
Source publisher / author Bjarne Stroustrup, Herb Sutter (editors), ISO C++
Source type standard
Source date (published) Unknown — not stated (living document)
Date researched 2026-10-27
Researched by tai (agent), model: Claude Opus 4.8, on behalf of retoor
Research method web search synthesis
Confidence High
Status Active

Summary

The reference set of rules for modern C++. The bounds-safety profile treats range errors as a class to eliminate: pass span instead of pointer+length, and make narrowing conversions explicit and checked.

Key findings

  • Bounds profile: prefer span over raw pointer-plus-length parameters so a length cannot be passed wrong.
  • gsl::narrow<T>(x) converts and throws narrowing_error if the value does not round-trip; narrow_cast<T> is an explicit, silent, lossy cast.
  • ES.46: a negative double through narrow<unsigned> throws, while narrow_cast silently wraps, which is exactly the signed->size_t trap behind the WhatsApp crash.
  • gsl::not_null<T*> documents and enforces that null is not a valid value for a pointer.
  • The guidelines favor RAII and the rule of zero so resource bugs do not exist to be found.

Pros / strengths

The canonical, broadly agreed rulebook; the span and narrow rules map directly onto wire parsing.

Cons / limitations / caveats

Aspirational and large; full adoption is a multi-year ratchet. GSL is a dependency detz chose not to add (uses std::span/own checked reader instead).

Relevance to tai

Names the exact anti-patterns detz hit (implicit narrowing, pointer+length) and the exact cures (explicit checked narrowing, span-style readers). detz applies the principle without the GSL dependency.

Changelog

Date Change
2026-10-27 Initial dossier created (web search synthesis).