Skip to content

Commit

Permalink
Ignore any invalid field indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and nyalldawson committed Oct 5, 2023
1 parent d5b62e2 commit b8391d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/providers/memory/qgsmemoryprovider.cpp
Expand Up @@ -674,6 +674,10 @@ bool QgsMemoryProvider::changeAttributeValues( const QgsChangedAttributesMap &at
// Break on errors
for ( QgsAttributeMap::const_iterator it2 = attrs.constBegin(); it2 != attrs.constEnd(); ++it2 )
{
const int fieldIndex = it2.key();
if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
continue;

QVariant attrValue = it2.value();
// Check attribute conversion
const bool conversionError { ! QgsVariantUtils::isNull( attrValue )
Expand Down
16 changes: 16 additions & 0 deletions tests/src/python/test_provider_memory.py
Expand Up @@ -627,6 +627,22 @@ def testCreateMemoryLayer(self):
self.assertEqual(layer.fields()[i].length(), fields[i].length())
self.assertEqual(layer.fields()[i].precision(), fields[i].precision())

def testChangeAttributeValuesInvalidFieldIndex(self):
"""
Test there's no crash when changeAttributeValues is called with a non existing field index (https://github.com/qgis/QGIS/issues/54817)
"""
layer = QgsVectorLayer(
'Point?crs=epsg:4326&index=yes&field=pk:integer', 'test', 'memory')
provider = layer.dataProvider()
f = QgsFeature()
f.setAttributes([0])
self.assertTrue(provider.addFeatures([f]))

saved_feature = next(provider.getFeatures())
self.assertTrue(provider.changeAttributeValues({saved_feature.id(): {-1: 42}}))
self.assertTrue(provider.changeAttributeValues({saved_feature.id(): {42: 42}}))
self.assertTrue(provider.changeAttributeValues({saved_feature.id(): {'fortytwo': 42}}))

def testAddChangeFeatureConvertAttribute(self):
"""
Test add features with attribute values which require conversion
Expand Down

0 comments on commit b8391d0

Please sign in to comment.