Clear UBsan warning -Wstringop-overflow

pull/828/head
Jeffrey Walton 2019-04-27 21:08:02 -04:00
parent 39418a8512
commit 255a6f2aa0
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
1 changed files with 11 additions and 2 deletions

View File

@ -3537,9 +3537,9 @@ class KDF2_RNG : public RandomNumberGenerator
{ {
public: public:
KDF2_RNG(const byte *seed, size_t seedSize) 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) void GenerateBlock(byte *output, size_t size)
@ -3550,6 +3550,15 @@ public:
P1363_KDF2<SHA1>::DeriveKey(output, size, m_counterAndSeed, m_counterAndSeed.size(), NULLPTR, 0); P1363_KDF2<SHA1>::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: private:
word32 m_counter; word32 m_counter;
SecByteBlock m_counterAndSeed; SecByteBlock m_counterAndSeed;