lib/file: Fix a potential memory leak created by yours truly

Soda
BodgeMaster 2022-10-05 04:50:41 +02:00
parent f3e03710f6
commit 39c5940200
1 changed files with 8 additions and 8 deletions

View File

@ -61,10 +61,6 @@ bool File::eof() {
} }
ErrorOr<uint8_t> File::readByte(){ ErrorOr<uint8_t> File::readByte(){
uint8_t* nextPointer = new uint8_t;
uint8_t nextByte;
bool failure = false;
if (!this->isOpen) { if (!this->isOpen) {
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_NOT_OPEN); return ErrorOr<uint8_t>(true, ErrorCodes::FILE_NOT_OPEN);
} }
@ -72,6 +68,10 @@ ErrorOr<uint8_t> File::readByte(){
return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN); return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN);
} }
uint8_t* nextPointer = new uint8_t;
uint8_t nextByte;
bool failure = false;
try { try {
this->fileStream.seekg(this->cursorPosition); this->fileStream.seekg(this->cursorPosition);
this->fileStream.read(reinterpret_cast<char*>(nextPointer), 1); this->fileStream.read(reinterpret_cast<char*>(nextPointer), 1);
@ -87,10 +87,6 @@ ErrorOr<uint8_t> File::readByte(){
} }
ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){ ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
uint8_t* buffer = new uint8_t[bytes];
std::vector<uint8_t> data;
bool failure = false;
if (!this->isOpen) { if (!this->isOpen) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_NOT_OPEN); return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_NOT_OPEN);
} }
@ -98,6 +94,10 @@ ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::OVERRUN); return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::OVERRUN);
} }
uint8_t* buffer = new uint8_t[bytes];
std::vector<uint8_t> data;
bool failure = false;
try { try {
this->fileStream.seekg(this->cursorPosition); this->fileStream.seekg(this->cursorPosition);
this->fileStream.read(reinterpret_cast<char*>(buffer), bytes); this->fileStream.read(reinterpret_cast<char*>(buffer), bytes);