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

Adding unchecked directive can save gas #46

Open
code423n4 opened this issue Oct 18, 2021 · 2 comments
Open

Adding unchecked directive can save gas #46

code423n4 opened this issue Oct 18, 2021 · 2 comments
Labels
bug Warden finding G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons

Comments

@code423n4
Copy link
Contributor

Handle

WatchPug

Vulnerability details

For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.

For example:

  1. QuickAccManager.sol#send()

    https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/wallet/QuickAccManager.sol#L76-L76

} else {
    address signer = SignatureValidator.recoverAddr(hash, sigs.one);
    require(acc.one == signer || acc.two == signer, 'SIG');
    // no need to check whether `scheduled[hash]` is already set here cause of the incrementing nonce
    scheduled[hash] = block.timestamp + acc.timelock;
    emit LogScheduled(hash, accHash, signer, initialNonce, block.timestamp, txns);
}

block.timestamp + acc.timelock will never overflow.

  1. Zapper.sol#exchangeV2()

    https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/wallet/Zapper.sol#L114-L118

} else {
    uint[] memory amounts = trade.router.swapExactTokensForTokens(trade.amountIn, trade.amountOutMin, trade.path, address(this), deadline);
    uint lastIdx = trade.path.length - 1;
    lendingPool.deposit(trade.path[lastIdx], amounts[lastIdx], to, aaveRefCode);
}

trade.path.length - 1 will never underflow.

  1. SignatureValidatorV2.sol#recoverAddrImpl()

    https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/libs/SignatureValidatorV2.sol#L53-L61

if (mode == SignatureMode.SmartWallet) {
    // 32 bytes for the addr, 1 byte for the type = 33
    require(sig.length > 33, "SignatureValidator: wallet sig len");
    // @TODO: can we pack the addr tigher into 20 bytes? should we?
    IERC1271Wallet wallet = IERC1271Wallet(address(uint160(uint256(sig.readBytes32(sig.length - 33)))));
    sig.trimToSize(sig.length - 33);
    require(ERC1271_MAGICVALUE_BYTES32 == wallet.isValidSignature(hash, sig), "SignatureValidator: invalid wallet sig");
    return address(wallet);
}

sig.length - 33 will never underflow.

  1. Identity.sol#execute()

    https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/Identity.sol#L95-L95

nonce = currentNonce + 1;

currentNonce + 1 will never overflow.

code423n4 added a commit that referenced this issue Oct 18, 2021
@Ivshti
Copy link
Member

Ivshti commented Oct 19, 2021

might be a duplicate of #22 but it seems more like a superset as it's more detailed

@Ivshti Ivshti added sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons duplicate Another warden found this issue resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) labels Oct 19, 2021
@GalloDaSballo
Copy link
Collaborator

Agree with the finding, although unchecked ends up cluttering the source code

Will mark the other one as duplicate

@GalloDaSballo GalloDaSballo removed the duplicate Another warden found this issue label Oct 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Warden finding G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Projects
None yet
Development

No branches or pull requests

3 participants