Skip to content

Commit

Permalink
Check return from QgsPalLabeling::prepareGeometry before dereference
Browse files Browse the repository at this point in the history
Fixes #15507

Includes automated test.
  • Loading branch information
strk committed May 16, 2017
1 parent 4dab1f8 commit 126cf5f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/qgspallabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2529,8 +2529,13 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, QgsRenderContext &cont
if ( QgsPalLabeling::geometryRequiresPreparation( &permissibleZone, context, ct, doClip ? extentGeom : nullptr ) )
{
QgsGeometry* preparedZone = QgsPalLabeling::prepareGeometry( &permissibleZone, context, ct, doClip ? extentGeom : nullptr );
permissibleZone = *preparedZone;
delete preparedZone;
if ( preparedZone )
{
// QgsPalLabeling::prepareGeometry may return NULL on
// QgsCsException exception
permissibleZone = *preparedZone;
delete preparedZone;
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/src/core/testqgslabelingenginev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* *
***************************************************************************/

#include <memory>

#include <QtTest/QtTest>

#include <qgsapplication.h>
Expand Down Expand Up @@ -45,6 +47,7 @@ class TestQgsLabelingEngineV2 : public QObject
void testEncodeDecodePositionOrder();
void testSubstitutions();
void testCapitalization();
void testRegisterFeatureUnprojectible();

private:
QgsVectorLayer* vl;
Expand Down Expand Up @@ -538,5 +541,43 @@ bool TestQgsLabelingEngineV2::imageCheck( const QString& testName, QImage &image
return resultFlag;
}

// See https://issues.qgis.org/issues/15507
void TestQgsLabelingEngineV2::testRegisterFeatureUnprojectible()
{
QgsPalLayerSettings settings;
settings.fieldName = QString( "'aa label'" );
settings.isExpression = true;
settings.fitInPolygonOnly = true;

std::unique_ptr< QgsVectorLayer> vl2( new QgsVectorLayer( "polygon?crs=epsg:4326&field=id:integer", "vl", "memory" ) );
QgsVectorLayerLabelProvider* provider = new QgsVectorLayerLabelProvider( vl2.get(), "test", true, &settings );
QgsFeature f( vl2->fields(), 1 );

QString wkt1 = "POLYGON((0 0,8 0,8 -90,0 0))";
f.setGeometry( QgsGeometry().fromWkt( wkt1 ) );

// make a fake render context
QSize size( 640, 480 );
QgsMapSettings mapSettings;
mapSettings.setCrsTransformEnabled( true );
QgsCoordinateReferenceSystem tgtCrs;
tgtCrs.createFromString( "EPSG:3857" );
mapSettings.setDestinationCrs( tgtCrs );

mapSettings.setOutputSize( size );
mapSettings.setExtent( vl2->extent() );
mapSettings.setLayers( QStringList() << vl2->id() );
mapSettings.setOutputDpi( 96 );
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
QStringList attributes;
QgsLabelingEngineV2 engine;
engine.setMapSettings( mapSettings );
engine.addProvider( provider );
provider->prepare( context, attributes );

provider->registerFeature( f, context );
QCOMPARE( provider->mLabels.size(), 0 );
}

QTEST_MAIN( TestQgsLabelingEngineV2 )
#include "testqgslabelingenginev2.moc"

0 comments on commit 126cf5f

Please sign in to comment.