Skip to content

Commit

Permalink
check that unpack is tolerant of small sizes (#3416)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeyakovenko committed Aug 3, 2022
1 parent c986098 commit 22faa05
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions token/program/src/instruction.rs
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() {
for i in 0..255u8 {
let expect = Vec::from([i, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
_ = TokenInstruction::unpack(&expect[0..2]);
}
}
}

0 comments on commit 22faa05

Please sign in to comment.