Skip to content

Commit 9656fda

Browse files
committed
Check that value offset does not extend pass end of message
1 parent abc7f8b commit 9656fda

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/error.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ pub enum Error {
2525
/// The associated byte sequence does not correspond to a valid Roughtime tag.
2626
InvalidTag(Box<[u8]>),
2727

28+
/// Invalid number of tags specified
29+
InvalidNumTags(u32),
30+
31+
/// Tag value length exceeds length of source bytes
32+
InvalidValueLength(Tag, u32),
33+
2834
/// Encoding failed. The associated `std::io::Error` should provide more information.
2935
EncodingFailure(std::io::Error),
3036

@@ -37,9 +43,6 @@ pub enum Error {
3743
/// Offset is outside of valid message range
3844
InvalidOffsetValue(u32),
3945

40-
/// Invalid number of tags specified
41-
InvalidNumTags(u32),
42-
4346
/// Could not convert bytes to message because bytes were too short
4447
MessageTooShort,
4548

src/message.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,19 @@ impl RtMessage {
112112
// as an offset from the end of the header
113113
let msg_end = bytes.len() - header_end;
114114

115-
assert_eq!(offsets.len(), tags.len() - 1);
116-
117115
for (tag, (value_start, value_end)) in tags.into_iter().zip(
118116
once(&0)
119117
.chain(offsets.iter())
120-
.zip(offsets.iter().chain(once(&msg_end))),
118+
.zip(offsets.iter().chain(once(&msg_end)))
121119
) {
122-
let value = bytes[(header_end + value_start)..(header_end + value_end)].to_vec();
120+
let start_idx = header_end + value_start;
121+
let end_idx = header_end + value_end;
122+
123+
if end_idx > msg_end || start_idx > end_idx {
124+
return Err(Error::InvalidValueLength(tag, end_idx as u32));
125+
}
126+
127+
let value = bytes[start_idx..end_idx].to_vec();
123128
rt_msg.add_field(tag, &value)?;
124129
}
125130

0 commit comments

Comments
 (0)