Do not terminate when a grid calculation reads a cell index out of range

RigAllGridCellsResultAccessor bounds checked the cell index with an assert that is compiled out in release builds, and then called std::vector::at(), which throws. RimGridCalculation::getActiveCellValues() calls the accessor from an OpenMP loop, using active cell indices from the destination case to read results from the source case, so an index beyond the source result vector threw an exception that could not escape the parallel region and called std::terminate. Return undefined for an out of range index instead, and guard against a null result accessor.
This commit is contained in:
Magne Sjaastad committed 2026-08-10 10:07:32 +02:00
1 parent dd63083c13
commit ff7bf6c7c9
4 files changed
+72 -6

No files matched your search

@@ -635,6 +635,7 @@ std::vector<double> RimGridCalculation::getActiveCellValues( const QString&
size_t gridIndex = 0;
auto resultAccessor =
RigResultAccessorFactory::createFromResultAddress( eclipseCaseData, gridIndex, porosityModel, timeStepToUse, resAddr );
if ( resultAccessor.isNull() ) return {};
#pragma omp parallel for
for ( int i = 0; i < static_cast<int>( activeReservoirCells.size() ); i++ )
@@ -21,8 +21,6 @@
#include "RigGridBase.h"
#include "cafAssert.h"
#include <cmath>
//--------------------------------------------------------------------------------------------------
@@ -42,9 +40,9 @@ double RigAllGridCellsResultAccessor::cellScalar( size_t gridLocalCellIndex ) co
if ( m_reservoirResultValues->empty() ) return HUGE_VAL;
size_t reservoirCellIndex = m_grid->reservoirCellIndex( gridLocalCellIndex );
CAF_ASSERT( reservoirCellIndex < m_reservoirResultValues->size() );
if ( reservoirCellIndex >= m_reservoirResultValues->size() ) return HUGE_VAL;
return m_reservoirResultValues->at( reservoirCellIndex );
return ( *m_reservoirResultValues )[reservoirCellIndex];
}
//--------------------------------------------------------------------------------------------------
@@ -62,9 +60,11 @@ double RigAllGridCellsResultAccessor::cellScalarGlobIdx( size_t globCellIndex )
{
if ( m_reservoirResultValues->empty() ) return HUGE_VAL;
CAF_ASSERT( globCellIndex < m_reservoirResultValues->size() );
// The cell index can come from another case, see RimGridCalculation::getActiveCellValues(). Avoid at(), which throws: an
// exception escaping an OpenMP loop calls std::terminate.
if ( globCellIndex >= m_reservoirResultValues->size() ) return HUGE_VAL;
return m_reservoirResultValues->at( globCellIndex );
return ( *m_reservoirResultValues )[globCellIndex];
}
//--------------------------------------------------------------------------------------------------
@@ -160,6 +160,7 @@ set(SOURCE_UNITTEST_FILES
${CMAKE_CURRENT_LIST_DIR}/RicWellPathExportMswGeometryPath-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RicMswBranchBuilder-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RigMswTableData-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RigAllGridCellsResultAccessor-Test.cpp
)
if(RESINSIGHT_ENABLE_GRPC)
@@ -0,0 +1,64 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "gtest/gtest.h"
#include "ResultAccessors/RigAllGridCellsResultAccessor.h"
#include <cmath>
#include <vector>
//--------------------------------------------------------------------------------------------------
/// The grid is only used by cellScalar(), the global index accessors read the result vector directly.
//--------------------------------------------------------------------------------------------------
TEST( RigAllGridCellsResultAccessorTest, CellScalarGlobIdxReturnsValueForValidIndex )
{
std::vector<double> values = { 1.0, 2.0, 3.0 };
RigAllGridCellsResultAccessor accessor( nullptr, &values );
EXPECT_DOUBLE_EQ( 1.0, accessor.cellScalarGlobIdx( 0 ) );
EXPECT_DOUBLE_EQ( 3.0, accessor.cellScalarGlobIdx( 2 ) );
}
//--------------------------------------------------------------------------------------------------
/// A cell index beyond the result vector must not throw. The accessor is called from OpenMP loops,
/// where an escaping exception terminates the application.
//--------------------------------------------------------------------------------------------------
TEST( RigAllGridCellsResultAccessorTest, CellScalarGlobIdxIsUndefinedForIndexBeyondResultVector )
{
std::vector<double> values = { 1.0, 2.0, 3.0 };
RigAllGridCellsResultAccessor accessor( nullptr, &values );
EXPECT_NO_THROW( accessor.cellScalarGlobIdx( 3 ) );
EXPECT_DOUBLE_EQ( HUGE_VAL, accessor.cellScalarGlobIdx( 3 ) );
EXPECT_DOUBLE_EQ( HUGE_VAL, accessor.cellScalarGlobIdx( 1000 ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RigAllGridCellsResultAccessorTest, CellScalarGlobIdxIsUndefinedForEmptyResultVector )
{
std::vector<double> values;
RigAllGridCellsResultAccessor accessor( nullptr, &values );
EXPECT_DOUBLE_EQ( HUGE_VAL, accessor.cellScalarGlobIdx( 0 ) );
}