Skip to content
Permalink
Browse files

Fix 'nan'='nan' evaluates to false in expressions

Also fix 'nan' and 'inf' being treated as doubles rather than
strings.
  • Loading branch information
nyalldawson committed Jul 22, 2015
1 parent 8c53821 commit a1a8d1b7a32bbde0e280201d67314a845ae4d6b9
Showing with 15 additions and 2 deletions.
  1. +8 −2 src/core/qgsexpression.cpp
  2. +7 −0 tests/src/core/testqgsexpression.cpp
@@ -176,7 +176,13 @@ inline bool isDoubleSafe( const QVariant& v )
if ( v.type() == QVariant::UInt ) return true;
if ( v.type() == QVariant::LongLong ) return true;
if ( v.type() == QVariant::ULongLong ) return true;
if ( v.type() == QVariant::String ) { bool ok; v.toString().toDouble( &ok ); return ok; }
if ( v.type() == QVariant::String )
{
bool ok;
double val = v.toString().toDouble( &ok );
ok = ok && qIsFinite( val ) && !qIsNaN( val );
return ok;
}
return false;
}

@@ -239,7 +245,7 @@ static double getDoubleValue( const QVariant& value, QgsExpression* parent )
{
bool ok;
double x = value.toDouble( &ok );
if ( !ok )
if ( !ok || qIsNaN( x ) || !qIsFinite( x ) )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to double" ).arg( value.toString() ) );
return 0;
@@ -202,7 +202,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "plus double" ) << "1+1.3" << false << QVariant( 2.3 );
QTest::newRow( "plus with null" ) << "null+3" << false << QVariant();
QTest::newRow( "plus invalid" ) << "1+'foo'" << true << QVariant();

QTest::newRow( "minus int" ) << "1-3" << false << QVariant( -2 );
QTest::newRow( "minus nan" ) << "1-'nan'" << true << QVariant();
QTest::newRow( "minus inf" ) << "1-'inf'" << true << QVariant();
QTest::newRow( "mul int" ) << "8*7" << false << QVariant( 56 );
QTest::newRow( "div int" ) << "5/2" << false << QVariant( 2.5 );
QTest::newRow( "mod int" ) << "20%6" << false << QVariant( 2 );
@@ -238,6 +241,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "ge int 2" ) << "3 >= 3" << false << QVariant( 1 );
QTest::newRow( "lt text 1" ) << "'bar' < 'foo'" << false << QVariant( 1 );
QTest::newRow( "lt text 2" ) << "'foo' < 'bar'" << false << QVariant( 0 );
QTest::newRow( "'nan'='nan'" ) << "'nan'='nan'" << false << QVariant( 1 );
QTest::newRow( "'nan'='x'" ) << "'nan'='x'" << false << QVariant( 0 );
QTest::newRow( "'inf'='inf'" ) << "'inf'='inf'" << false << QVariant( 1 );
QTest::newRow( "'inf'='x'" ) << "'inf'='x'" << false << QVariant( 0 );

// is, is not
QTest::newRow( "is null,null" ) << "null is null" << false << QVariant( 1 );

0 comments on commit a1a8d1b

Please sign in to comment.
You can’t perform that action at this time.