Update token_contract.go, modify the overflow judgment condition for function sub().

The general overflow judgment condition for function sub is not in every case.
The judgment result is wrong in the condition of b < 0 && q >=0
for example, b = -3, q = 2, diff = -5, it's not overflow,
but if (diff > b) == (b >= 0 && q >= 0) wil be true.

for another example, b = -3, q = maxint, b-q is overflow,
but if (diff > b) == (b >= 0 && q >= 0) wil be false.

Signed-off-by: yjwxfq <112159687+yjwxfq@users.noreply.github.com>
This commit is contained in:
yjwxfq 2023-06-08 15:36:41 +08:00 committed by Dave Enyeart
parent bc3a6bfa05
commit 9a4920d565

View file

@ -723,7 +723,7 @@ func sub(b int, q int) (int, error) {
var diff int var diff int
diff = b - q diff = b - q
if (diff > b) == (b >= 0 && q >= 0) { if (diff < b || diff < -q) == (b >= 0 && q <= 0) {
return 0, fmt.Errorf("Math: Subtraction overflow occurred %d - %d", b, q) return 0, fmt.Errorf("Math: Subtraction overflow occurred %d - %d", b, q)
} }