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

Multiple double storage reading _xp(self) function calls #191

Open
code423n4 opened this issue Nov 10, 2021 · 0 comments
Open

Multiple double storage reading _xp(self) function calls #191

code423n4 opened this issue Nov 10, 2021 · 0 comments
Labels
bug Something isn't working 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

hyh

Vulnerability details

Impact

Gas overspending due to excessive storage reads and function calls

Proof of Concept

SwapUtils's removeLiquidityImbalance does multiple _xp(self) calls, which can be saved to memory when balances don't change inbetween executions
https://github.com/code-423n4/2021-11-bootfinance/blob/main/customswap/contracts/SwapUtils.sol#L1415

Recommended Mitigation Steps

Now:
uint256[] memory balances1 = self.balances;
v.preciseA = determineA(self, _xp(self));
v.d0 = getD(_xp(self), v.preciseA);
...
v.d1 = getD(_xp(self, balances1), determineA(self, _xp(self, balances1)));
...
v.d2 = getD(_xp(self, balances1), determineA(self, _xp(self, balances1)));

To be:
uint256[] memory balances1 = self.balances;
uint256[] memory tokenPM = self.tokenPrecisionMultipliers; // doesn't change, save and reuse
uint256[] memory xP = _xp(balances1, tokenPM); // We already copied self.balances, no need to reread storage
v.d0 = getD(xP, determineA(self, xP)); // v.preciseA isn't used elsewhere and can be dropped
...
xP = _xp(balances1, tokenPM); // balances1 was modified, recomputing
v.d1 = getD(xP, determineA(self, xP));
...
xP = _xp(balances1, tokenPM); // balances1 was modified, recomputing
v.d2 = getD(xP, determineA(self, xP));

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Nov 10, 2021
code423n4 added a commit that referenced this issue Nov 10, 2021
@chickenpie347 chickenpie347 added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Jan 3, 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 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

2 participants