From 087547c6a0dfe002f4ea76786d7227b2d5aabe73 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 3 Mar 2021 11:04:47 +0000 Subject: [PATCH] Report out-of-memory in some loading places BTG can throw bad-alloc in the wild; catch this case and report it. --- simgear/scene/model/ModelRegistry.cxx | 19 +++++++++++++++---- simgear/scene/tgdb/SGReaderWriterBTG.cxx | 6 +++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/simgear/scene/model/ModelRegistry.cxx b/simgear/scene/model/ModelRegistry.cxx index 42db5dfd..725d0bc4 100644 --- a/simgear/scene/model/ModelRegistry.cxx +++ b/simgear/scene/model/ModelRegistry.cxx @@ -559,9 +559,16 @@ ModelRegistry::readImage(const string& fileName, } } - // REVIEW: Memory Leak - 262,144 bytes in 1 blocks are indirectly lost - // The leak occurs with OSG, but may be related to opt being a raw pointer - res = registry->readImageImplementation(absFileName, opt); + try { + // REVIEW: Memory Leak - 262,144 bytes in 1 blocks are indirectly lost + // The leak occurs with OSG, but may be related to opt being a raw pointer + 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 +740,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 +748,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; } diff --git a/simgear/scene/tgdb/SGReaderWriterBTG.cxx b/simgear/scene/tgdb/SGReaderWriterBTG.cxx index 11734805..0ede8db7 100644 --- a/simgear/scene/tgdb/SGReaderWriterBTG.cxx +++ b/simgear/scene/tgdb/SGReaderWriterBTG.cxx @@ -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; }