Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix 'nan'='nan' evaluates to false in expressions
Also fix 'nan' and 'inf' being treated as doubles rather than
strings.
  • Loading branch information
nyalldawson authored and jef-n committed Jul 24, 2015
1 parent e0b2445 commit d110452
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/core/qgsexpression.cpp
Expand Up @@ -175,7 +175,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;
}

Expand Down Expand Up @@ -238,7 +244,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;
Expand Down
7 changes: 7 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -171,7 +171,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 );
Expand Down Expand Up @@ -207,6 +210,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 );
Expand Down

0 comments on commit d110452

Please sign in to comment.