Skip to content

Commit

Permalink
Don't crash when writing invalid variants via QgsXmlUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 4, 2018
1 parent 2041cad commit d449d41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/core/qgsxmlutils.cpp
Expand Up @@ -105,6 +105,12 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
QDomElement element = doc.createElement( QStringLiteral( "Option" ) );
switch ( value.type() )
{
case QVariant::Invalid:
{
element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "invalid" ) );
break;
}

case QVariant::Map:
{
QVariantMap map = value.toMap();
Expand Down Expand Up @@ -166,7 +172,7 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
}

default:
Q_ASSERT_X( false, "QgsXmlUtils::writeVariant", "unsupported variant type" );
Q_ASSERT_X( false, "QgsXmlUtils::writeVariant", QStringLiteral( "unsupported variant type %1" ).arg( QVariant::typeToName( value.type() ) ).toLocal8Bit() );
break;
}

Expand All @@ -177,7 +183,11 @@ QVariant QgsXmlUtils::readVariant( const QDomElement &element )
{
QString type = element.attribute( QStringLiteral( "type" ) );

if ( type == QLatin1String( "int" ) )
if ( type == QLatin1String( "invalid" ) )
{
return QVariant();
}
else if ( type == QLatin1String( "int" ) )
{
return element.attribute( QStringLiteral( "value" ) ).toInt();
}
Expand Down
11 changes: 11 additions & 0 deletions tests/src/python/test_qgsxmlutils.py
Expand Up @@ -27,6 +27,17 @@

class TestQgsXmlUtils(unittest.TestCase):

def test_invalid(self):
"""
Test that invalid attributes are correctly loaded and written
"""
doc = QDomDocument("properties")

elem = QgsXmlUtils.writeVariant(None, doc)

prop2 = QgsXmlUtils.readVariant(elem)
self.assertIsNone(prop2)

def test_integer(self):
"""
Test that maps are correctly loaded and written
Expand Down

1 comment on commit d449d41

@nirvn
Copy link
Contributor

@nirvn nirvn commented on d449d41 Sep 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nyalldawson , for the record, this commit does fix the crash while saving regression. Thanks.

Please sign in to comment.