Skip to content

Commit

Permalink
check for valid field index in QgsFieldFormatter::representValue
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Sep 29, 2021
1 parent 77be95f commit 89e8d7d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core/qgsfieldformatter.cpp
Expand Up @@ -26,6 +26,9 @@ QString QgsFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex
Q_UNUSED( config )
Q_UNUSED( cache )

if ( ! layer->fields().exists( fieldIndex ) )
return QString();

QString defVal;
if ( layer->fields().fieldOrigin( fieldIndex ) == QgsFields::OriginProvider && layer->dataProvider() )
defVal = layer->dataProvider()->defaultValueClause( layer->fields().fieldOriginIndex( fieldIndex ) );
Expand Down
2 changes: 2 additions & 0 deletions tests/src/python/test_qgsfieldformatters.py
Expand Up @@ -660,6 +660,8 @@ def test_representValueWithDefault(self):
'4')
self.assertEqual(fieldFormatter.representValue(vl, 1, {}, None, "123"),
'123')
# bad field index
self.assertEqual(fieldFormatter.representValue(vl, 3, {}, None, 5), "")


class TestQgsDateTimeFieldFormatter(unittest.TestCase):
Expand Down

0 comments on commit 89e8d7d

Please sign in to comment.