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(){ | ||||||
|     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; |         //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++; | ||||||
|  | 
 | ||||||
|  |     } catch (std::exception& e) { | ||||||
|  |         failure = true; | ||||||
|     } |     } | ||||||
| 
 |     delete nextPointer; | ||||||
|     this->cursorPosition += 1; |     return failure? ErrorOr<uint8_t>(true, ErrorCodes::FILE_READ_FAILED) : ErrorOr<uint8_t>(nextByte); | ||||||
|     return ErrorOr<uint8_t>((uint8_t) *byte); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){ | ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){ | ||||||
|     this->fileStream.seekg(this->cursorPosition); |     uint8_t* buffer = new uint8_t[bytes]; | ||||||
| 
 |  | ||||||
|     char* buffer = new char[bytes]; |  | ||||||
|     std::vector<uint8_t> data; |     std::vector<uint8_t> data; | ||||||
|     try{ |     bool failure = false; | ||||||
|         this->fileStream.read(buffer, bytes); |     try { | ||||||
|         for(uint64_t i=0; i<bytes; i++){ |         this->fileStream.seekg(this->cursorPosition); | ||||||
|             data.push_back((uint8_t) buffer[i]); | 
 | ||||||
|         } |         //FIXME: check that enough data is available to read
 | ||||||
|         delete[] buffer; |         this->fileStream.read(reinterpret_cast<char*>(buffer), bytes); | ||||||
|     }catch(std::exception& e){ |         data = std::vector<uint8_t>(buffer, buffer+bytes); | ||||||
|         return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_READ_FAILED); |         this->cursorPosition += bytes; | ||||||
|  |     } catch (std::exception& e) { | ||||||
|  |         failure = true; | ||||||
|     } |     } | ||||||
|     this->cursorPosition += bytes; |     delete[] buffer; | ||||||
|     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){ | ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){ | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 BodgeMaster
						BodgeMaster