refactor: use `DataStream::clear` in `::read` and `::ignore`
What changed, and why it matters
This is a small internal cleanup in Bitcoin Core's data-stream class. It replaces two snippets of identical code with a single call to an existing `clear()` method and removes an unused helper function. The commit message explicitly states the behavior is unchanged. There is no security-relevant change visible in the diff or references.
No security action needed. Treat as a normal code-quality refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
In src/streams.h, the DataStream::read() and DataStream::ignore() methods previously reset the stream to an empty state by setting m_read_pos = 0 and calling vch.clear() directly. The patch replaces those two lines with a call to DataStream::clear(), which performs the same operation. It also removes the unused Compact() method. The logic, state transitions, and externally observable behavior are identical before and after the change.
Changed components
src/streams.hInspect captured patch +4 / −10
diff --git a/src/streams.h b/src/streams.h
index d6d6d57f..4fc5879f 100644
--- a/src/streams.h
+++ b/src/streams.h
@@ -206,12 +206,6 @@ public:
value_type* data() { return vch.data() + m_read_pos; }
const value_type* data() const { return vch.data() + m_read_pos; }
- inline void Compact()
- {
- vch.erase(vch.begin(), vch.begin() + m_read_pos);
- m_read_pos = 0;
- }
-
bool Rewind(std::optional<size_type> n = std::nullopt)
{
// Total rewind if no size is passed
@@ -243,8 +237,8 @@ public:
}
memcpy(dst.data(), &vch[m_read_pos], dst.size());
if (next_read_pos.value() == vch.size()) {
- m_read_pos = 0;
- vch.clear();
+ // If fully consumed, reset to empty state.
+ clear();
return;
}
m_read_pos = next_read_pos.value();
@@ -258,8 +252,8 @@ public:
throw std::ios_base::failure("DataStream::ignore(): end of data");
}
if (next_read_pos.value() == vch.size()) {
- m_read_pos = 0;
- vch.clear();
+ // If all bytes are ignored, reset to empty state.
+ clear();
return;
}
m_read_pos = next_read_pos.value();
Why this scored 15/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.