Skip to content

Commit 882912f

Browse files
committedJun 9, 2014
Fix #10365 (incorrectly escaped strings from QgsExpression)
1 parent bca3553 commit 882912f

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed
 

‎src/core/qgsexpression.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,20 @@ QList<QgsExpression::Function*> QgsExpression::specialColumns()
17391739
return defs;
17401740
}
17411741

1742+
QString QgsExpression::quotedColumnRef( QString name )
1743+
{
1744+
return QString( "\"%1\"" ).arg( name.replace( "\"", "\"\"" ) );
1745+
}
1746+
1747+
QString QgsExpression::quotedString( QString text )
1748+
{
1749+
text.replace( "'", "''" );
1750+
text.replace( '\\', "\\\\" );
1751+
text.replace( '\n', "\\n" );
1752+
text.replace( '\t', "\\t" );
1753+
return QString( "'%1'" ).arg( text );
1754+
}
1755+
17421756
bool QgsExpression::isFunctionName( QString name )
17431757
{
17441758
return functionIndex( name ) != -1;
@@ -2429,7 +2443,7 @@ QString QgsExpression::NodeLiteral::dump() const
24292443
{
24302444
case QVariant::Int: return QString::number( mValue.toInt() );
24312445
case QVariant::Double: return QString::number( mValue.toDouble() );
2432-
case QVariant::String: return QString( "'%1'" ).arg( mValue.toString() );
2446+
case QVariant::String: return quotedString( mValue.toString() );
24332447
default: return QObject::tr( "[unsupported type;%1; value:%2]" ).arg( mValue.typeName() ).arg( mValue.toString() );
24342448
}
24352449
}

‎src/core/qgsexpression.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ class CORE_EXPORT QgsExpression
314314
static QList<Function*> specialColumns();
315315

316316
//! return quoted column reference (in double quotes)
317-
static QString quotedColumnRef( QString name ) { return QString( "\"%1\"" ).arg( name.replace( "\"", "\"\"" ) ); }
317+
static QString quotedColumnRef( QString name );
318318
//! return quoted string (in single quotes)
319-
static QString quotedString( QString text ) { return QString( "'%1'" ).arg( text.replace( "'", "''" ) ); }
319+
static QString quotedString( QString text );
320320

321321
//////
322322

‎tests/src/core/testqgsexpression.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -882,18 +882,31 @@ class TestQgsExpression: public QObject
882882
QgsExpression::unsetSpecialColumn( "$var1" );
883883
}
884884

885+
void expression_from_expression_data()
886+
{
887+
QTest::addColumn<QString>( "string" );
888+
QTest::newRow( "column ref" ) << "my_column";
889+
QTest::newRow( "column ref with space" ) << "\"my column\"";
890+
QTest::newRow( "string literal" ) << "'hello'";
891+
QTest::newRow( "string with quote" ) << "'hel''lo'";
892+
}
893+
885894
void expression_from_expression()
886895
{
887-
{
888-
QgsExpression e( "my_column" );
889-
QCOMPARE( e.expression() , QgsExpression( e.expression() ).expression() );
890-
}
891-
{
892-
QgsExpression e( "\"my column\"" );
893-
QCOMPARE( e.expression() , QgsExpression( e.expression() ).expression() );
894-
}
896+
QFETCH( QString, string );
897+
898+
QgsExpression e( string );
899+
QVERIFY( !e.hasParserError() );
900+
qDebug() << e.expression();
901+
QCOMPARE( e.expression() , QgsExpression( e.expression() ).expression() );
895902
}
896903

904+
void quote_string()
905+
{
906+
QCOMPARE( QgsExpression::quotedString( "hello\nworld" ), QString( "'hello\\nworld'" ) );
907+
QCOMPARE( QgsExpression::quotedString( "hello\tworld" ), QString( "'hello\\tworld'" ) );
908+
QCOMPARE( QgsExpression::quotedString( "hello\\world" ), QString( "'hello\\\\world'" ) );
909+
}
897910

898911
};
899912

1 commit comments

Comments
 (1)

nyalldawson commented on Jun 9, 2014

@nyalldawson
Collaborator

@wonder-sk thanks heaps for this fix!

Please sign in to comment.