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

Use calldata instead of memory in function parameter declarations #75

Open
code423n4 opened this issue Sep 20, 2021 · 1 comment
Open
Labels
bug Warden finding G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

chasemartin01

Vulnerability details

Impact

Gas optimisation

Example

As an example, you can change the declaration of inputTokens, inputWeights, outputTokens, outputWeights to be calldata as a gas optimisation

https://github.com/code-423n4/2021-09-defiProtocol/blob/main/contracts/contracts/Auction.sol#L69-L75

There's other instances of this in Basket.sol andFactory.sol

Explanation

When you specify memory for a function param for an external function, the following happens: the compiler copies elements from calldata to memory (using the opcode calldatacopy.) Note that there is also the opcode calldataload to read an offset from calldata. By changing the location from memory to calldata, you avoid this expensive copy from calldata to memory, while managing to do exactly what's needed.

Tools Used

Manual analysis

Recommended Mitigation Steps

Change all instances of memory to calldata where the function parameter isn't being modified

code423n4 added a commit that referenced this issue Sep 20, 2021
@frank-beard frank-beard added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Nov 6, 2021
@GalloDaSballo
Copy link
Collaborator

Agree because the function is public, notice that by changing to external you avoid this as well

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) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants