Skip to content
Permalink
Browse files

Don't crash when writing invalid variants via QgsXmlUtils

  • Loading branch information
nyalldawson committed Sep 4, 2018
1 parent 2041cad commit d449d415033432f82e0c74a29cbfcf776dea732f
Showing with 23 additions and 2 deletions.
  1. +12 −2 src/core/qgsxmlutils.cpp
  2. +11 −0 tests/src/python/test_qgsxmlutils.py
@@ -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();
@@ -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;
}

@@ -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();
}
@@ -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

1 comment on commit d449d41

@nirvn

This comment has been minimized.

Copy link
Contributor

@nirvn nirvn commented on d449d41 Sep 5, 2018

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

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