Skip to content

Commit 7d89f23

Browse files
committed
Reject impossible Seq lengths in get_structure and add regression test
1 parent a2c39c4 commit 7d89f23

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

  • sv2/binary-sv2/src/datatypes/non_copy_data_types

sv2/binary-sv2/src/datatypes/non_copy_data_types/seq_inner.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ macro_rules! impl_codec_for_sequence {
223223
data: &[u8],
224224
) -> Result<Vec<crate::codec::decodable::FieldMarker>, Error> {
225225
let len = Self::expected_len(data)?;
226+
let available = data.len().saturating_sub(Self::HEADERSIZE);
227+
if len > available {
228+
return Err(Error::ReadError(data.len(), len + Self::HEADERSIZE));
229+
}
226230
let mut inner = Vec::with_capacity(len + Self::HEADERSIZE);
227231
for _ in 0..Self::HEADERSIZE {
228232
inner.push(FieldMarker::Primitive(PrimitiveMarker::U8));
@@ -516,3 +520,22 @@ impl<T: GetSize> GetSize for Sv2Option<'_, T> {
516520
size
517521
}
518522
}
523+
524+
#[cfg(test)]
525+
mod test {
526+
use crate::{Decodable, Seq064K};
527+
528+
#[test]
529+
fn get_structure_does_not_overallocate_from_tiny_header() {
530+
let data: [u8; 2] = [0xff, 0xff];
531+
match <Seq064K<'static, u8> as Decodable<'static>>::get_structure(&data) {
532+
Err(_) => {}
533+
Ok(markers) => assert!(
534+
markers.len() <= data.len(),
535+
"get_structure built {} markers from a {}-byte buffer",
536+
markers.len(),
537+
data.len()
538+
),
539+
}
540+
}
541+
}

0 commit comments

Comments
 (0)