From 39c59402009d6bc6932a7af665a29f8fe51ba66b Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Wed, 5 Oct 2022 04:50:41 +0200 Subject: [PATCH] lib/file: Fix a potential memory leak created by yours truly --- src/lib/file.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 4794fd6..216077f 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -61,10 +61,6 @@ bool File::eof() { } ErrorOr File::readByte(){ - uint8_t* nextPointer = new uint8_t; - uint8_t nextByte; - bool failure = false; - if (!this->isOpen) { return ErrorOr(true, ErrorCodes::FILE_NOT_OPEN); } @@ -72,6 +68,10 @@ ErrorOr File::readByte(){ return ErrorOr(true, ErrorCodes::OVERRUN); } + uint8_t* nextPointer = new uint8_t; + uint8_t nextByte; + bool failure = false; + try { this->fileStream.seekg(this->cursorPosition); this->fileStream.read(reinterpret_cast(nextPointer), 1); @@ -87,10 +87,6 @@ ErrorOr File::readByte(){ } ErrorOr> File::read(uint64_t bytes){ - uint8_t* buffer = new uint8_t[bytes]; - std::vector data; - bool failure = false; - if (!this->isOpen) { return ErrorOr>(true, ErrorCodes::FILE_NOT_OPEN); } @@ -98,6 +94,10 @@ ErrorOr> File::read(uint64_t bytes){ return ErrorOr>(true, ErrorCodes::OVERRUN); } + uint8_t* buffer = new uint8_t[bytes]; + std::vector data; + bool failure = false; + try { this->fileStream.seekg(this->cursorPosition); this->fileStream.read(reinterpret_cast(buffer), bytes);