Fix some bit-shifting overflow/UB issues reported by Coverity

This commit is contained in:
Tim Wojtulewicz 2025-07-21 15:24:00 -07:00
parent 22e78c3c24
commit d4cb3c8225
2 changed files with 9 additions and 1 deletions

View file

@ -259,6 +259,10 @@ AnonymizeIPAddr_A50::Node* AnonymizeIPAddr_A50::make_peer(ipaddr32_t a, Node* n)
// swivel is first bit 'a' and 'old->input' differ.
int swivel = bi_ffs(a ^ n->input);
// Shifting by more than 31 bits below results in undefined behavior.
// This shouldn't be possible, but check anyways.
ASSERT(swivel > 0);
// bitvalue is the value of that bit of 'a'.
int bitvalue = (a >> (32 - swivel)) & 1;
@ -305,6 +309,10 @@ AnonymizeIPAddr_A50::Node* AnonymizeIPAddr_A50::find_node(ipaddr32_t a) {
// differ.
int swivel = bi_ffs(n->child[0]->input ^ n->child[1]->input);
// Shifting by more than 31 bits below results in undefined behavior.
// This shouldn't be possible, but check anyways.
ASSERT(swivel > 0);
if ( bi_ffs(a ^ n->input) < swivel )
// Input differs earlier.
n = make_peer(a, n);

View file

@ -751,7 +751,7 @@ IPAddr SubNetVal::Mask() const {
for ( w = subnet_val->Length(); w >= 32; w -= 32 )
*(mp++) = 0xffffffff;
*mp = ~((1 << (32 - w)) - 1);
*mp = ~((static_cast<uint32_t>(1) << (32 - w)) - 1);
while ( ++mp < m + 4 )
*mp = 0;