Skip to content

Commit 5f0c3f8

Browse files
committed
#14632 Guard against out-of-range time step index when loading results
A case in an ensemble can have fewer time steps than the case defining the time step axis of a statistics case. The time step index was used to index m_cellScalarResults directly, reading past the end of the vector in Release where CAF_ASSERT is a no-op.
1 parent 6d7959d commit 5f0c3f8

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

ApplicationLibCode/ReservoirDataModel/ResultCalculators/RigSoilResultCalculator.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ void RigSoilResultCalculator::calculate( const RigEclipseResultAddress& resVarAd
9494
timeStepIndex );
9595

9696
// Early exit if none of SWAT or SGAS is present
97-
if ( scalarIndexSWAT == cvf::UNDEFINED_SIZE_T && scalarIndexSGAS == cvf::UNDEFINED_SIZE_T )
98-
{
99-
return;
100-
}
97+
if ( scalarIndexSWAT == cvf::UNDEFINED_SIZE_T && scalarIndexSGAS == cvf::UNDEFINED_SIZE_T ) return;
10198

10299
size_t soilResultValueCount = 0;
103100
size_t soilTimeStepCount = 0;
@@ -124,8 +121,13 @@ void RigSoilResultCalculator::calculate( const RigEclipseResultAddress& resVarAd
124121
}
125122
}
126123

124+
// The result may be present in metadata but unavailable for this time step.
125+
if ( soilResultValueCount == 0 ) return;
126+
127127
// Make sure memory is allocated for the new SOIL results
128128
size_t soilResultScalarIndex = m_resultsData->findScalarResultIndexFromAddress( resVarAddr );
129+
if ( soilResultScalarIndex == cvf::UNDEFINED_SIZE_T ) return;
130+
129131
m_resultsData->m_cellScalarResults[soilResultScalarIndex].resize( soilTimeStepCount );
130132

131133
if ( !m_resultsData->cellScalarResults( resVarAddr, timeStepIndex ).empty() )
@@ -143,28 +145,19 @@ void RigSoilResultCalculator::calculate( const RigEclipseResultAddress& resVarAd
143145
if ( scalarIndexSWAT != cvf::UNDEFINED_SIZE_T )
144146
{
145147
swatForTimeStep = &( m_resultsData->cellScalarResults( SWATAddr, timeStepIndex ) );
146-
if ( swatForTimeStep->empty() )
147-
{
148-
swatForTimeStep = nullptr;
149-
}
148+
if ( swatForTimeStep->empty() ) swatForTimeStep = nullptr;
150149
}
151150

152151
if ( scalarIndexSGAS != cvf::UNDEFINED_SIZE_T )
153152
{
154153
sgasForTimeStep = &( m_resultsData->cellScalarResults( SGASAddr, timeStepIndex ) );
155-
if ( sgasForTimeStep->empty() )
156-
{
157-
sgasForTimeStep = nullptr;
158-
}
154+
if ( sgasForTimeStep->empty() ) sgasForTimeStep = nullptr;
159155
}
160156

161157
if ( scalarIndexSSOL != cvf::UNDEFINED_SIZE_T )
162158
{
163159
ssolForTimeStep = &( m_resultsData->cellScalarResults( SSOLAddr, timeStepIndex ) );
164-
if ( ssolForTimeStep->empty() )
165-
{
166-
ssolForTimeStep = nullptr;
167-
}
160+
if ( ssolForTimeStep->empty() ) ssolForTimeStep = nullptr;
168161
}
169162

170163
std::vector<double>* soilForTimeStep = m_resultsData->modifiableCellScalarResult( resVarAddr, timeStepIndex );

ApplicationLibCode/ReservoirDataModel/RigCaseCellResultsData.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,11 @@ size_t RigCaseCellResultsData::findOrLoadKnownScalarResultForTimeStep( const Rig
17651765

17661766
if ( mustBeCalculated( soilScalarResultIndex ) )
17671767
{
1768-
m_cellScalarResults[soilScalarResultIndex].resize( maxTimeStepCount() );
1768+
// A case in an ensemble can have fewer time steps than the case defining the time step axis
1769+
const size_t timeStepCount = maxTimeStepCount();
1770+
if ( timeStepIndex >= timeStepCount ) return cvf::UNDEFINED_SIZE_T;
1771+
1772+
m_cellScalarResults[soilScalarResultIndex].resize( timeStepCount );
17691773

17701774
std::vector<double>& values = m_cellScalarResults[soilScalarResultIndex][timeStepIndex];
17711775
if ( values.empty() )
@@ -1782,7 +1786,11 @@ size_t RigCaseCellResultsData::findOrLoadKnownScalarResultForTimeStep( const Rig
17821786

17831787
if ( mustBeCalculated( sgasScalarResultIndex ) )
17841788
{
1785-
m_cellScalarResults[sgasScalarResultIndex].resize( maxTimeStepCount() );
1789+
// A case in an ensemble can have fewer time steps than the case defining the time step axis
1790+
const size_t timeStepCount = maxTimeStepCount();
1791+
if ( timeStepIndex >= timeStepCount ) return cvf::UNDEFINED_SIZE_T;
1792+
1793+
m_cellScalarResults[sgasScalarResultIndex].resize( timeStepCount );
17861794

17871795
if ( m_cellScalarResults[sgasScalarResultIndex][timeStepIndex].empty() )
17881796
{
@@ -1817,6 +1825,9 @@ size_t RigCaseCellResultsData::findOrLoadKnownScalarResultForTimeStep( const Rig
18171825

18181826
if ( type == RiaDefines::ResultCatType::DYNAMIC_NATIVE && timeStepCount > 0 )
18191827
{
1828+
// A case in an ensemble can have fewer time steps than the case defining the time step axis
1829+
if ( timeStepIndex >= timeStepCount ) return cvf::UNDEFINED_SIZE_T;
1830+
18201831
m_cellScalarResults[scalarResultIndex].resize( timeStepCount );
18211832

18221833
std::vector<double>& values = m_cellScalarResults[scalarResultIndex][timeStepIndex];

0 commit comments

Comments
 (0)