File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff line change @@ -184,6 +184,8 @@ impl<T: AsRef<[u8]>> Packet<T> {
184
184
185
185
/// Ensure that no accessor method will panic if called.
186
186
/// Returns `Err(Error::Truncated)` if the buffer is too short.
187
+ /// Returns `Err(Error::Malformed)` if the header length is greater
188
+ /// than total length.
187
189
///
188
190
/// The result of this check is invalidated by calling [set_header_len]
189
191
/// and [set_total_len].
@@ -196,6 +198,8 @@ impl<T: AsRef<[u8]>> Packet<T> {
196
198
Err ( Error :: Truncated )
197
199
} else if len < self . header_len ( ) as usize {
198
200
Err ( Error :: Truncated )
201
+ } else if self . header_len ( ) as u16 > self . total_len ( ) {
202
+ Err ( Error :: Malformed )
199
203
} else if len < self . total_len ( ) as usize {
200
204
Err ( Error :: Truncated )
201
205
} else {
@@ -739,6 +743,13 @@ mod test {
739
743
assert_eq ! ( Repr :: parse( & packet, & ChecksumCapabilities :: default ( ) ) , Err ( Error :: Malformed ) ) ;
740
744
}
741
745
746
+ #[ test]
747
+ fn test_parse_total_len_less_than_header_len ( ) {
748
+ let mut bytes = vec ! [ 0 ; 40 ] ;
749
+ bytes[ 0 ] = 0x09 ;
750
+ assert_eq ! ( Packet :: new_checked( & mut bytes) , Err ( Error :: Malformed ) ) ;
751
+ }
752
+
742
753
#[ test]
743
754
fn test_emit ( ) {
744
755
let repr = packet_repr ( ) ;
You can’t perform that action at this time.
0 commit comments