Skip to content

Commit cdf2241

Browse files
MarcoFalkeluke-jr
authored andcommitted
refactor: Avoid UB in SHA3_256::Write
It is UB to apply a distance to a pointer or iterator further than the end itself, even if the distance is (partially) revoked later on. Fix the issue by advancing the data pointer at most to the end. Github-Pull: bitcoin#31655 Rebased-From: fabeca3
1 parent 831675c commit cdf2241

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/crypto/sha3.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ void KeccakF(uint64_t (&st)[25])
110110

111111
SHA3_256& SHA3_256::Write(Span<const unsigned char> data)
112112
{
113-
if (m_bufsize && m_bufsize + data.size() >= sizeof(m_buffer)) {
113+
if (m_bufsize && data.size() >= sizeof(m_buffer) - m_bufsize) {
114114
// Fill the buffer and process it.
115-
std::copy(data.begin(), data.begin() + sizeof(m_buffer) - m_bufsize, m_buffer + m_bufsize);
115+
std::copy(data.begin(), data.begin() + (sizeof(m_buffer) - m_bufsize), m_buffer + m_bufsize);
116116
data = data.subspan(sizeof(m_buffer) - m_bufsize);
117117
m_state[m_pos++] ^= ReadLE64(m_buffer);
118118
m_bufsize = 0;

0 commit comments

Comments
 (0)