Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't crash when findLayer is called with a nullptr
  • Loading branch information
nyalldawson committed Jun 22, 2018
1 parent 41d6b9d commit cd495fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core/layertree/qgslayertreegroup.cpp
Expand Up @@ -193,6 +193,9 @@ void QgsLayerTreeGroup::removeAllChildren()

QgsLayerTreeLayer *QgsLayerTreeGroup::findLayer( QgsMapLayer *layer ) const
{
if ( !layer )
return nullptr;

return findLayer( layer->id() );
}

Expand Down
24 changes: 24 additions & 0 deletions tests/src/core/testqgslayertree.cpp
Expand Up @@ -47,6 +47,7 @@ class TestQgsLayerTree : public QObject
void testLegendSymbolRuleBased();
void testResolveReferences();
void testEmbeddedGroup();
void testFindLayer();
void testLayerDeleted();

private:
Expand Down Expand Up @@ -589,6 +590,29 @@ void TestQgsLayerTree::testEmbeddedGroup()
}
}

void TestQgsLayerTree::testFindLayer()
{
//new memory layer
QgsVectorLayer *vl = new QgsVectorLayer( QStringLiteral( "Point?field=col1:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
QVERIFY( vl->isValid() );

QgsProject project;
project.addMapLayer( vl );

QgsLayerTree root;
QgsLayerTreeModel model( &root );

QVERIFY( !root.findLayer( vl->id() ) );
QVERIFY( !root.findLayer( nullptr ) );

root.addLayer( vl );

QCOMPARE( root.findLayer( vl->id() )->layer(), vl );
QCOMPARE( root.findLayer( vl )->layer(), vl );
QVERIFY( !root.findLayer( QStringLiteral( "xxx" ) ) );
QVERIFY( !root.findLayer( nullptr ) );
}

void TestQgsLayerTree::testLayerDeleted()
{
//new memory layer
Expand Down

0 comments on commit cd495fb

Please sign in to comment.