File tree 1 file changed +18
-6
lines changed
1 file changed +18
-6
lines changed Original file line number Diff line number Diff line change @@ -119,19 +119,21 @@ impl<T: AsRef<[u8]>> Packet<T> {
119
119
/// Ensure that no accessor method will panic if called.
120
120
/// Returns `Err(Error::Truncated)` if the buffer is too short.
121
121
///
122
- /// The result of this check is invalidated by calling [set_header_len].
122
+ /// The result of this check is invalidated by calling [set_header_len]
123
+ /// and [set_total_len].
123
124
///
124
125
/// [set_header_len]: #method.set_header_len
126
+ /// [set_total_len]: #method.set_total_len
125
127
pub fn check_len ( & self ) -> Result < ( ) > {
126
128
let len = self . buffer . as_ref ( ) . len ( ) ;
127
129
if len < field:: DST_ADDR . end {
128
130
Err ( Error :: Truncated )
131
+ } else if len < self . header_len ( ) as usize {
132
+ Err ( Error :: Truncated )
133
+ } else if len < self . total_len ( ) as usize {
134
+ Err ( Error :: Truncated )
129
135
} else {
130
- if len < self . header_len ( ) as usize {
131
- Err ( Error :: Truncated )
132
- } else {
133
- Ok ( ( ) )
134
- }
136
+ Ok ( ( ) )
135
137
}
136
138
}
137
139
@@ -634,6 +636,16 @@ mod test {
634
636
PAYLOAD_BYTES . len( ) ) ;
635
637
}
636
638
639
+ #[ test]
640
+ fn test_total_len_overflow ( ) {
641
+ let mut bytes = vec ! [ ] ;
642
+ bytes. extend ( & PACKET_BYTES [ ..] ) ;
643
+ Packet :: new ( & mut bytes) . set_total_len ( 128 ) ;
644
+
645
+ assert_eq ! ( Packet :: new_checked( & bytes) . unwrap_err( ) ,
646
+ Error :: Truncated ) ;
647
+ }
648
+
637
649
static REPR_PACKET_BYTES : [ u8 ; 24 ] =
638
650
[ 0x45 , 0x00 , 0x00 , 0x18 ,
639
651
0x00 , 0x00 , 0x40 , 0x00 ,
You can’t perform that action at this time.
0 commit comments