Skip to content
Open
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
8 changes: 4 additions & 4 deletions p-token/src/processor/close_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
};

#[inline(always)]
#[allow(clippy::arithmetic_side_effects)]
pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
let [source_account_info, destination_account_info, authority_info, remaining @ ..] = accounts
else {
Expand Down Expand Up @@ -44,14 +45,13 @@ pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
}
}

let destination_starting_lamports = destination_account_info.lamports();
// SAFETY: single mutable borrow to `destination_account_info` lamports and
// there are no "active" borrows of `source_account_info` account data.
unsafe {
// Moves the lamports to the destination account.
*destination_account_info.borrow_mut_lamports_unchecked() = destination_starting_lamports
.checked_add(source_account_info.lamports())
.ok_or(TokenError::Overflow)?;
//
// Note: The total lamports supply is bound to `u64::MAX`.
*destination_account_info.borrow_mut_lamports_unchecked() += source_account_info.lamports();
// Closes the source account.
source_account_info.close_unchecked();
}
Expand Down
4 changes: 2 additions & 2 deletions p-token/src/processor/shared/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
};

#[inline(always)]
#[allow(clippy::arithmetic_side_effects)]
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: If you want to keep the clippy check, we can change the code to wrapping_*, but then it will also wrap for test builds

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we should keep it as it is then? It would be useful to panic on tests, although asserts should also pick up any issues.

pub fn process_burn(
accounts: &[AccountInfo],
amount: u64,
Expand Down Expand Up @@ -83,8 +84,7 @@ pub fn process_burn(
source_account.set_amount(updated_source_amount);
// Note: The amount of a token account is always within the range of the
// mint supply (`u64`).
let mint_supply = mint.supply().checked_sub(amount).unwrap();
mint.set_supply(mint_supply);
mint.set_supply(mint.supply() - amount);
}

Ok(())
Expand Down
7 changes: 3 additions & 4 deletions p-token/src/processor/withdraw_excess_lamports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ pub fn process_withdraw_excess_lamports(accounts: &[AccountInfo]) -> ProgramResu
source_starting_lamports - transfer_amount;
}

let destination_starting_lamports = destination_info.lamports();
// SAFETY: single mutable borrow to `destination_info` lamports.
unsafe {
// Moves the lamports to the destination account.
*destination_info.borrow_mut_lamports_unchecked() = destination_starting_lamports
.checked_add(transfer_amount)
.ok_or(TokenError::Overflow)?;
//
// Note: The total lamports supply is bound to `u64::MAX`.
*destination_info.borrow_mut_lamports_unchecked() += transfer_amount;
}

Ok(())
Expand Down