runtime_errors.sol 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. contract RuntimeErrors {
  2. bytes b = hex"0000_00fa";
  3. uint256[] arr;
  4. child public c;
  5. constructor() {}
  6. function print_test(int8 num) public returns (int8) {
  7. print("Hello world!");
  8. require(num > 10, "sesa");
  9. assert(num > 10);
  10. int8 ovf = num + 120;
  11. print("x = {}".format(ovf));
  12. return ovf;
  13. }
  14. function math_overflow(int8 num) public returns (int8) {
  15. int8 ovf = num + 120;
  16. print("x = {}".format(ovf));
  17. return ovf;
  18. }
  19. function require_test(int256 num) public returns (int8) {
  20. require(num > 10, "sesa");
  21. return 0;
  22. }
  23. // assert failure
  24. function assert_test(int256 num) public returns (int8) {
  25. assert(num > 10);
  26. return 0;
  27. }
  28. // storage index out of bounds
  29. function set_storage_bytes() public returns (bytes) {
  30. bytes sesa = new bytes(1);
  31. b[5] = sesa[0];
  32. return sesa;
  33. }
  34. // storage array index out of bounds
  35. function get_storage_bytes() public returns (bytes) {
  36. bytes sesa = new bytes(1);
  37. sesa[0] = b[5];
  38. return sesa;
  39. }
  40. // value transfer failure
  41. function transfer_abort() public {
  42. address a = address(0);
  43. payable(a).transfer(10);
  44. }
  45. // pop from empty storage array
  46. function pop_empty_storage() public {
  47. arr.pop();
  48. }
  49. // external call failed
  50. function call_ext(callee_error e) public {
  51. e.callee_func();
  52. }
  53. // contract creation failed (contract was deplyed with no value)
  54. function create_child() public {
  55. c = new child();
  56. }
  57. // non payable function dont_pay_me received value
  58. function dont_pay_me() public {}
  59. function pay_me() public payable {}
  60. function i_will_revert() public {
  61. revert();
  62. }
  63. function write_integer_failure(uint256 buf_size) public {
  64. bytes smol_buf = new bytes(buf_size);
  65. smol_buf.writeUint32LE(350, 20);
  66. }
  67. function write_bytes_failure(uint256 buf_size) public {
  68. bytes data = new bytes(10);
  69. bytes smol_buf = new bytes(buf_size);
  70. smol_buf.writeBytes(data, 0);
  71. }
  72. function read_integer_failure(uint32 offset) public {
  73. bytes smol_buf = new bytes(1);
  74. smol_buf.readUint16LE(offset);
  75. }
  76. // truncated type overflows
  77. function trunc_failure(uint256 input) public returns (uint256[]) {
  78. uint256[] a = new uint256[](input);
  79. return a;
  80. }
  81. function out_of_bounds(uint256 input) public returns (uint256) {
  82. uint256[] a = new uint256[](input);
  83. return a[20];
  84. }
  85. function invalid_instruction() public {
  86. assembly {
  87. invalid()
  88. }
  89. }
  90. function byte_cast_failure(uint256 num) public returns (bytes) {
  91. bytes smol_buf = new bytes(num);
  92. bytes32 b32 = bytes32(smol_buf);
  93. return b32;
  94. }
  95. }
  96. contract callee_error {
  97. constructor() {}
  98. function callee_func() public {
  99. revert();
  100. }
  101. }
  102. contract child {
  103. constructor() payable {}
  104. function say_my_name() public pure returns (string memory) {
  105. return "child";
  106. }
  107. }