Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove unsafe casts #507

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion staking/programs/integrity-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub mod integrity_pool {

// update publisher accounting
let position = stake_account_positions
.read_position(position_index as usize)?
.read_position(position_index.into())?
.ok_or(IntegrityPoolError::ThisCodeShouldBeUnreachable)?;

let position_state =
Expand Down
15 changes: 9 additions & 6 deletions staking/programs/integrity-pool/src/state/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ use {
},
std::{
cmp::min,
convert::TryInto,
convert::{
TryFrom,
TryInto,
},
},
};

Expand Down Expand Up @@ -121,11 +124,11 @@ impl PoolData {
_ => continue,
}

let mut last_event_index: usize = self.num_events as usize;
let mut last_event_index: usize = self.num_events.try_into()?;
loop {
// prevent infinite loop and double counting events
// by breaking the loop when visiting all events
if self.num_events as usize == last_event_index + MAX_EVENTS {
if usize::try_from(self.num_events)? == last_event_index + MAX_EVENTS {
break;
}

Expand Down Expand Up @@ -195,7 +198,7 @@ impl PoolData {

for epoch in self.last_updated_epoch..current_epoch {
let event =
self.get_event_mut((self.num_events + epoch - self.last_updated_epoch) as usize);
self.get_event_mut((self.num_events + epoch - self.last_updated_epoch).try_into()?);
event.epoch = epoch;
event.y = y;
}
Expand Down Expand Up @@ -253,7 +256,7 @@ impl PoolData {
i += 1;
}

for j in 0..(publisher_caps.num_publishers() as usize) {
for j in 0..publisher_caps.num_publishers() as usize {
Copy link
Contributor Author

@guibescos guibescos Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weirdly the u16 to usize conversion error can't be converted to AnchorError using the ? so I'm not updating this

// Silently ignore if there are more publishers than MAX_PUBLISHERS
if !existing_publishers.get(j) && i < MAX_PUBLISHERS {
self.publishers[i] = publisher_caps.get_cap(j).pubkey;
Expand Down Expand Up @@ -294,7 +297,7 @@ impl PoolData {
.unwrap_or(0)
.try_into()?;

self.get_event_mut((self.num_events + epoch - self.last_updated_epoch) as usize)
self.get_event_mut((self.num_events + epoch - self.last_updated_epoch).try_into()?)
.event_data[publisher_index] = PublisherEventData {
self_reward_ratio,
other_reward_ratio,
Expand Down
7 changes: 5 additions & 2 deletions staking/programs/publisher-caps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use {
WormholePayload,
},
},
std::convert::TryInto,
std::convert::{
TryFrom,
TryInto,
},
wormhole_solana_vaas::zero_copy::VaaAccount,
};

Expand Down Expand Up @@ -69,7 +72,7 @@ pub mod publisher_caps {
let account_info = ctx.accounts.publisher_caps.to_account_info();
sol_memcpy(
&mut account_info.try_borrow_mut_data().unwrap()
[PublisherCaps::HEADER_LEN + index as usize..],
[PublisherCaps::HEADER_LEN + usize::try_from(index)?..],
&data,
data.len(),
);
Expand Down
10 changes: 5 additions & 5 deletions staking/programs/staking/src/state/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ impl<'a> DynamicPositionArray<'a> {

// Makes position at index i none, and swaps positions to preserve the invariant
pub fn make_none(&mut self, i: usize, next_index: &mut u8) -> Result<()> {
if (*next_index as usize) <= i {
if usize::from(*next_index) <= i {
return Err(error!(ErrorCode::PositionOutOfBounds));
}
*next_index -= 1;
let positions = self.get_positions_slice()?;
positions[i] = positions[*next_index as usize];
None::<Option<Position>>.try_write(&mut positions[*next_index as usize])
positions[i] = positions[usize::from(*next_index)];
None::<Option<Position>>.try_write(&mut positions[usize::from(*next_index)])
}

pub fn write_position(&mut self, i: usize, &position: &Position) -> Result<()> {
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<'a> DynamicPositionArray<'a> {
next_index: &mut u8,
target_with_parameters: TargetWithParameters,
) -> Result<()> {
let mut i = *next_index as usize;
let mut i = usize::from(*next_index);
while i >= 1 {
i -= 1;
if let Some(position) = self.read_position(i)? {
Expand Down Expand Up @@ -571,7 +571,7 @@ impl Position {
let has_activated: bool = self.activation_epoch <= current_epoch;
let unlock_started: bool = unlocking_start <= current_epoch;
let unlock_ended: bool =
unlocking_start + unlocking_duration as u64 <= current_epoch;
unlocking_start + u64::from(unlocking_duration) <= current_epoch;

if has_activated && !unlock_started {
Ok(PositionState::PREUNLOCKING)
Expand Down
29 changes: 18 additions & 11 deletions staking/programs/staking/src/state/vesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ impl VestingSchedule {
// accounts simpler.
let remaining_periods = num_periods.saturating_sub(periods_passed);

(((remaining_periods as u128) * (initial_balance as u128)) / (num_periods as u128))
.try_into()
.unwrap()
(((u128::from(remaining_periods)) * (u128::from(initial_balance)))
/ (u128::from(num_periods)))
.try_into()
.unwrap()
}
}
}
Expand Down Expand Up @@ -257,15 +258,18 @@ impl VestingSchedule {
num_periods,
} => Ok((
VestingSchedule::PeriodicVesting {
initial_balance: ((remaining_amount as u128) * (*initial_balance as u128)
/ (total_amount as u128)) as u64,
initial_balance: (u128::from(remaining_amount) * u128::from(*initial_balance)
/ u128::from(total_amount))
.try_into()?,
start_date: *start_date,
period_duration: *period_duration,
num_periods: *num_periods,
},
VestingSchedule::PeriodicVesting {
initial_balance: ((transferred_amount as u128) * (*initial_balance as u128)
/ (total_amount as u128)) as u64,
initial_balance: (u128::from(transferred_amount)
* u128::from(*initial_balance)
/ u128::from(total_amount))
.try_into()?,
start_date: *start_date,
period_duration: *period_duration,
num_periods: *num_periods,
Expand All @@ -277,14 +281,17 @@ impl VestingSchedule {
num_periods,
} => Ok((
VestingSchedule::PeriodicVestingAfterListing {
initial_balance: ((remaining_amount as u128) * (*initial_balance as u128)
/ (total_amount as u128)) as u64,
initial_balance: (u128::from(remaining_amount) * u128::from(*initial_balance)
/ u128::from(total_amount))
.try_into()?,
period_duration: *period_duration,
num_periods: *num_periods,
},
VestingSchedule::PeriodicVestingAfterListing {
initial_balance: ((transferred_amount as u128) * (*initial_balance as u128)
/ (total_amount as u128)) as u64,
initial_balance: (u128::from(transferred_amount)
* u128::from(*initial_balance)
/ u128::from(total_amount))
.try_into()?,
period_duration: *period_duration,
num_periods: *num_periods,
},
Expand Down
4 changes: 2 additions & 2 deletions staking/programs/staking/src/utils/voter_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub fn compute_voter_weight(
}
}
}
let voter_weight: u64 = ((raw_voter_weight as u128) * (total_supply as u128))
.checked_div(current_locked as u128)
let voter_weight: u64 = ((u128::from(raw_voter_weight)) * (u128::from(total_supply)))
.checked_div(u128::from(current_locked))
.unwrap_or(0_u128)
.try_into()
.map_err(|_| ErrorCode::GenericOverflow)?;
Expand Down
Loading