From ec44ac95310b82425c9d51a20500ca6adff70105 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Wed, 5 Oct 2022 03:46:42 +0200 Subject: [PATCH] lib/file: Check if there are enough bytes left to read --- src/lib/file.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 5406d28..b0da8ab 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -61,10 +61,12 @@ ErrorOr File::readByte(){ uint8_t* nextPointer = new uint8_t; uint8_t nextByte; bool failure = false; + if (!this->size.isError && this->cursorPosition >= this->size.value) { + return ErrorOr(true, ErrorCodes::OVERRUN); + } try { this->fileStream.seekg(this->cursorPosition); - //FIXME: check that a byte is available for read this->fileStream.read(reinterpret_cast(nextPointer), 1); nextByte = *nextPointer; @@ -81,6 +83,9 @@ ErrorOr> File::read(uint64_t bytes){ uint8_t* buffer = new uint8_t[bytes]; std::vector data; bool failure = false; + if (!this->size.isError && this->cursorPosition >= this->size.value+bytes) { + return ErrorOr>(true, ErrorCodes::OVERRUN); + } try { this->fileStream.seekg(this->cursorPosition);