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