Skip to content

span: diagnose dangling views from MakeByteSpan/MakeUCharSpan - #36183

Open
kevkevinpal wants to merge 2 commits into
bitcoin:masterfrom
kevkevinpal:span-lifetimebound-make-byte-uchar
Open

kevkevinpal wants to merge 2 commits into
bitcoin:masterfrom
kevkevinpal:span-lifetimebound-make-byte-uchar

Conversation

@kevkevinpal

@kevkevinpal kevkevinpal commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #36164, as suggested in #36164 (comment).

Tracked in kevkevinpal#550.

Problem: MakeByteSpan and MakeUCharSpan return views into their input. A temporary container can leave those views dangling.

Fix: Add LIFETIMEBOUND to the overload taking non-borrowed_range types, so Clang diagnoses the misuse on owning containers without flagging correct code that passes a view. leveldb::Slice is opted into borrowed_range so dbwrapper can keep using the helper. Matching the util/string.h helpers from #36164.

@DrahtBot

DrahtBot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

Type Reviewers
Concept ACK jeanpablojp

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:

  • #34132 (coins,dbwrapper: make database point-read failures fatal by l0rinc)

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.

@jeanpablojp jeanpablojp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

LIFETIMEBOUND on const V& claims the result borrows from v. True when V owns the bytes, false when V is a non-owning view, and MakeByteSpan takes 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 MakeByteSpan of a local std::span or std::string_view now 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 in net_tests. leveldb::Slice is not a borrowed range, so dbwrapper needs its own handling regardless.

Comment thread src/span.h Resolved
Comment thread src/dbwrapper.cpp Resolved
Comment thread src/test/serialize_tests.cpp Resolved

@maflcko maflcko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering about the great picture of this file here (left an unrelated comment).

Comment thread src/span.h
@@ -108,7 +118,12 @@ concept BasicByte = requires { UCharCast(std::span<B>{}.data()); };
template <typename T, size_t N> constexpr auto UCharSpanCast(std::span<T, N> s) { return std::span<std::remove_pointer_t<decltype(UCharCast(s.data()))>, N>{UCharCast(s.data()), s.size()}; }

/** Like the std::span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

#include <span>
#include <cstddef>

enum class EndianPolicy {
    Strict,
    Ignore
};

template <EndianPolicy Policy = EndianPolicy::Strict, typename V>
auto MakeByteSpan(const V& v) noexcept
{
    if constexpr (sizeof(typename decltype(std::span{v})::element_type) > 1) {
        static_assert(Policy == EndianPolicy::Ignore,
            "Cannot create byte span for multi-byte types: byte order is architecture-dependent. "
            "Use 'MakeByteSpan<EndianPolicy::Ignore>(...)' to acknowledge this.");
    }

    return std::as_bytes(std::span{v});
}

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))?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, reminds me that UCharCast will eat the annotation anyway right now. At least I don't get any warning for:

diff --git a/src/test/span_tests.cpp b/src/test/span_tests.cpp
index 42b62413aa..f0a22636d6 100644
--- a/src/test/span_tests.cpp
+++ b/src/test/span_tests.cpp
@@ -44,0 +45,6 @@ BOOST_AUTO_TEST_SUITE(span_tests)
+auto test_function()
+{
+    std::vector<std::byte> a;
+    return UCharSpanCast(std::span{a});
+}
+

So this pull is incomplete anyway

@sedited

sedited commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Ping for rebase @kevkevinpal

It is only used by MakeUCharSpan, and is the span counterpart of the
pointer UCharCast overloads. No behavior change.
Follow-up to bitcoin#36164. Add LIFETIMEBOUND on the non-borrowed-range
overloads so Clang diagnoses storing spans of temporary owning
containers, matching the util/string.h helpers.

Also annotate the span overload of UCharCast, so the lifetime is not
lost when casting an already borrowed span, e.g.
UCharCast(MakeByteSpan(v)).
@kevkevinpal
kevkevinpal force-pushed the span-lifetimebound-make-byte-uchar branch from b5a86b5 to aa4bbd0 Compare November 12, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants