Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 2a5acff

Browse files
jackcmaymvines
andauthored
Safer pack/unpack (#349)
* Safer pack/unpack * fix cli * clippy * fix swap * nit * clippy Co-authored-by: Michael Vines <[email protected]>
1 parent 74bf8a1 commit 2a5acff

File tree

10 files changed

+1159
-942
lines changed

10 files changed

+1159
-942
lines changed

Cargo.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

token-swap/program/src/processor.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use solana_sdk::{
1717
entrypoint::ProgramResult, info, program_error::PrintProgramError, program_error::ProgramError,
1818
pubkey::Pubkey,
1919
};
20+
use spl_token::pack::Pack;
2021
use std::mem::size_of;
2122

2223
impl State {
@@ -68,14 +69,19 @@ impl State {
6869
pub fn token_account_deserialize(
6970
info: &AccountInfo,
7071
) -> Result<spl_token::state::Account, Error> {
71-
Ok(*spl_token::state::unpack(&mut info.data.borrow_mut())
72-
.map_err(|_| Error::ExpectedAccount)?)
72+
spl_token::state::Account::unpack_from_slice(&info.data.borrow_mut())
73+
.map_err(|_| Error::ExpectedAccount)
74+
// Ok(*spl_token::state::unpack(&mut info.data.borrow_mut())
75+
// .map_err(|_| Error::ExpectedAccount)?)
7376
}
7477

7578
/// Deserializes a spl_token `Mint`.
7679
pub fn mint_deserialize(info: &AccountInfo) -> Result<spl_token::state::Mint, Error> {
77-
Ok(*spl_token::state::unpack(&mut info.data.borrow_mut())
78-
.map_err(|_| Error::ExpectedToken)?)
80+
spl_token::state::Mint::unpack_from_slice(&info.data.borrow_mut())
81+
.map_err(|_| Error::ExpectedAccount)
82+
83+
// Ok(*spl_token::state::unpack(&mut info.data.borrow_mut())
84+
// .map_err(|_| Error::ExpectedToken)?)
7985
}
8086

8187
/// Calculates the authority id by generating a program address.

token/cli/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use spl_token::{
2020
self,
2121
instruction::*,
2222
native_mint,
23-
state::{self, Account, Mint},
23+
pack::Pack,
24+
state::{Account, Mint},
2425
};
2526
use std::{mem::size_of, process::exit};
2627

@@ -218,9 +219,8 @@ fn command_burn(config: &Config, source: Pubkey, ui_amount: f64) -> CommmandResu
218219
.get_account_with_commitment(&source, config.commitment_config)?
219220
.value
220221
.unwrap_or_default();
221-
let mut data = source_account.data.to_vec();
222-
let mint_pubkey = state::unpack::<Account>(&mut data)?.mint;
223-
222+
let data = source_account.data.to_vec();
223+
let mint_pubkey = Account::unpack_from_slice(&data)?.mint;
224224
let amount = spl_token::ui_amount_to_amount(ui_amount, source_token_balance.decimals);
225225
let mut transaction = Transaction::new_with_payer(
226226
&[burn(

token/program/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ num-traits = "0.2"
2323
remove_dir_all = "=0.5.0"
2424
solana-sdk = { version = "1.3.4", default-features = false, optional = true }
2525
thiserror = "1.0"
26+
arrayref = "0.3.6"
27+
num_enum = "0.5.1"
2628

2729
[dev-dependencies]
2830
rand = { version = "0.7.0"}

0 commit comments

Comments
 (0)