Skip to content

Commit

Permalink
[processing] Fix crash in extract by attribute when field name does n…
Browse files Browse the repository at this point in the history
…ot exist

Fixes #19531
  • Loading branch information
nyalldawson committed Aug 3, 2018
1 parent 9e08606 commit 4acef1e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions python/plugins/processing/algs/qgis/SelectByAttribute.py
Expand Up @@ -130,6 +130,9 @@ def processAlgorithm(self, parameters, context, feedback):
fields = layer.fields()

idx = layer.fields().lookupField(fieldName)
if idx < 0:
raise QgsProcessingException(self.tr("Field '{}' was not found in layer").format(fieldName))

fieldType = fields[idx].type()

if fieldType != QVariant.String and operator in self.STRING_OPERATORS:
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/processing/tests/AlgorithmsTestBase.py
Expand Up @@ -119,6 +119,9 @@ def check_algorithm(self, name, defs):
exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
expectFailure = eval(defs['expectedFailure'][-1])

if 'expectedException' in defs:
expectFailure = True

# ignore user setting for invalid geometry handling
context = QgsProcessingContext()
context.setProject(QgsProject.instance())
Expand Down
Binary file not shown.
14 changes: 14 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -2086,6 +2086,20 @@ tests:
name: expected/extract_by_attribute_not_null.gml
type: vector

- algorithm: native:extractbyattribute
name: Extract by attribute (bad field)
params:
FIELD: notafield
INPUT:
name: polys.gml
type: vector
OPERATOR: '9'
results:
OUTPUT:
name: expected/failure.gml
type: vector
expectedException: true

- algorithm: native:extractbyattribute
name: Extract by attribute (starts with)
params:
Expand Down
4 changes: 3 additions & 1 deletion src/analysis/processing/qgsalgorithmextractbyattribute.cpp
Expand Up @@ -101,8 +101,10 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap
std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral( "FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(),
source->wkbType(), source->sourceCrs() ) );


int idx = source->fields().lookupField( fieldName );
if ( idx < 0 )
throw QgsProcessingException( QObject::tr( "Field '%1' was not found in INPUT source" ).arg( fieldName ) );

QVariant::Type fieldType = source->fields().at( idx ).type();

if ( fieldType != QVariant::String && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
Expand Down

0 comments on commit 4acef1e

Please sign in to comment.