Skip to content

Commit 2e0cba5

Browse files
committed
expressions: fix modulo 0 crashes (fixes #12431)
(cherry picked from commit 9596f97)
1 parent 3d3ccd0 commit 2e0cba5

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/core/qgsexpression.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,10 @@ QVariant QgsExpression::NodeBinaryOperator::eval( QgsExpression* parent, const Q
22522252
// both are integers - let's use integer arithmetics
22532253
int iL = getIntValue( vL, parent ); ENSURE_NO_EVAL_ERROR;
22542254
int iR = getIntValue( vR, parent ); ENSURE_NO_EVAL_ERROR;
2255+
2256+
if ( mOp == boMod && iR == 0 )
2257+
return QVariant();
2258+
22552259
return QVariant( computeInt( iL, iR ) );
22562260
}
22572261
else if ( isDateTimeSafe( vL ) && isIntervalSafe( vR ) )
@@ -2270,7 +2274,7 @@ QVariant QgsExpression::NodeBinaryOperator::eval( QgsExpression* parent, const Q
22702274
// general floating point arithmetic
22712275
double fL = getDoubleValue( vL, parent ); ENSURE_NO_EVAL_ERROR;
22722276
double fR = getDoubleValue( vR, parent ); ENSURE_NO_EVAL_ERROR;
2273-
if ( mOp == boDiv && fR == 0 )
2277+
if (( mOp == boDiv || mOp == boMod ) && fR == 0. )
22742278
return QVariant(); // silently handle division by zero and return NULL
22752279
return QVariant( computeDouble( fL, fR ) );
22762280
}
@@ -2280,7 +2284,7 @@ QVariant QgsExpression::NodeBinaryOperator::eval( QgsExpression* parent, const Q
22802284
//integer division
22812285
double fL = getDoubleValue( vL, parent ); ENSURE_NO_EVAL_ERROR;
22822286
double fR = getDoubleValue( vR, parent ); ENSURE_NO_EVAL_ERROR;
2283-
if ( fR == 0 )
2287+
if ( fR == 0. )
22842288
return QVariant(); // silently handle division by zero and return NULL
22852289
return QVariant( qFloor( fL / fR ) );
22862290
}

0 commit comments

Comments
 (0)