runtime_errors.sol 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import 'solana';
  2. contract RuntimeErrors {
  3. bytes b = hex"0000_00fa";
  4. uint256[] arr;
  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(1e15);
  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() public {
  51. Creature.say_my_name();
  52. }
  53. function i_will_revert() public {
  54. revert();
  55. }
  56. function write_integer_failure(uint256 buf_size) public {
  57. bytes smol_buf = new bytes(buf_size);
  58. smol_buf.writeUint32LE(350, 20);
  59. }
  60. function write_bytes_failure(uint256 buf_size) public {
  61. bytes data = new bytes(10);
  62. bytes smol_buf = new bytes(buf_size);
  63. smol_buf.writeBytes(data, 0);
  64. }
  65. function read_integer_failure(uint32 offset) public {
  66. bytes smol_buf = new bytes(1);
  67. smol_buf.readUint16LE(offset);
  68. }
  69. // truncated type overflows
  70. function trunc_failure(uint256 input) public returns (uint256[]) {
  71. uint256[] a = new uint256[](input);
  72. return a;
  73. }
  74. function out_of_bounds(uint256 input) public returns (uint256) {
  75. uint256[] a = new uint256[](input);
  76. return a[20];
  77. }
  78. function invalid_instruction() public {
  79. assembly {
  80. invalid()
  81. }
  82. }
  83. function byte_cast_failure(uint256 num) public returns (bytes) {
  84. bytes smol_buf = new bytes(num);
  85. bytes32 b32 = bytes32(smol_buf);
  86. return b32;
  87. }
  88. }
  89. @program_id("Cre7AzxtwSxXwU2jekYtCAQ57DkBhY9SjGDLdcrwhAo6")
  90. contract Creature {
  91. @payer(payer)
  92. @space(511 + 7)
  93. constructor(address payer) {
  94. print("In child constructor");
  95. }
  96. function say_my_name() public pure returns (string memory) {
  97. print("say_my_name");
  98. return "child_contract";
  99. }
  100. }
  101. contract calle_contract {
  102. constructor() {}
  103. function calle_contract_func() public {
  104. revert();
  105. }
  106. }