From 9a4920d565e5b241c9c9bdf0b2edda81cf749d2b Mon Sep 17 00:00:00 2001 From: yjwxfq <112159687+yjwxfq@users.noreply.github.com> Date: Thu, 8 Jun 2023 15:36:41 +0800 Subject: [PATCH] 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> --- token-erc-20/chaincode-go/chaincode/token_contract.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/token-erc-20/chaincode-go/chaincode/token_contract.go b/token-erc-20/chaincode-go/chaincode/token_contract.go index b6b7bad6..9752fd3c 100644 --- a/token-erc-20/chaincode-go/chaincode/token_contract.go +++ b/token-erc-20/chaincode-go/chaincode/token_contract.go @@ -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) }