Skip to content

Commit

Permalink
Safer pack/unpack (solana-labs#349)
Browse files Browse the repository at this point in the history
* Safer pack/unpack

* fix cli

* clippy

* fix swap

* nit

* clippy

Co-authored-by: Michael Vines <mvines@gmail.com>
  • Loading branch information
jackcmay and mvines authored Aug 28, 2020
1 parent 74bf8a1 commit 2a5acff
Show file tree
Hide file tree
Showing 10 changed files with 1,159 additions and 942 deletions.
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions token-swap/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use solana_sdk::{
entrypoint::ProgramResult, info, program_error::PrintProgramError, program_error::ProgramError,
pubkey::Pubkey,
};
use spl_token::pack::Pack;
use std::mem::size_of;

impl State {
Expand Down Expand Up @@ -68,14 +69,19 @@ impl State {
pub fn token_account_deserialize(
info: &AccountInfo,
) -> Result<spl_token::state::Account, Error> {
Ok(*spl_token::state::unpack(&mut info.data.borrow_mut())
.map_err(|_| Error::ExpectedAccount)?)
spl_token::state::Account::unpack_from_slice(&info.data.borrow_mut())
.map_err(|_| Error::ExpectedAccount)
// Ok(*spl_token::state::unpack(&mut info.data.borrow_mut())
// .map_err(|_| Error::ExpectedAccount)?)
}

/// Deserializes a spl_token `Mint`.
pub fn mint_deserialize(info: &AccountInfo) -> Result<spl_token::state::Mint, Error> {
Ok(*spl_token::state::unpack(&mut info.data.borrow_mut())
.map_err(|_| Error::ExpectedToken)?)
spl_token::state::Mint::unpack_from_slice(&info.data.borrow_mut())
.map_err(|_| Error::ExpectedAccount)

// Ok(*spl_token::state::unpack(&mut info.data.borrow_mut())
// .map_err(|_| Error::ExpectedToken)?)
}

/// Calculates the authority id by generating a program address.
Expand Down
8 changes: 4 additions & 4 deletions token/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use spl_token::{
self,
instruction::*,
native_mint,
state::{self, Account, Mint},
pack::Pack,
state::{Account, Mint},
};
use std::{mem::size_of, process::exit};

Expand Down Expand Up @@ -218,9 +219,8 @@ fn command_burn(config: &Config, source: Pubkey, ui_amount: f64) -> CommmandResu
.get_account_with_commitment(&source, config.commitment_config)?
.value
.unwrap_or_default();
let mut data = source_account.data.to_vec();
let mint_pubkey = state::unpack::<Account>(&mut data)?.mint;

let data = source_account.data.to_vec();
let mint_pubkey = Account::unpack_from_slice(&data)?.mint;
let amount = spl_token::ui_amount_to_amount(ui_amount, source_token_balance.decimals);
let mut transaction = Transaction::new_with_payer(
&[burn(
Expand Down
2 changes: 2 additions & 0 deletions token/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ num-traits = "0.2"
remove_dir_all = "=0.5.0"
solana-sdk = { version = "1.3.4", default-features = false, optional = true }
thiserror = "1.0"
arrayref = "0.3.6"
num_enum = "0.5.1"

[dev-dependencies]
rand = { version = "0.7.0"}
Expand Down
Loading

0 comments on commit 2a5acff

Please sign in to comment.