From 52f954a3961a683e1b317c63067063082501ce3d Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Mon, 15 Nov 2021 17:27:15 +0100 Subject: [PATCH] core/byteio: check for end boundary in BoundedFileReader reads --- nihav-core/src/io/byteio.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index b3d0621..2bac190 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -717,6 +717,12 @@ impl ByteIO for BoundedFileReader { } fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + if let Some(epos) = self.end { + if self.real_tell() >= epos { + self.eof = true; + return Err(ByteIOError::EOF); + } + } let len = self.max_read_len(buf.len()); match self.file.read_exact(&mut buf[..len]) { Ok(()) if len == buf.len() => Ok(buf.len()), @@ -736,6 +742,12 @@ impl ByteIO for BoundedFileReader { } fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult { + if let Some(epos) = self.end { + if self.real_tell() >= epos { + self.eof = true; + return Err(ByteIOError::EOF); + } + } let len = self.max_read_len(buf.len()); let ret = self.file.read(&mut buf[..len]); if ret.is_err() { return Err(ByteIOError::ReadError); } @@ -743,6 +755,9 @@ impl ByteIO for BoundedFileReader { if sz < len { if let Err(_err) = self.file.read(&mut buf[sz..][..1]) { self.eof = true; + if sz == 0 { + return Err(ByteIOError::EOF); + } } else { return Ok(sz + 1); } -- 2.39.5