dbwrapper: use `SpanReader` for iterator keys
What changed, and why it matters
This is a small internal code cleanup in Bitcoin Core's database wrapper. It changes how LevelDB iterator keys are read so the bytes are decoded directly from a borrowed memory view instead of being copied into a temporary buffer first. The commit message and diff show no security fix, bug correction, or behavior change—only a performance and clarity improvement.
No security action required. Treat as a routine refactoring/optimization commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
CDBIterator::GetKey() previously constructed a DataStream from GetKeyImpl()’s span, which copied the key bytes into an owning stream. The patch switches to SpanReader, which reads directly from the span without allocation or copy. Exception handling and return semantics are preserved. A comment is added clarifying that GetKeyImpl()’s span borrows from the current iterator entry and is invalidated on advancement. The commit explicitly notes the same change is not appropriate for GetValue() because value bytes are deobfuscated in place and still require a mutable buffer.
Changed components
src/dbwrapper.cppsrc/dbwrapper.hInspect captured patch +3 / −1
diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp
index eb222078..50a58d5f 100644
--- a/src/dbwrapper.cpp
+++ b/src/dbwrapper.cpp
@@ -370,6 +370,8 @@ void CDBIterator::SeekImpl(std::span<const std::byte> key)
std::span<const std::byte> CDBIterator::GetKeyImpl() const
{
+ // The returned span borrows from the current iterator entry and is only
+ // valid until the iterator is advanced.
return MakeByteSpan(m_impl_iter->iter->key());
}
diff --git a/src/dbwrapper.h b/src/dbwrapper.h
index 2eee6c1c..75ac5fdb 100644
--- a/src/dbwrapper.h
+++ b/src/dbwrapper.h
@@ -153,7 +153,7 @@ public:
template<typename K> bool GetKey(K& key) {
try {
- DataStream ssKey{GetKeyImpl()};
+ SpanReader ssKey{GetKeyImpl()};
ssKey >> key;
} catch (const std::exception&) {
return false;
Why this scored 13/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.