#34844 util: Add util::NotNull<SmartPtrType>

full analysis

https://github.com/bitcoin/bitcoin/pull/34844 · maflcko · +380/-65 in 13 files, 5 commits · labels: Utils/log/libs

Goal

  • Enforce non-null smart pointer contracts at construction and function boundaries
  • Avoid repetitive defensive assertion checks and clarify API expectations across the codebase

Introduces `util::NotNull<SmartPtrType>` (along with `NotNullUniquePtr` and `NotNullSharedPtr` aliases) to express that a smart pointer cannot be null. It enforces non-nullness upon construction via `Assert` and documents pointer contracts at function boundaries, demonstrating its use across several areas including `CTxMemPool`, `CDBWrapper`, and `CNode`.

Problem: Bitcoin Core relies extensively on smart pointers where nullability is often either disallowed or unexpected, requiring repetitive defensive `Assert()` checks before dereferencing and obscuring API contracts.

Category: Utilities (logging, arguments, libraries) (#40 of 66)

P3 · new feature

  • P3 because it provides a reusable type invariant that simplifies null handling across the codebase
  • Modest leverage improvement by replacing repetitive runtime assertions with clearer API contracts

Provides a reusable type invariant that simplifies null handling and improves type safety across the codebase, resolving long-standing issue #24423.

Membership: Adds a general-purpose smart-pointer utility header and type in src/util/pointers.h.

Factors: security/stability 1, bug 0, performance 0, user value 0, leverage 1

Reviewability: Ready

  • Ready for review with a settled implementation and passing tests

The implementation is settled, tests pass, and it has an approval on the latest head commit.

Author status: Active; addressed all review feedback and squashed changes up to the latest ACK.

Resolved concerns:

  • Replaced the initial third-party GSL-based implementation with a minimal and modern C++20 implementation co-authored with stickies-v
  • Added aliases (`NotNullUniquePtr`, `NotNullSharedPtr`) to mitigate template verbosity at call sites
  • Addressed potential constructor selection ambiguity and unnecessary assertions on copy operations

Agreement: Strong

  • Strong support after co-authoring the simplified implementation (stickies-v)
  • Concept approval without detailed comments (sedited)
  • Approved earlier iterations prior to the custom rewrite (l0rinc)

Strong approval from stickies-v following collaboration on the implementation, with no active objections.

stickies-v co-authored the simplified implementation and gave a full ACK on the current head commit. l0rinc previously reviewed and approved earlier versions before the rewrite.

  • stickies-v approved head commit fa0e12e77b
  • sedited gave Concept ACK
  • l0rinc approved earlier iterations before the rewrite from GSL to custom C++20

Review verdicts (DrahtBot): 1 (+1)

Files

298 lines under test/bench/ci.

  • src/test/util_pointers_tests.cpp +189/-0
  • src/util/pointers.h +117/-0
  • src/test/rbf_tests.cpp +50/-46
  • src/coins.h +4/-4
  • src/test/coinsviewoverlay_tests.cpp +4/-4
  • src/net.cpp +3/-3
  • src/txmempool.h +4/-2
  • src/dbwrapper.h +3/-2
  • src/net.h +2/-1
  • src/test/fuzz/coins_view.cpp +1/-1
  • src/test/fuzz/coinscache_sim.cpp +1/-1
  • src/validation.cpp +1/-1
  • src/test/CMakeLists.txt +1/-0

Card

This PR introduces `util::NotNull<SmartPtrType>` to document and enforce non-nullness invariants for smart pointers at compile time and initialization. It solves the recurrent burden of defensive runtime assertions on smart pointer arguments and returns, addressing issue #24423. Reviewers successfully guided the PR from an imported GSL dependency to a clean, idiomatic C++20 custom implementation. The PR is fully reviewable and has an active approval from stickies-v.

Data

dossier JSON · extract JSON · model openrouter/google/gemini-3.8-flash, generated 2026-09-17T16:18, confidence high, input hash 9d6a8233c60278a7