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

noReenter Can Be Optimised Better #31

Closed
code423n4 opened this issue Jan 4, 2022 · 2 comments
Closed

noReenter Can Be Optimised Better #31

code423n4 opened this issue Jan 4, 2022 · 2 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists 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

leastwood

Vulnerability details

Impact

The noReenter modifier can be done more efficiently by using 1 and 2 to represent unlocked and locked states respectively. This is due to the fact that there are additional gas costs when updating a slow from a zero value to a non-zero value. The gas refunded when resetting the slot back to its zero value does not cover the cost of the initial change.

Proof of Concept

modifier noReenter() {
    require(_locked == 0, "LOCKED");
    _locked = uint256(1);
    _;
    _locked = uint256(0);
}

Tools Used

Manual code review.

Recommended Mitigation Steps

Consider using 1 and 2 to represent the unlocked and locked states respectively.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jan 4, 2022
code423n4 added a commit that referenced this issue Jan 4, 2022
@deluca-mike deluca-mike added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Jan 5, 2022
@deluca-mike
Copy link
Collaborator

deluca-mike commented Jan 5, 2022

For some cases 0 -> 1 -> 0 is cheaper, but after some testing, its overall cheaper to use 1 -> 2 -> 1 (specifically in the batch functions).

@deluca-mike deluca-mike added the duplicate This issue or pull request already exists label Jan 9, 2022
@deluca-mike
Copy link
Collaborator

Duplicate #1

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) 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