Fix integer overflow (#10222)

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
This commit is contained in:
Potuz 2022-02-10 15:20:36 -03:00 committed by GitHub
parent 1a0e16a48b
commit 72a2dd004b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -451,21 +451,15 @@ func (s *Store) applyWeightChanges(
}
s.proposerBoostLock.Unlock()
// A node's weight can not be negative but the delta can be negative.
if nodeDelta < 0 {
// A node's weight can not be negative but the delta can be negative.
if int(n.weight)+nodeDelta < 0 {
d := uint64(-nodeDelta)
if n.weight < d {
n.weight = 0
} else {
// Absolute value of node delta.
d := nodeDelta
if nodeDelta < 0 {
d *= -1
}
// Subtract node's weight.
n.weight -= uint64(d)
n.weight -= d
}
} else {
// Add node's weight.
n.weight += uint64(nodeDelta)
}