Skip to content

Commit 80db838

Browse files
committed
Enhance balance management in StateDB by adding overflow checks for AddBalance and SubBalance methods. This prevents potential errors during balance calculations.
1 parent b19675b commit 80db838

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

core/state/statedb.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,10 @@ func (s *StateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tr
458458
key := GetRSS3BalanceKey(addr)
459459
value := s.GetState(params.RSS3Address, key)
460460
balance := uint256.MustFromBig(value.Big())
461-
newBalance := new(uint256.Int).Add(balance, amount)
461+
newBalance, overflow := new(uint256.Int).AddOverflow(balance, amount)
462+
if overflow {
463+
s.setError(fmt.Errorf("AddBalance overflow"))
464+
}
462465

463466
s.SetState(params.RSS3Address, key, common.BytesToHash(newBalance.Bytes()))
464467

@@ -470,7 +473,10 @@ func (s *StateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tr
470473
key := GetRSS3BalanceKey(addr)
471474
value := s.GetState(params.RSS3Address, key)
472475
balance := uint256.MustFromBig(value.Big())
473-
newBalance := new(uint256.Int).Sub(balance, amount)
476+
newBalance, overflow := new(uint256.Int).SubOverflow(balance, amount)
477+
if overflow {
478+
s.setError(fmt.Errorf("SubBalance overflow"))
479+
}
474480
s.SetState(params.RSS3Address, key, common.BytesToHash(newBalance.Bytes()))
475481
return *newBalance
476482
}

0 commit comments

Comments
 (0)