Skip to content

Commit

Permalink
Fix crash when iterating csv with bool values
Browse files Browse the repository at this point in the history
Fix #46749
  • Loading branch information
elpaso authored and nyalldawson committed Jan 7, 2022
1 parent 6bf4aeb commit 0653d98
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -786,12 +786,16 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes, bool forceFullScan,
QString csvtMessage;
QgsDebugMsgLevel( QStringLiteral( "Reading CSVT: %1" ).arg( mFile->fileName() ), 2 );
QStringList csvtTypes = readCsvtFieldTypes( mFile->fileName(), &csvtMessage );
int fieldIdxOffset { 0 };

for ( int fieldIdx = 0; fieldIdx < fieldNames.size(); fieldIdx++ )
{
// Skip over WKT field ... don't want to display in attribute table
if ( fieldIdx == mWktFieldIndex )
{
fieldIdxOffset++;
continue;
}

// Add the field index lookup for the column
attributeColumns.append( fieldIdx );
Expand Down Expand Up @@ -862,7 +866,7 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes, bool forceFullScan,
if ( typeName == QLatin1String( "bool" ) )
{
fieldType = QVariant::Bool;
mFieldBooleanLiterals.insert( fieldIdx, boolCandidates[fieldIdx] );
mFieldBooleanLiterals.insert( fieldIdx - fieldIdxOffset, boolCandidates[fieldIdx] );
}
else if ( typeName == QLatin1String( "integer" ) )
{
Expand Down
20 changes: 20 additions & 0 deletions tests/src/python/test_qgsdelimitedtextprovider.py
Expand Up @@ -1365,6 +1365,26 @@ def test_type_override(self):
self.assertEqual(attrs, [[1.0, 0, 9.189304972279763e+18, '1.234', 'text'],
[2.0, 1, NULL, '5.678', 'another text']])

def test_regression_gh46749(self):
"""Test regression GH #46749"""

vl = self._make_test_file('\n'.join((
"integer,wkt,bool",
"1,POINT(0 0),1",
"2,POINT(1 1),0",
"3,POINT(2 2),1",
)), uri_options='geomType=Point&crs=EPSG:4326&wktField=wkt')

self.assertTrue(vl.isValid())
fields = {f.name(): (f.type(), f.typeName()) for f in vl.fields()}
self.assertEqual(fields, {
'integer': (QVariant.Int, 'integer'),
'bool': (QVariant.Bool, 'bool'),
})

# This was crashing!
features = [f for f in vl.getFeatures()]


if __name__ == '__main__':
unittest.main()

0 comments on commit 0653d98

Please sign in to comment.