Skip to main content

Can This Attack Be Prevented by Using a Non-Reentrant Lock?

Yes, a non-reentrant lock (reentrancy guard) is an effective way to prevent both direct and read-only reentrancy attacks. By using a state variable to lock the function at the beginning of its execution, any attempt by an external call to re-enter the function, even just to read a state variable, will fail the lock check and revert.

This prevents the attacker from accessing the function's logic and reading the potentially stale state before the transaction's effects are finalized.

Do Reentrancy Guards Protect against Both Single-Function and Cross-Function Attacks?
What Are the Differences between Single-Function and Cross-Function Reentrancy Attacks?
What Is the Difference between ‘Last Look’ and ‘Pre-Trade Credit Check’ in Derivatives Trading?
Can a Reentrancy Attack Be Performed without a Fallback Function?