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 GitHub
parent 21647b3fac
commit 9042b8c63f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -723,7 +723,7 @@ func sub(b int, q int) (int, error) {
var diff int
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)
}