diff --git a/integer.cpp b/integer.cpp index 78dd10a1..9af43bfa 100644 --- a/integer.cpp +++ b/integer.cpp @@ -3537,9 +3537,9 @@ class KDF2_RNG : public RandomNumberGenerator { public: KDF2_RNG(const byte *seed, size_t seedSize) - : m_counter(0), m_counterAndSeed(seedSize + 4) + : m_counter(0), m_counterAndSeed(ClampSize(seedSize) + 4) { - memcpy(m_counterAndSeed + 4, seed, seedSize); + memcpy(m_counterAndSeed + 4, seed, ClampSize(seedSize)); } void GenerateBlock(byte *output, size_t size) @@ -3550,6 +3550,15 @@ public: P1363_KDF2::DeriveKey(output, size, m_counterAndSeed, m_counterAndSeed.size(), NULLPTR, 0); } + // UBsan finding, -Wstringop-overflow + inline size_t ClampSize(size_t req) const + { + // Clamp at 16 MB + if (req > 16U*1024*1024) + return 16U*1024*1024; + return req; + } + private: word32 m_counter; SecByteBlock m_counterAndSeed;