mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
span: diagnose dangling views from MakeByteSpan/MakeUCharSpan #36183
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36183
Loading…
Reference in a new issue
No description provided.
Delete branch "span-lifetimebound-make-byte-uchar"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follow-up to #36164, as suggested in https://github.com/bitcoin/bitcoin/pull/36164#issuecomment-5541268260.
Tracked in https://github.com/kevkevinpal/bitcoin/issues/550.
Problem:
MakeByteSpanandMakeUCharSpanreturn views into their input. A temporary container can leave those views dangling.Fix: Add
LIFETIMEBOUNDto the overload taking non-borrowed_rangetypes, so Clang diagnoses the misuse on owning containers without flagging correct code that passes a view.leveldb::Sliceis opted intoborrowed_rangesodbwrappercan keep using the helper. Matching theutil/string.hhelpers from #36164.The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
Concept ACK
LIFETIMEBOUNDonconst V&claims the result borrows fromv. True whenVowns the bytes, false whenVis a non-owning view, andMakeByteSpantakes both, so the annotation also fires on correct code. The four warnings this diff works around are all that case.It also reaches a shape outside the diff, where a function returning
MakeByteSpanof a localstd::spanorstd::string_viewnow gets-Wreturn-stack-address.Would a
requires (!std::ranges::borrowed_range<V>)on the annotated overload work? Compiled both ways here, the constrained form keeps the temporary-container warning and drops the two innet_tests.leveldb::Sliceis not a borrowed range, sodbwrapperneeds its own handling regardless.Neither form keeps the helper here.
MakeByteSpan(m_impl_iter->iter->key())warns because theSliceis a temporary, and binding it to a local only changes the warning to "address of stack memory associated with local variable 'key' returned".Would opting the
Slicein be worth it? On top of those overloads it brings both getters back to the helper, and a storedMakeByteSpanof a temporarystd::vectorstill warns. On its own it changes nothing, since the annotation as it stands is unconditional.Concretely this, plus a
#include <ranges>at the top:The first overload takes whatever
std::ranges::borrowed_rangeaccepts,std::spanandstd::string_viewamong them, and everything else takes the second.MakeUCharSpantakes the same pair, with therequiressitting alongside itsdecltypereturn.UncopyableStreaminherits theDataStreamconstructor that copies intovch, so no view is kept here. Reverted, the file still compiles clean.Reverted, thanks, you're right that
UncopyableStreamcopies intovch, sono view outlives the temporary.
Taken as-is, with
#include <ranges>added. Only difference is that clang-format puts the requires-clause on its own line rather than inline with thedecltypereturn forMakeUCharSpan.Opted it in, and it turns out to be load-bearing rather than just a cleanup. With the constrained overloads but no specialization, clang gives
-Wreturn-stack-addresson both getters, sinceSliceis not aborrowed_rangeand lands on the annotated overload. With the specialization both getters go back toMakeByteSpanand the file is clean.I kept it in
dbwrapper.cpprather than the header:dbwrapper.hdeliberately doesn't pull in leveldb headers, and this is the only TU that pairsSlicewith the span helpers.Just wondering about the great picture of this file here (left an unrelated comment).
unrelated: Seems odd to require BasicByte here for MakeUCharSpan, but not for MakeByteSpan.
Either it should be removed for both, or enforced for both.
Maybe it can be made explicit with something like:
Also, UCharSpanCast is only called/used once, so maybe it can be renamed to
UCharCast?Afterwards, MakeUCharSpan(a) could even be removed and replaced by UCharCast(MakeByteSpan(a))?
also, reminds me that UCharCast will eat the annotation anyway right now. At least I don't get any warning for:
So this pull is incomplete anyway
Earlier concerns remain unresolved; see the Run archive linked from the Review report.
🔵 Suggestion · Chained span conversions lose the new lifetime dependency
Chained conversions bypass the new Clang lifetime annotation; no current caller producing this input was found, and the underlying dangling-span behavior predates this PR.
Location:
src/span.h:121(head).Review coverage was incomplete.
Reviewed:
aa4bbd0c9b40· Review report · Previous reportspermalink
🐙 This pull request conflicts with the target branch and needs rebase.
Ping for rebase @kevkevinpal