-
Notifications
You must be signed in to change notification settings - Fork 22
/
sol81_underflow.sol
41 lines (36 loc) · 1.23 KB
/
sol81_underflow.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
contract ContractError {
function Underflow() public pure returns (uint) {
uint x = 0;
x--; // this will generate an underflow
return x;
}
function UncheckedUnderflow() public pure returns (uint) {
uint x = 0;
unchecked { x--; } // this will generate an underflow
return x;
}
}
contract C {
ContractError e = new ContractError();
function TestUnderflow() public view returns (string memory) {
try e.Underflow() returns (uint) {
return "Ok";
} catch Error(string memory reason) {
return reason;
} catch Panic(uint _code) {
if (_code == 0x01) { return "Assertion failed"; }
else if (_code == 0x11) { return "Underflow/overflow"; }
// We ignore the other errors.
return "Other Panic";
} catch (bytes memory reason) {
uint x=0;
for (uint i=0;i<4;i++) //get first 4 bytes
x = (x<<8) + uint(uint8(reason[i]));
if (x == 0x08c379a0) // abi.encodeWithSignature("Error(string)")
return "Error";
return "Unknown";
}
}
}