From aa27fc6f132e89ea765ac1b4a4c09ba17f7ca8ad Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Tue, 30 Apr 2024 18:52:20 +0000 Subject: [PATCH] overflow check --- src/error.rs | 4 ++-- src/lib.rs | 1 - src/processor/update_tolerance.rs | 13 ++++++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/error.rs b/src/error.rs index 452e229..03e9e30 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,8 +17,8 @@ pub enum OreError { StakeTooLarge = 4, #[error("The clock time is invalid")] ClockInvalid = 5, - #[error("The tolerance cannot be negative")] - ToleranceNegative = 6, + #[error("The tolerance cannot exceed i64 max value")] + ToleranceInvalid = 6, } impl From for ProgramError { diff --git a/src/lib.rs b/src/lib.rs index 17c0395..d1a0e83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,6 @@ use solana_program::{ program_error::ProgramError, pubkey::Pubkey, }; -// TODO Admin fn for spam/liveness tolerances? // TODO Alternative to bincode? // TODO Is downgrade necessary? diff --git a/src/processor/update_tolerance.rs b/src/processor/update_tolerance.rs index 25ea1c9..8a55294 100644 --- a/src/processor/update_tolerance.rs +++ b/src/processor/update_tolerance.rs @@ -30,14 +30,17 @@ pub fn process_update_tolerance<'a, 'info>( return Err(ProgramError::MissingRequiredSignature); } + // Overflow checks + if args.tolerance_liveness.gt(&(i64::MAX as u64)) { + return Err(OreError::ToleranceInvalid.into()); + } + if args.tolerance_spam.gt(&(i64::MAX as u64)) { + return Err(OreError::ToleranceInvalid.into()); + } + // Update tolerances config.tolerance_liveness = args.tolerance_liveness as i64; config.tolerance_spam = args.tolerance_spam as i64; - // Sanity checks - if config.tolerance_liveness.lt(&0) || config.tolerance_spam.lt(&0) { - return Err(OreError::ToleranceNegative.into()); - } - Ok(()) }