File tree Expand file tree Collapse file tree 1 file changed +18
-6
lines changed
Expand file tree Collapse file tree 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> {
119119 /// Ensure that no accessor method will panic if called.
120120 /// Returns `Err(Error::Truncated)` if the buffer is too short.
121121 ///
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].
123124 ///
124125 /// [set_header_len]: #method.set_header_len
126+ /// [set_total_len]: #method.set_total_len
125127 pub fn check_len ( & self ) -> Result < ( ) > {
126128 let len = self . buffer . as_ref ( ) . len ( ) ;
127129 if len < field:: DST_ADDR . end {
128130 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 )
129135 } else {
130- if len < self . header_len ( ) as usize {
131- Err ( Error :: Truncated )
132- } else {
133- Ok ( ( ) )
134- }
136+ Ok ( ( ) )
135137 }
136138 }
137139
@@ -634,6 +636,16 @@ mod test {
634636 PAYLOAD_BYTES . len( ) ) ;
635637 }
636638
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+
637649 static REPR_PACKET_BYTES : [ u8 ; 24 ] =
638650 [ 0x45 , 0x00 , 0x00 , 0x18 ,
639651 0x00 , 0x00 , 0x40 , 0x00 ,
You can’t perform that action at this time.
0 commit comments