Skip to content

Commit 49c150c

Browse files
committedAug 17, 2021
[expressions] Don't cache aggregate results if there's an evaluation error.
Otherwise subsequent evaluations of the expression will happily grab the cached null variant, unaware that it's actually a result of a broken expression and not a valid null aggregate calculation. (cherry picked from commit 97f0242)
1 parent 1cb8df8 commit 49c150c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed
 

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,13 @@ static QVariant fcnAggregate( const QVariantList &values, const QgsExpressionCon
681681
subContext.appendScope( subScope );
682682
result = vl->aggregate( aggregate, subExpression, parameters, &subContext, &ok );
683683

684-
context->setCachedValue( cacheKey, result );
684+
if ( ok )
685+
{
686+
// important -- we should only store cached values when the expression is successfully calculated. Otherwise subsequent
687+
// use of the expression context will happily grab the invalid QVariant cached value without realising that there was actually an error
688+
// associated with it's calculation!
689+
context->setCachedValue( cacheKey, result );
690+
}
685691
}
686692
else
687693
{

‎tests/src/core/testqgsexpression.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,8 @@ class TestQgsExpression: public QObject
21542154

21552155
// check again - make sure value was correctly cached
21562156
res = exp.evaluate( &context );
2157+
// if first evaluation has an eval error, so should any subsequent evaluations!
2158+
QCOMPARE( exp.hasEvalError(), evalError );
21572159
QCOMPARE( res, result );
21582160
}
21592161

0 commit comments

Comments
 (0)
Please sign in to comment.