From 22b4923ef27a5cd0b9944a9668830682fb1bf0a9 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Fri, 21 Jan 2022 08:21:00 +0100 Subject: [PATCH] Address all math issues reported by soteria None of these should have been a real issue. --- .../src/instructions/log_voter_info.rs | 7 ++++++- programs/voter-stake-registry/src/state/deposit_entry.rs | 5 ++++- programs/voter-stake-registry/src/state/lockup.rs | 6 +++++- programs/voter-stake-registry/src/state/registrar.rs | 6 +++++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/programs/voter-stake-registry/src/instructions/log_voter_info.rs b/programs/voter-stake-registry/src/instructions/log_voter_info.rs index 1b82538..152d3f2 100644 --- a/programs/voter-stake-registry/src/instructions/log_voter_info.rs +++ b/programs/voter-stake-registry/src/instructions/log_voter_info.rs @@ -54,7 +54,12 @@ pub fn log_voter_info(ctx: Context, deposit_entry_begin: u8) -> Re end_timestamp: (lockup.kind != LockupKind::Constant).then(|| end_ts), vesting: lockup.kind.is_vesting().then(|| VestingInfo { rate: deposit.amount_initially_locked_native / periods_total, - next_timestamp: end_ts - (periods_left - 1) * lockup.kind.period_secs(), + next_timestamp: end_ts.saturating_sub( + periods_left + .saturating_sub(1) + .checked_mul(lockup.kind.period_secs()) + .unwrap(), + ), }), }); diff --git a/programs/voter-stake-registry/src/state/deposit_entry.rs b/programs/voter-stake-registry/src/state/deposit_entry.rs index d0944dc..06e5453 100644 --- a/programs/voter-stake-registry/src/state/deposit_entry.rs +++ b/programs/voter-stake-registry/src/state/deposit_entry.rs @@ -342,7 +342,10 @@ impl DepositEntry { vested_amount <= self.amount_initially_locked_native, InternalProgramError ); - self.amount_initially_locked_native -= vested_amount; + self.amount_initially_locked_native = self + .amount_initially_locked_native + .checked_sub(vested_amount) + .unwrap(); self.lockup.remove_past_periods(curr_ts)?; require!(self.vested(curr_ts)? == 0, InternalProgramError); Ok(()) diff --git a/programs/voter-stake-registry/src/state/lockup.rs b/programs/voter-stake-registry/src/state/lockup.rs index a5586d3..f24918f 100644 --- a/programs/voter-stake-registry/src/state/lockup.rs +++ b/programs/voter-stake-registry/src/state/lockup.rs @@ -113,7 +113,11 @@ impl Lockup { if curr_ts < self.start_ts { return self.periods_total(); } - Ok((self.seconds_left(curr_ts) + period_secs - 1) / period_secs) + Ok(self + .seconds_left(curr_ts) + .checked_add(period_secs.saturating_sub(1)) + .unwrap() + / period_secs) } /// Returns the current period in the vesting schedule. diff --git a/programs/voter-stake-registry/src/state/registrar.rs b/programs/voter-stake-registry/src/state/registrar.rs index a21e649..4aac72f 100644 --- a/programs/voter-stake-registry/src/state/registrar.rs +++ b/programs/voter-stake-registry/src/state/registrar.rs @@ -26,7 +26,11 @@ const_assert!(std::mem::size_of::() == 5 * 32 + 4 * 120 + 8 + 1 + 31) impl Registrar { pub fn clock_unix_timestamp(&self) -> i64 { - Clock::get().unwrap().unix_timestamp + self.time_offset + Clock::get() + .unwrap() + .unix_timestamp + .checked_add(self.time_offset) + .unwrap() } pub fn voting_mint_config_index(&self, mint: Pubkey) -> Result {