_lock in noRenter can be optimized to use less gas #198
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")
Handle
mtz
Vulnerability details
Impact
The noRenter modifier sets the value of the
_locked
variable to1
to acquire the lock and prevent reentrancy and sets_locked
to0
to release the lock.Instead it should use
1
for unlocked and2
for locked since zeroing values in storage can use more gas.This implementation of
noRenter
is more gas efficient:This is similar to OpenZeppelin's ReentrancyGuard implementation
See the rationale there for why 1 and 2 are more efficient than 0 and 1.
Proof of Concept
In my tests (code snippet below), the more efficient reentrancy guard uses 11130 less gas
Tools Used
N/A
Recommended Mitigation Steps
Use the more efficient
noRenter
implementation recommended above.The text was updated successfully, but these errors were encountered: