Skip to content
Permalink
Browse files

Don't crash when iterating empty QgsFields

  • Loading branch information
nyalldawson committed Mar 9, 2016
1 parent db69451 commit 0e5214cdece0758a5c1130b74ff839cc0b7e3494
Showing with 33 additions and 0 deletions.
  1. +18 −0 src/core/qgsfield.cpp
  2. +15 −0 tests/src/core/testqgsfields.cpp
@@ -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 );
}
@@ -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" ) );
@@ -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" ) ) );

0 comments on commit 0e5214c

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