4 suggestions
Earlier concerns neither republished nor resolved: 5 suggestions. See the Run archive for their assessments.
Optional improvements
[suggestion] · The public witness API hides the quadratic cost of Items()
The public API does not explain that traversing Items() is now quadratic despite the linear bulk alternative; no in-tree production caller was found, so external impact remains unconfirmed.
Location: src/kernel/bitcoinkernel_wrapper.h:679 (head).
Details and suggested fix
Items() dereferences through GetItem(index), which reaches CScriptWitness::operator[] and walks from begin() on every access. A full traversal therefore repeats roughly n(n-1)/2 prefix-decoding steps, in addition to copying payloads. Document the O(index) lookup cost on btck_witness_stack_get_item_at and GetItem(), warn that full Items() traversal is O(n²), and point whole-stack readers to btck_witness_stack_get_items or GetItems(). Distinguish the lazy per-access copies from GetItems()'s eager owning copies so callers can choose without changing the existing range's behavior.
[suggestion] · CScriptWitness construction can violate its noexcept invariant
The vector constructor accepts an element larger than MAX_SIZE, but reading it terminates in a noexcept accessor; no current production caller creating such a vector was found.
Location: src/script/script.cpp:309 (head).
Details and suggested fix
The constructor bounds only the total serialized length to uint32_t; WriteCompactSize accepts counts and element lengths above MAX_SIZE (32 MiB). A one-element vector containing MAX_SIZE + 1 bytes is therefore accepted, but front() decodes its length using range-checked ReadCompactSize inside Iterator::operator*() noexcept, causing std::terminate. Reject oversized counts and element lengths in the constructor, and add a constructor test at this boundary. Deserialized witnesses already receive these range checks, and the 32 MiB HTTP request-body cap keeps raw RPC hex elements below the threshold.
[suggestion] · GetItems() always copies the borrowed witness payloads
Although linear and lifetime-safe, GetItems() allocates and copies each non-empty payload; a separate borrowed-view accessor would avoid that cost for read-only access.
Location: src/kernel/bitcoinkernel_wrapper.h:689 (head).
Details and suggested fix
GetItems() obtains borrowed pointers from btck_witness_stack_get_items, then constructs an independent byte vector for every item with copies.emplace_back(...). Read-only bulk access consequently pays per-item allocation and payload-copy costs even though the C function already supplies usable views. Offer an explicit borrowed bulk accessor returning std::vector<std::span<const std::byte>>, documented as valid only while the underlying witness storage remains valid. Keep the owning GetItems() variant for callers needing independent storage: its ownership matches GetItem(), and this suggestion saves allocations rather than correcting a complexity regression.
[suggestion] · The ToSpans() lifetime promise omits storage replacement
Assignment or successful Unserialize() can invalidate spans before the CScriptWitness dies, contrary to the comment; no current caller retaining views across replacement was found.
Location: src/script/script.h:712 (head).
Details and suggested fix
A non-self copy assignment calls CopyFrom(), which resets or replaces m_data. Move assignment and successful deserialization can also replace the destination's storage. Existing element spans and iterators then refer to freed storage while the witness object remains alive. Clarify that elements cannot be edited in place, rather than saying the witness cannot be modified, and document that replacing its contents by assignment or deserialization invalidates views into the previous contents.
Earlier concerns
Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.
- [suggestion]:
VerifyWitnessProgrammaterializes witnesses on nonexecuting paths (earlier report) - [suggestion]: Mempool witness-memory changes lack an operator-facing note (earlier report)
- [suggestion]:
GetItems()permits borrowed results from a temporary owning handle (earlier report) - [suggestion]:
IsWitnessStandardmaterializes spans for checks that need only a scan (earlier report) - [suggestion]:
GetItems()needs two full-size scratch buffers (earlier report)