lib/file: Fix (potential) memory leaks
parent
79650e390e
commit
341b4c187e
|
@ -58,35 +58,41 @@ void File::close(){
|
|||
}
|
||||
|
||||
ErrorOr<uint8_t> File::readByte(){
|
||||
this->fileStream.seekg(this->cursorPosition);
|
||||
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++;
|
||||
|
||||
} catch (std::exception& e) {
|
||||
failure = true;
|
||||
}
|
||||
|
||||
this->cursorPosition += 1;
|
||||
return ErrorOr<uint8_t>((uint8_t) *byte);
|
||||
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){
|
||||
this->fileStream.seekg(this->cursorPosition);
|
||||
|
||||
char* buffer = new char[bytes];
|
||||
uint8_t* buffer = new uint8_t[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]);
|
||||
}
|
||||
delete[] buffer;
|
||||
}catch(std::exception& e){
|
||||
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED);
|
||||
bool failure = false;
|
||||
try {
|
||||
this->fileStream.seekg(this->cursorPosition);
|
||||
|
||||
//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;
|
||||
}
|
||||
this->cursorPosition += bytes;
|
||||
return ErrorOr<std::vector<uint8_t>>(data);
|
||||
delete[] buffer;
|
||||
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