Skip to content

Commit

Permalink
Merge pull request #548 from Enet4/bug/parser/547-item-header-out-seq…
Browse files Browse the repository at this point in the history
…uence

[parser] Turn illegal item header parse into recoverable error
  • Loading branch information
Enet4 authored Jul 28, 2024
2 parents dba0412 + ff98911 commit af18fbe
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions parser/src/dataset/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ pub enum Error {
},
#[snafu(display("Unexpected item tag {} while reading element header", tag))]
UnexpectedItemTag { tag: Tag, backtrace: Backtrace },
#[snafu(display(
"Unexpected item header outside a dataset sequence at {} bytes",
bytes_read
))]
UnexpectedItemHeader {
bytes_read: u64,
backtrace: Backtrace,
},
/// Undefined pixel item length
UndefinedItemLength,
}
Expand Down Expand Up @@ -277,12 +285,23 @@ where
SequenceItemHeader::Item { len } => {
// entered a new item
self.in_sequence = false;

let last_delimiter = match self.seq_delimiters.last() {
Some(d) => d,
None => {
return Some(
UnexpectedItemHeaderSnafu {
bytes_read: self.parser.position(),
}
.fail(),
)
}
};
self.push_sequence_token(
SeqTokenType::Item,
len,
self.seq_delimiters.last()
.expect("item header should be read only inside an existing sequence")
.pixel_data);
last_delimiter.pixel_data,
);
// items can be empty
if len == Length(0) {
self.delimiter_check_pending = true;
Expand Down Expand Up @@ -1224,4 +1243,42 @@ mod tests {
// finished reading, peek should return None
assert!(iter.peek().unwrap().is_none());
}

#[test]
fn read_pixel_sequence_bad_item_end() {
#[rustfmt::skip]
static DATA: &[u8] = &[
0xe0, 0x7f, 0x10, 0x00, // (7FE0, 0010) PixelData
b'O', b'B', // VR
0x00, 0x00, // reserved
0xff, 0xff, 0xff, 0xff, // length: undefined
// -- 12 --
0xfe, 0xff, 0x00, 0xe0, // item start tag
0x00, 0x00, 0x00, 0x00, // item length: 0
// -- 20 --
0xfe, 0xff, 0x0d, 0xe0, // item end
0x00, 0x00, 0x00, 0x00, // length is always zero
// -- 28 --
0xfe, 0xff, 0x0d, 0xe0, // another item end (bad)
0x00, 0x00, 0x00, 0x00, //
// -- 36 --
0xfe, 0xff, 0x00, 0xe0, // another item start
0x00, 0x00, 0x00, 0x00, // item length: 0
];

let mut cursor = DATA;
let parser = StatefulDecoder::new(
&mut cursor,
ExplicitVRLittleEndianDecoder::default(),
LittleEndianBasicDecoder::default(),
SpecificCharacterSet::default(),
);
let mut dset_reader = DataSetReader::new(parser, Default::default());

let token_res = (&mut dset_reader)
.into_iter()
.collect::<Result<Vec<_>, _>>();
dbg!(&token_res);
assert!(token_res.is_err());
}
}

0 comments on commit af18fbe

Please sign in to comment.