lib.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. use anchor_lang::prelude::*;
  2. declare_id!("overf1owChecks11111111111111111111111111111");
  3. #[program]
  4. pub mod overflow_checks {
  5. use super::*;
  6. pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
  7. *ctx.accounts.account = OverflowAccount {
  8. i8_left: i8::MIN,
  9. i8_right: i8::MAX,
  10. i16_left: i16::MIN,
  11. i16_right: i16::MAX,
  12. i32_left: i32::MIN,
  13. i32_right: i32::MAX,
  14. i64_left: i64::MIN,
  15. i64_right: i64::MAX,
  16. i128_left: i128::MIN,
  17. i128_right: i128::MAX,
  18. u8_left: u8::MIN,
  19. u8_right: u8::MAX,
  20. u16_left: u16::MIN,
  21. u16_right: u16::MAX,
  22. u32_left: u32::MIN,
  23. u32_right: u32::MAX,
  24. u64_left: u64::MIN,
  25. u64_right: u64::MAX,
  26. u128_left: u128::MIN,
  27. u128_right: u128::MAX,
  28. };
  29. Ok(())
  30. }
  31. pub fn test_overflow_add(ctx: Context<TestOverflowAdd>) -> Result<()> {
  32. let account = &mut ctx.accounts.account;
  33. account.i8_right += 1;
  34. account.i16_right += 1;
  35. account.i32_right += 1;
  36. account.i64_right += 1;
  37. account.i128_right += 1;
  38. account.u8_right += 1;
  39. account.u16_right += 1;
  40. account.u32_right += 1;
  41. account.u64_right += 1;
  42. account.u128_right += 1;
  43. Ok(())
  44. }
  45. pub fn test_overflow_sub(ctx: Context<TestOverflowSub>) -> Result<()> {
  46. let account = &mut ctx.accounts.account;
  47. account.i8_left -= 1;
  48. account.i16_left -= 1;
  49. account.i32_left -= 1;
  50. account.i64_left -= 1;
  51. account.i128_left -= 1;
  52. account.u8_left -= 1;
  53. account.u16_left -= 1;
  54. account.u32_left -= 1;
  55. account.u64_left -= 1;
  56. account.u128_left -= 1;
  57. Ok(())
  58. }
  59. pub fn test_overflow_mul(ctx: Context<TestOverflowMul>) -> Result<()> {
  60. let account = &mut ctx.accounts.account;
  61. account.i8_right *= 2;
  62. account.i16_right *= 2;
  63. account.i32_right *= 2;
  64. account.i64_right *= 2;
  65. account.i128_right *= 2;
  66. account.u8_right *= 2;
  67. account.u16_right *= 2;
  68. account.u32_right *= 2;
  69. account.u64_right *= 2;
  70. account.u128_right *= 2;
  71. Ok(())
  72. }
  73. }
  74. #[derive(Accounts)]
  75. pub struct Initialize<'info> {
  76. #[account(init, payer = payer, space = 8 + OverflowAccount::INIT_SPACE)]
  77. pub account: Account<'info, OverflowAccount>,
  78. #[account(mut)]
  79. pub payer: Signer<'info>,
  80. pub system_program: Program<'info, System>,
  81. }
  82. #[derive(Accounts)]
  83. pub struct TestOverflowAdd<'info> {
  84. #[account(mut)]
  85. pub account: Account<'info, OverflowAccount>,
  86. }
  87. #[derive(Accounts)]
  88. pub struct TestOverflowSub<'info> {
  89. #[account(mut)]
  90. pub account: Account<'info, OverflowAccount>,
  91. }
  92. #[derive(Accounts)]
  93. pub struct TestOverflowMul<'info> {
  94. #[account(mut)]
  95. pub account: Account<'info, OverflowAccount>,
  96. }
  97. #[account]
  98. #[derive(InitSpace)]
  99. pub struct OverflowAccount {
  100. pub i8_left: i8,
  101. pub i8_right: i8,
  102. pub i16_left: i16,
  103. pub i16_right: i16,
  104. pub i32_left: i32,
  105. pub i32_right: i32,
  106. pub i64_left: i64,
  107. pub i64_right: i64,
  108. pub i128_left: i128,
  109. pub i128_right: i128,
  110. pub u8_left: u8,
  111. pub u8_right: u8,
  112. pub u16_left: u16,
  113. pub u16_right: u16,
  114. pub u32_left: u32,
  115. pub u32_right: u32,
  116. pub u64_left: u64,
  117. pub u64_right: u64,
  118. pub u128_left: u128,
  119. pub u128_right: u128,
  120. }