From 736e92ebd5547540e3ae530d352b15c177ff4e4f Mon Sep 17 00:00:00 2001 From: C-3PO Date: Fri, 14 Sep 2018 04:52:18 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=9A=20Fix=20crash=20when=20trying=20to?= =?UTF-8?q?=20read=20beyond=20last=20disk=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fileReader.c | 5 +++-- src/fileReader.h | 2 +- src/main.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/fileReader.c b/src/fileReader.c index 9906853..582d998 100644 --- a/src/fileReader.c +++ b/src/fileReader.c @@ -33,7 +33,7 @@ void initFileReader(char path[], unsigned long offset) { //Reads the given amount of bytes from the file and writes them into the given buffer. Automatically opens next file if EOF is reached. -void getBytes(uint8_t* buffer, unsigned long numBytes) { +void getBytes(uint8_t* buffer, unsigned long numBytes, bool isLast) { uint8_t* bufferPosition = buffer; unsigned long remainingBytes = numBytes; @@ -52,7 +52,8 @@ void getBytes(uint8_t* buffer, unsigned long numBytes) { remainingBytes -= availableBytes; //If we've reached end of file, close file and open next file - if (file.offset == file.size) { + //Unless we've reached the end, in that case don't open next file in case we've reached the end of the last disk + if (!isLast && file.offset == file.size) { openNextFile(); } } diff --git a/src/fileReader.h b/src/fileReader.h index fdf15bb..d8300fa 100644 --- a/src/fileReader.h +++ b/src/fileReader.h @@ -4,4 +4,4 @@ void initFileReader(char path[], unsigned long offset); -void getBytes(uint8_t* buffer, unsigned long numBytes); +void getBytes(uint8_t* buffer, unsigned long numBytes, bool isLast); diff --git a/src/main.c b/src/main.c index eff7cf8..fcfc73c 100644 --- a/src/main.c +++ b/src/main.c @@ -95,7 +95,7 @@ int main(int argc, char *argv[]) { while (remainingBytes > 0 || !hasReachedEnd) { if (needToRead) { chunkSize = min(BUFFER_SIZE - uncompressedPosition, remainingBytes); - getBytes(compressedChunk, chunkSize); + getBytes(compressedChunk, chunkSize, chunkSize == remainingBytes); remainingBytes -= chunkSize; //Decrypt file if it is encrypted