Repository navigation
span: diagnose dangling views from MakeByteSpan/MakeUCharSpan - #36183
kevkevinpal wants to merge 2 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, 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. |
jeanpablojp
left a comment
There was a problem hiding this comment.
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.
6591bf0 to
b5a86b5
Compare
maflcko
left a comment
There was a problem hiding this comment.
Just wondering about the great picture of this file here (left an unrelated comment).
| @@ -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. */ | |||
There was a problem hiding this 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:
#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))?
There was a problem hiding this comment.
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
|
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)).
b5a86b5 to
aa4bbd0
Compare

Follow-up to #36164, as suggested in #36164 (comment).
Tracked in kevkevinpal#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.