From fabeca3458b38a3d8930cb0cbc866388c3f120f1 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 14 Jan 2025 19:11:45 +0100 Subject: [PATCH] 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. --- src/crypto/sha3.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crypto/sha3.cpp b/src/crypto/sha3.cpp index 770500bfe27..56aaa4615e7 100644 --- a/src/crypto/sha3.cpp +++ b/src/crypto/sha3.cpp @@ -105,9 +105,9 @@ void KeccakF(uint64_t (&st)[25]) SHA3_256& SHA3_256::Write(Span 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;