Skip to content

Commit

Permalink
Don't crash when iterating empty QgsFields
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 9, 2016
1 parent db69451 commit 0e5214c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,32 +441,50 @@ bool QgsFields::operator==( const QgsFields &other ) const

QgsFields::const_iterator QgsFields::constBegin() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();

return const_iterator( &d->fields.first() );
}

QgsFields::const_iterator QgsFields::constEnd() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();

return const_iterator( &d->fields.last() + 1 );
}

QgsFields::const_iterator QgsFields::begin() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();

return const_iterator( &d->fields.first() );
}

QgsFields::const_iterator QgsFields::end() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();

return const_iterator( &d->fields.last() + 1 );
}

QgsFields::iterator QgsFields::begin()
{
if ( d->fields.isEmpty() )
return iterator();

d.detach();
return iterator( &d->fields.first() );
}

QgsFields::iterator QgsFields::end()
{
if ( d->fields.isEmpty() )
return iterator();

d.detach();
return iterator( &d->fields.last() + 1 );
}
Expand Down
15 changes: 15 additions & 0 deletions tests/src/core/testqgsfields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ void TestQgsFields::qforeach()
void TestQgsFields::iterator()
{
QgsFields fields;

//test with empty fields
QCOMPARE( fields.begin(), fields.end() );

QgsField field( QString( "1" ) );
fields.append( field );
QgsField field2( QString( "2" ) );
Expand Down Expand Up @@ -531,6 +535,17 @@ void TestQgsFields::iterator()
void TestQgsFields::constIterator()
{
QgsFields fields;

//test with empty fields
QCOMPARE( fields.constBegin(), fields.constEnd() );
QCOMPARE( const_cast< const QgsFields* >( &fields )->begin(), const_cast< const QgsFields* >( &fields )->end() );
Q_FOREACH ( const QgsField& f, fields )
{
Q_UNUSED( f );
//should not be called!
QVERIFY( false );
}

QgsField field( QString( QString( "1" ) ) );
fields.append( field );
QgsField field2( QString( QString( "2" ) ) );
Expand Down

0 comments on commit 0e5214c

Please sign in to comment.