From 13e13d836f8342537bd14d67512ac4f98719bbd9 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Wed, 28 Jul 2021 12:05:10 +0200 Subject: [PATCH] tape: changer: handle libraries that sends wrong amount of data if the library sends more data than advertised, simply cut it off, but if it sends less data, bail out (depending on how much data is missing, trying to parse it could lead to a panic, so bail out early) Signed-off-by: Dominik Csapak --- src/tape/changer/sg_pt_changer.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/tape/changer/sg_pt_changer.rs b/src/tape/changer/sg_pt_changer.rs index 2c5434f6..7ff9bc9d 100644 --- a/src/tape/changer/sg_pt_changer.rs +++ b/src/tape/changer/sg_pt_changer.rs @@ -692,6 +692,15 @@ fn decode_element_status_page( bail!("got wrong first_element_address_reported"); // sanity check } + let len = head.byte_count_of_report_available; + let len = ((len[0] as usize) << 16) + ((len[1] as usize) << 8) + (len[2] as usize); + + if len < reader.len() { + reader = &reader[..len]; + } else if len > reader.len() { + bail!("wrong amount of data: expected {}, got {}", len, reader.len()); + } + loop { if reader.is_empty() { break;