@@ -519,6 +519,9 @@ impl<'a> TokenInstruction<'a> {
519
519
10 => Self :: FreezeAccount ,
520
520
11 => Self :: ThawAccount ,
521
521
12 => {
522
+ if rest. len ( ) < 8 {
523
+ return Err ( TokenError :: InvalidInstruction . into ( ) ) ;
524
+ }
522
525
let ( amount, rest) = rest. split_at ( 8 ) ;
523
526
let amount = amount
524
527
. try_into ( )
@@ -530,6 +533,9 @@ impl<'a> TokenInstruction<'a> {
530
533
Self :: TransferChecked { amount, decimals }
531
534
}
532
535
13 => {
536
+ if rest. len ( ) < 8 {
537
+ return Err ( TokenError :: InvalidInstruction . into ( ) ) ;
538
+ }
533
539
let ( amount, rest) = rest. split_at ( 8 ) ;
534
540
let amount = amount
535
541
. try_into ( )
@@ -541,6 +547,9 @@ impl<'a> TokenInstruction<'a> {
541
547
Self :: ApproveChecked { amount, decimals }
542
548
}
543
549
14 => {
550
+ if rest. len ( ) < 8 {
551
+ return Err ( TokenError :: InvalidInstruction . into ( ) ) ;
552
+ }
544
553
let ( amount, rest) = rest. split_at ( 8 ) ;
545
554
let amount = amount
546
555
. try_into ( )
@@ -552,6 +561,9 @@ impl<'a> TokenInstruction<'a> {
552
561
Self :: MintToChecked { amount, decimals }
553
562
}
554
563
15 => {
564
+ if rest. len ( ) < 8 {
565
+ return Err ( TokenError :: InvalidInstruction . into ( ) ) ;
566
+ }
555
567
let ( amount, rest) = rest. split_at ( 8 ) ;
556
568
let amount = amount
557
569
. try_into ( )
@@ -588,6 +600,9 @@ impl<'a> TokenInstruction<'a> {
588
600
21 => Self :: GetAccountDataSize ,
589
601
22 => Self :: InitializeImmutableOwner ,
590
602
23 => {
603
+ if rest. len ( ) < 8 {
604
+ return Err ( TokenError :: InvalidInstruction . into ( ) ) ;
605
+ }
591
606
let ( amount, _rest) = rest. split_at ( 8 ) ;
592
607
let amount = amount
593
608
. try_into ( )
@@ -1689,4 +1704,12 @@ mod test {
1689
1704
let unpacked = TokenInstruction :: unpack ( & expect) . unwrap ( ) ;
1690
1705
assert_eq ! ( unpacked, check) ;
1691
1706
}
1707
+
1708
+ #[ test]
1709
+ fn test_instruction_unpack_panic ( ) {
1710
+ for i in 0 ..255u8 {
1711
+ let expect = Vec :: from ( [ i, 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 ] ) ;
1712
+ _ = TokenInstruction :: unpack ( & expect[ 0 ..2 ] ) ;
1713
+ }
1714
+ }
1692
1715
}
0 commit comments