Report out-of-memory in some loading places

BTG can throw bad-alloc in the wild; catch this case and report it.
This commit is contained in:
James Turner 2021-03-03 11:04:47 +00:00 committed by Automatic Release Builder
parent b585df04a5
commit 84a569913d
2 changed files with 17 additions and 3 deletions

View File

@ -557,7 +557,13 @@ ModelRegistry::readImage(const string& fileName,
}
}
res = registry->readImageImplementation(absFileName, opt);
try {
res = registry->readImageImplementation(absFileName, opt);
} catch (std::bad_alloc&) {
simgear::reportFailure(simgear::LoadFailure::OutOfMemory, simgear::ErrorCode::ThreeDModelLoad,
"Out of memory loading texture", sg_location{absFileName});
return ReaderWriter::ReadResult::INSUFFICIENT_MEMORY_TO_LOAD;
}
if (!res.success()) {
SG_LOG(SG_IO, SG_DEV_WARN, "Image loading failed:" << res.message());
@ -733,7 +739,6 @@ ModelRegistry::readNode(const string& fileName,
ec.addFromMap(sgopt->getErrorContext());
}
ReaderWriter::ReadResult res;
CallbackMap::iterator iter
= nodeCallbackMap.find(getFileExtension(fileName));
ReaderWriter::ReadResult result;
@ -742,6 +747,11 @@ ModelRegistry::readNode(const string& fileName,
else
result = _defaultCallback->readNode(fileName, opt);
if (!result.validNode()) {
simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::ThreeDModelLoad,
"Failed to load 3D model:" + result.message(), sg_location{fileName});
}
return result;
}

View File

@ -73,8 +73,12 @@ SGReaderWriterBTG::readNode(const std::string& fileName,
"Failed to load BTG file:" + e.getFormattedMessage(),
e.getLocation());
return ReadResult::ERROR_IN_READING_FILE;
} catch (std::bad_alloc&) {
simgear::reportFailure(simgear::LoadFailure::OutOfMemory, simgear::ErrorCode::BTGLoad,
"Out of memory loading BTG:" + fileName, sg_location{fileName});
return ReadResult::ERROR_IN_READING_FILE;
}
return result;
}