Skip to content
Permalink
Browse files

[processing] Fix crash in extract by attribute when field name does n…

…ot exist

Fixes #19531
  • Loading branch information
nyalldawson committed Aug 3, 2018
1 parent 9e08606 commit 4acef1efbf0c4bae01fc75915ad8374955af9fa7
@@ -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:
@@ -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())
Binary file not shown.
@@ -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:
@@ -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 ) )

0 comments on commit 4acef1e

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