Skip to content

Commit d449d41

Browse files
committed
Don't crash when writing invalid variants via QgsXmlUtils
1 parent 2041cad commit d449d41

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/core/qgsxmlutils.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
105105
QDomElement element = doc.createElement( QStringLiteral( "Option" ) );
106106
switch ( value.type() )
107107
{
108+
case QVariant::Invalid:
109+
{
110+
element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "invalid" ) );
111+
break;
112+
}
113+
108114
case QVariant::Map:
109115
{
110116
QVariantMap map = value.toMap();
@@ -166,7 +172,7 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
166172
}
167173

168174
default:
169-
Q_ASSERT_X( false, "QgsXmlUtils::writeVariant", "unsupported variant type" );
175+
Q_ASSERT_X( false, "QgsXmlUtils::writeVariant", QStringLiteral( "unsupported variant type %1" ).arg( QVariant::typeToName( value.type() ) ).toLocal8Bit() );
170176
break;
171177
}
172178

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

180-
if ( type == QLatin1String( "int" ) )
186+
if ( type == QLatin1String( "invalid" ) )
187+
{
188+
return QVariant();
189+
}
190+
else if ( type == QLatin1String( "int" ) )
181191
{
182192
return element.attribute( QStringLiteral( "value" ) ).toInt();
183193
}

tests/src/python/test_qgsxmlutils.py

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727

2828
class TestQgsXmlUtils(unittest.TestCase):
2929

30+
def test_invalid(self):
31+
"""
32+
Test that invalid attributes are correctly loaded and written
33+
"""
34+
doc = QDomDocument("properties")
35+
36+
elem = QgsXmlUtils.writeVariant(None, doc)
37+
38+
prop2 = QgsXmlUtils.readVariant(elem)
39+
self.assertIsNone(prop2)
40+
3041
def test_integer(self):
3142
"""
3243
Test that maps are correctly loaded and written

0 commit comments

Comments
 (0)