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

guards #3416

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
23 changes: 23 additions & 0 deletions token/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ impl<'a> TokenInstruction<'a> {
10 => Self::FreezeAccount,
11 => Self::ThawAccount,
12 => {
if rest.len() < 8 {
return Err(TokenError::InvalidInstruction.into());
}
let (amount, rest) = rest.split_at(8);
let amount = amount
.try_into()
Expand All @@ -530,6 +533,9 @@ impl<'a> TokenInstruction<'a> {
Self::TransferChecked { amount, decimals }
}
13 => {
if rest.len() < 8 {
return Err(TokenError::InvalidInstruction.into());
}
let (amount, rest) = rest.split_at(8);
let amount = amount
.try_into()
Expand All @@ -541,6 +547,9 @@ impl<'a> TokenInstruction<'a> {
Self::ApproveChecked { amount, decimals }
}
14 => {
if rest.len() < 8 {
return Err(TokenError::InvalidInstruction.into());
}
let (amount, rest) = rest.split_at(8);
let amount = amount
.try_into()
Expand All @@ -552,6 +561,9 @@ impl<'a> TokenInstruction<'a> {
Self::MintToChecked { amount, decimals }
}
15 => {
if rest.len() < 8 {
return Err(TokenError::InvalidInstruction.into());
}
let (amount, rest) = rest.split_at(8);
let amount = amount
.try_into()
Expand Down Expand Up @@ -588,6 +600,9 @@ impl<'a> TokenInstruction<'a> {
21 => Self::GetAccountDataSize,
22 => Self::InitializeImmutableOwner,
23 => {
if rest.len() < 8 {
return Err(TokenError::InvalidInstruction.into());
}
let (amount, _rest) = rest.split_at(8);
let amount = amount
.try_into()
Expand Down Expand Up @@ -1689,4 +1704,12 @@ mod test {
let unpacked = TokenInstruction::unpack(&expect).unwrap();
assert_eq!(unpacked, check);
}

#[test]
fn test_instruction_unpack_panic() {
Copy link
Contributor

Choose a reason for hiding this comment

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

As a follow-up, perhaps something more exhaustive?

    #[test]
    fn test_instruction_unpack_panic() {
        for i in 0..255u8 {
            for j in 1..10 {
                let mut data = vec![0;j];
                data[0] = i;
                let _no_panic = TokenInstruction::unpack(&data);
            }
        }
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, on my list already :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I am fuzzing it with

    #[test]
    fn test_instruction_unpack_fuzz() {
        for _ in 0..10000000 {
            let r: usize = rand::thread_rng().gen_range(0..10);
            let data: Vec<u8> = (0..r).map(|_| rand::thread_rng().gen()).collect();
            _ = TokenInstruction::unpack(&data);
        }
    }

for i in 0..255u8 {
let expect = Vec::from([i, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
_ = TokenInstruction::unpack(&expect[0..2]);
}
}
}