From 6a15cce5400d46dff8b1d5a8aeeff862c3bbdde5 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 13 Apr 2021 11:44:06 +0200 Subject: [PATCH] tape: SgTapeReader::read_block - disable reading beyond EOF --- src/tape/drive/lto/sg_tape.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/tape/drive/lto/sg_tape.rs b/src/tape/drive/lto/sg_tape.rs index 3f73e025..7faa1639 100644 --- a/src/tape/drive/lto/sg_tape.rs +++ b/src/tape/drive/lto/sg_tape.rs @@ -694,19 +694,30 @@ impl Drop for SgTape { pub struct SgTapeReader<'a> { sg_tape: &'a mut SgTape, + end_of_file: bool, } impl <'a> SgTapeReader<'a> { pub fn new(sg_tape: &'a mut SgTape) -> Self { - Self { sg_tape } + Self { sg_tape, end_of_file: false, } } } impl <'a> BlockRead for SgTapeReader<'a> { fn read_block(&mut self, buffer: &mut [u8]) -> Result { - self.sg_tape.read_block(buffer) + if self.end_of_file { + return Err(BlockReadError::Error(proxmox::io_format_err!("detected read after EOF!"))); + } + match self.sg_tape.read_block(buffer) { + Ok(usize) => Ok(usize), + Err(BlockReadError::EndOfFile) => { + self.end_of_file = true; + Err(BlockReadError::EndOfFile) + }, + Err(err) => Err(err), + } } }