Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix crash in OGR provider when requesting invalid field indexes
in feature request
  • Loading branch information
nyalldawson authored and github-actions[bot] committed Jan 17, 2023
1 parent e935f9f commit 9e17701
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -609,6 +609,9 @@ bool QgsOgrFeatureIterator::readFeature( const gdal::ogr_feature_unique_ptr &fet
for ( int i = 0; i < requestedAttributeTotal; ++i )
{
const int idx = requestAttribute[i];
if ( idx >= fieldCount )
continue;

attributeData[idx] = getFeatureAttribute( fet.get(), idx );
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -282,6 +282,59 @@ def testGeometryCollection(self):
os.unlink(datasource)
self.assertFalse(os.path.exists(datasource))

def test_request_invalid_attributes(self):
"""
Test asking for invalid attributes in feature request
"""
points_layer = QgsVectorLayer(os.path.join(unitTestDataPath(), 'points.shp'), 'points')
self.assertTrue(points_layer.isValid())

req = QgsFeatureRequest()
req.setSubsetOfAttributes([8, 0, 3, 7, 9, 10, 11, 13, 14])

features = list(points_layer.dataProvider().getFeatures(req))
self.assertCountEqual([f.attributes() for f in features],
[['Jet', None, None, 2, None, None],
['Biplane', None, None, 3, None, None],
['Jet', None, None, 1, None, None],
['Jet', None, None, 1, None, None],
['Jet', None, None, 1, None, None],
['Biplane', None, None, 3, None, None],
['Biplane', None, None, 3, None, None],
['Biplane', None, None, 3, None, None],
['Biplane', None, None, 3, None, None],
['B52', None, None, 2, None, None],
['B52', None, None, 1, None, None],
['B52', None, None, 2, None, None],
['B52', None, None, 2, None, None],
['Jet', None, None, 2, None, None],
['Jet', None, None, 1, None, None],
['Jet', None, None, 1, None, None],
['Jet', None, None, 3, None, None]])

req = QgsFeatureRequest()
req.setSubsetOfAttributes(['nope', 'Class', 'Pilots', 'nope2'], points_layer.dataProvider().fields())

features = list(points_layer.dataProvider().getFeatures(req))
self.assertCountEqual([f.attributes() for f in features],
[['Jet', None, None, 2, None, None],
['Biplane', None, None, 3, None, None],
['Jet', None, None, 1, None, None],
['Jet', None, None, 1, None, None],
['Jet', None, None, 1, None, None],
['Biplane', None, None, 3, None, None],
['Biplane', None, None, 3, None, None],
['Biplane', None, None, 3, None, None],
['Biplane', None, None, 3, None, None],
['B52', None, None, 2, None, None],
['B52', None, None, 1, None, None],
['B52', None, None, 2, None, None],
['B52', None, None, 2, None, None],
['Jet', None, None, 2, None, None],
['Jet', None, None, 1, None, None],
['Jet', None, None, 1, None, None],
['Jet', None, None, 3, None, None]])

def testGdb(self):
""" Test opening a GDB database layer"""
gdb_path = os.path.join(unitTestDataPath(), 'test_gdb.gdb')
Expand Down

0 comments on commit 9e17701

Please sign in to comment.