Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas: > 0 is less efficient than != 0 for unsigned integers (with proof) #133

Closed
code423n4 opened this issue Feb 1, 2022 · 1 comment
Closed
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Handle

Dravee

Vulnerability details

Impact

!= 0 costs less gas compared to > 0 for unsigned integers in require statements with the optimizer enabled (6 gas)

Proof: While it may seem that > 0 is cheaper than !=, this is only true without the optimizer enabled and outside a require statement. If you enable the optimizer at 10k AND you're in a require statement, this will save gas. You can see this tweet for more proofs: https://twitter.com/gzeon/status/1485428085885640706

Proof of Concept

> 0 in require statements are used in the following location(s):

contracts\dex\bsc\UniV2ClassDex.sol:199:        require(amountIn > 0, 'INSUFFICIENT_INPUT_AMOUNT');
contracts\dex\bsc\UniV2ClassDex.sol:200:        require(reserveIn > 0 && reserveOut > 0, 'INSUFFICIENT_LIQUIDITY');
contracts\dex\bsc\UniV2ClassDex.sol:208:        require(amountOut > 0, 'INSUFFICIENT_OUTPUT_AMOUNT');
contracts\dex\bsc\UniV2ClassDex.sol:209:        require(reserveIn > 0 && reserveOut > 0, 'INSUFFICIENT_LIQUIDITY');
contracts\dex\eth\UniV2Dex.sol:197:        require(amountIn > 0, 'INSUFFICIENT_INPUT_AMOUNT');
contracts\dex\eth\UniV2Dex.sol:198:        require(reserveIn > 0 && reserveOut > 0, 'INSUFFICIENT_LIQUIDITY');
contracts\dex\eth\UniV2Dex.sol:206:        require(amountOut > 0, 'INSUFFICIENT_OUTPUT_AMOUNT');
contracts\dex\eth\UniV2Dex.sol:207:        require(reserveIn > 0 && reserveOut > 0, 'INSUFFICIENT_LIQUIDITY');
contracts\dex\eth\UniV3Dex.sol:126:        require(secondsAgo > 0, "SecondsAgo must >0");
contracts\dex\eth\UniV3Dex.sol:165:        require(amount0Delta > 0 || amount1Delta > 0);
contracts\farming\FarmingPools.sol:96:        require(amount > 0, "Cannot stake 0");
contracts\farming\FarmingPools.sol:104:        require(amount > 0, "Cannot withdraw 0");
contracts\gov\GovernorAlpha.sol:248:        require(proposalCount >= proposalId && proposalId > 0, "GovernorAlpha::state: invalid proposal id");
contracts\liquidity\LPool.sol:57:        require(initialExchangeRateMantissa > 0, "Initial Exchange Rate Mantissa should be greater zero");
contracts\liquidity\LPool.sol:674:            require(balanceAfter > balanceBefore, 'mint 0');
contracts\ControllerV1.sol:311:        require(rewards > 0, 'rewards is zero');
contracts\ControllerV1.sol:342:        require(supplyAmount > 0 || borrowAmount > 0, 'amount is less than 0');
contracts\ControllerV1.sol:358:        require(supplyAmount > 0 || borrowAmount > 0, 'amount0 and amount1 is 0');
contracts\OLETokenLock.sol:46:        require(amount > 0, "no amount available");
contracts\OLETokenLock.sol:57:        require(releaseVars[beneficiary].amount > 0, 'beneficiary does not exist');
contracts\OLETokenLock.sol:75:        require(releaseVar.amount > 0, "beneficiary does not exist");
contracts\OpenLevV1Lib.sol:81:        require(defaultFeesRate < 10000 && insuranceRatio < 100 && defaultMarginLimit > 0 && updatePriceDiscount <= 100
contracts\OpenLevV1Lib.sol:82:        && feesDiscount <= 100 && penaltyRatio < 10000 && twapDuration > 0, 'PRI');
contracts\OpenLevV1Lib.sol:112:        require(feesRate < 10000 && marginLimit > 0 && dexs.length > 0, 'PRI');
contracts\OpenLevV1Lib.sol:297:            require(borrow > 0, "BB0");
contracts\Reserve.sol:32:        require(amount > 0, "amount is 0!");
contracts\XOLE.sol:71:        require(totalSupply > 0, "Can't share without locked OLE");
contracts\XOLE.sol:252:        require(_value > 0, "Non zero value");
contracts\XOLE.sol:266:        require(_value > 0, "need non - zero value");
contracts\XOLE.sol:267:        require(_locked.amount > 0, "No existing lock found");
contracts\XOLE.sol:279:        require(_locked.amount > 0, "Nothing is locked");

Tools Used

VS Code

Recommended Mitigation Steps

Change > 0 with != 0 in require statements. Also, enable the Optimizer.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 1, 2022
code423n4 added a commit that referenced this issue Feb 1, 2022
@ColaM12 ColaM12 added the duplicate This issue or pull request already exists label Feb 2, 2022
@ColaM12
Copy link
Collaborator

ColaM12 commented Feb 2, 2022

Duplicate to #21

@ColaM12 ColaM12 closed this as completed Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants