From 3371141a60e5d9c1e7480295cc401499f4c01512 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Thu, 30 Jul 2026 14:04:08 +0200 Subject: [PATCH] #14319 Guard against malformed statistics cache files when restoring contour map results --- .../ContourMap/RimStatisticsContourMap.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ApplicationLibCode/ProjectDataModel/ContourMap/RimStatisticsContourMap.cpp b/ApplicationLibCode/ProjectDataModel/ContourMap/RimStatisticsContourMap.cpp index 9ebc7c9bbf..1fb67d27f4 100644 --- a/ApplicationLibCode/ProjectDataModel/ContourMap/RimStatisticsContourMap.cpp +++ b/ApplicationLibCode/ProjectDataModel/ContourMap/RimStatisticsContourMap.cpp @@ -1052,8 +1052,19 @@ bool RimStatisticsContourMap::loadCachedResults() { const StatisticsType statisticsType = caf::AppEnum::fromIndex( statisticsTypeIndex ); - const QString fileName = cacheFileName( m_cacheFileBaseName(), statisticsType, timeStep ); - auto surfaceData = RifSurfio::importSurfaceData( fileName.toStdString() ); + const QString fileName = cacheFileName( m_cacheFileBaseName(), statisticsType, timeStep ); + + // The surface reader throws on malformed files, fall back to recomputation + std::expected>, std::string> surfaceData; + try + { + surfaceData = RifSurfio::importSurfaceData( fileName.toStdString() ); + } + catch ( ... ) + { + surfaceData = std::unexpected( "Unexpected file content" ); + } + if ( !surfaceData.has_value() ) { RiaLogging::warning( std::format( "Failed to read ensemble contour map statistics cache, recomputing: {}", fileName ) ); @@ -1061,7 +1072,8 @@ bool RimStatisticsContourMap::loadCachedResults() } const auto& [regularSurface, values] = surfaceData.value(); - if ( regularSurface.nx != static_cast( mapSize.x() ) || regularSurface.ny != static_cast( mapSize.y() ) ) + if ( regularSurface.nx != static_cast( mapSize.x() ) || regularSurface.ny != static_cast( mapSize.y() ) || + values.size() != static_cast( mapSize.x() ) * static_cast( mapSize.y() ) ) { RiaLogging::warning( std::format( "Ensemble contour map statistics cache does not match the sample grid, recomputing: {}", fileName ) );