mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-11 09:42:17 +01:00
refactor: Avoid UB in SpanReader::ignore
Currently std::span::subspan is called without checking the size first. This is UB, unless the std lib is hardened. With a hardened stdlib, the program aborts: > include/c++/v1/span:512: libc++ Hardening assertion __offset <= size() > failed: span<T>::subspan(offset, count): offset out of range Fix the UB and the abort by using the implementation from DataStream, which throws when hitting end-of-data. This commit should not change any behavior, because the UB is currently unreachable. Also, the newly added throw should properly be caught by any code that calls any streams function.
This commit is contained in:
@@ -117,6 +117,9 @@ public:
|
||||
|
||||
void ignore(size_t n)
|
||||
{
|
||||
if (n > m_data.size()) {
|
||||
throw std::ios_base::failure("SpanReader::ignore(): end of data");
|
||||
}
|
||||
m_data = m_data.subspan(n);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user