lib/file: Check if there are enough bytes left to read

Soda
BodgeMaster 2022-10-05 03:46:42 +02:00
parent 341b4c187e
commit ec44ac9531
1 changed files with 6 additions and 1 deletions

View File

@ -61,10 +61,12 @@ ErrorOr<uint8_t> File::readByte(){
uint8_t* nextPointer = new uint8_t;
uint8_t nextByte;
bool failure = false;
if (!this->size.isError && this->cursorPosition >= this->size.value) {
return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN);
}
try {
this->fileStream.seekg(this->cursorPosition);
//FIXME: check that a byte is available for read
this->fileStream.read(reinterpret_cast<char*>(nextPointer), 1);
nextByte = *nextPointer;
@ -81,6 +83,9 @@ 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->size.isError && this->cursorPosition >= this->size.value+bytes) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::OVERRUN);
}
try {
this->fileStream.seekg(this->cursorPosition);