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.