lib/file: Fix (potential) memory leaks
parent
79650e390e
commit
341b4c187e
|
@ -58,35 +58,41 @@ void File::close(){
|
|||
}
|
||||
|
||||
ErrorOr<uint8_t> File::readByte(){
|
||||
uint8_t* nextPointer = new uint8_t;
|
||||
uint8_t nextByte;
|
||||
bool failure = false;
|
||||
try {
|
||||
this->fileStream.seekg(this->cursorPosition);
|
||||
|
||||
char* byte = new char;
|
||||
try{
|
||||
this->fileStream.read(byte, 1);
|
||||
}catch(std::exception& e){
|
||||
return ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED);
|
||||
}
|
||||
//FIXME: check that a byte is available for read
|
||||
this->fileStream.read(reinterpret_cast<char*>(nextPointer), 1);
|
||||
nextByte = *nextPointer;
|
||||
|
||||
this->cursorPosition += 1;
|
||||
return ErrorOr<uint8_t>((uint8_t) *byte);
|
||||
this->cursorPosition++;
|
||||
|
||||
} catch (std::exception& e) {
|
||||
failure = true;
|
||||
}
|
||||
delete nextPointer;
|
||||
return failure? ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED) : ErrorOr<uint8_t>(nextByte);
|
||||
}
|
||||
|
||||
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;
|
||||
try {
|
||||
this->fileStream.seekg(this->cursorPosition);
|
||||
|
||||
char* buffer = new char[bytes];
|
||||
std::vector<uint8_t> data;
|
||||
try{
|
||||
this->fileStream.read(buffer, bytes);
|
||||
for(uint64_t i=0; i<bytes; i++){
|
||||
data.push_back((uint8_t) buffer[i]);
|
||||
//FIXME: check that enough data is available to read
|
||||
this->fileStream.read(reinterpret_cast<char*>(buffer), bytes);
|
||||
data = std::vector<uint8_t>(buffer, buffer+bytes);
|
||||
this->cursorPosition += bytes;
|
||||
} catch (std::exception& e) {
|
||||
failure = true;
|
||||
}
|
||||
delete[] buffer;
|
||||
}catch(std::exception& e){
|
||||
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED);
|
||||
}
|
||||
this->cursorPosition += bytes;
|
||||
return ErrorOr<std::vector<uint8_t>>(data);
|
||||
return failure? ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED) : ErrorOr<std::vector<uint8_t>>(data);
|
||||
}
|
||||
|
||||
ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){
|
||||
|
|
Loading…
Reference in New Issue