overflow check

This commit is contained in:
Hardhat Chad
2024-04-30 18:52:20 +00:00
parent 93657d02cc
commit aa27fc6f13
3 changed files with 10 additions and 8 deletions

View File

@@ -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<OreError> for ProgramError {

View File

@@ -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?

View File

@@ -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(())
}