Skip to content

Commit

Permalink
Fix incorrect indexing in decode of ReferenceIdRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
davidv1992 committed Dec 13, 2024
1 parent a611ad2 commit 09b7fb0
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion ntp-proto/src/packet/v5/extension_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ impl ReferenceIdRequest {
pub fn decode(msg: &[u8]) -> Result<Self, ParsingError<Infallible>> {
let payload_len =
u16::try_from(msg.len()).expect("NTP fields can not be longer than u16::MAX");
let offset_bytes: [u8; 2] = msg[0..2].try_into().unwrap();
let offset_bytes: [u8; 2] = msg
.get(0..2)
.ok_or(ParsingError::IncorrectLength)?
.try_into()
.unwrap();

Ok(Self {
payload_len,
Expand Down Expand Up @@ -222,4 +226,19 @@ mod tests {
assert_eq!(bits, bits2);
}
}

#[test]
fn test_reference_id_request_too_short() {
assert!(matches!(
ReferenceIdRequest::decode(&[]),
Err(ParsingError::IncorrectLength)
));
}

#[test]
fn test_reference_id_request_decode() {
let res = ReferenceIdRequest::decode(&[0, 2, 0, 0, 0]).unwrap();
assert_eq!(res.payload_len, 5);
assert_eq!(res.offset, 2);
}
}

0 comments on commit 09b7fb0

Please sign in to comment.