Skip to content
Permalink
Browse files
Fix crash when iterating csv with bool values
Fix #46749
  • Loading branch information
elpaso authored and nyalldawson committed Jan 7, 2022
1 parent 6bf4aeb commit 0653d981f8aad82b58a5beb29a3b85d17c808023
Showing with 25 additions and 1 deletion.
  1. +5 −1 src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
  2. +20 −0 tests/src/python/test_qgsdelimitedtextprovider.py
@@ -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 );
@@ -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" ) )
{
@@ -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.