Skip to content

Commit

Permalink
refactor: Avoid UB in SHA3_256::Write
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarcoFalke committed Jan 14, 2025
1 parent fad4032 commit fabeca3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/crypto/sha3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ void KeccakF(uint64_t (&st)[25])

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

0 comments on commit fabeca3

Please sign in to comment.