Skip to content

Commit 7619cba

Browse files
committed
Fix/avoid Coverity null pointer dereference warnings
1 parent f84508a commit 7619cba

12 files changed

+47
-35
lines changed

src/analysis/vector/qgsgeometrysnapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ void QgsGeometrySnapper::processFeature( QgsFeature& feature, double snapToleran
481481

482482
QgsGeometry QgsGeometrySnapper::snapGeometry( const QgsGeometry& geometry, double snapTolerance, SnapMode mode ) const
483483
{
484-
QgsPointV2 center = dynamic_cast< const QgsPointV2* >( geometry.geometry() ) ? *dynamic_cast< const QgsPointV2* >( geometry.geometry() ) :
484+
QgsPointV2 center = dynamic_cast< const QgsPointV2* >( geometry.geometry() ) ? *static_cast< const QgsPointV2* >( geometry.geometry() ) :
485485
QgsPointV2( geometry.geometry()->boundingBox().center() );
486486

487487
// Get potential reference features and construct snap index

src/app/composer/qgscomposerpolygonwidget.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ void QgsComposerPolygonWidget::setGuiElementValues()
8585

8686
void QgsComposerPolygonWidget::updateStyleFromWidget()
8787
{
88-
QgsSymbolSelectorWidget* w = qobject_cast<QgsSymbolSelectorWidget*>( sender() );
89-
mComposerPolygon->setPolygonStyleSymbol( dynamic_cast< QgsFillSymbol* >( w->symbol() ) );
90-
mComposerPolygon->update();
88+
if ( QgsSymbolSelectorWidget* w = qobject_cast<QgsSymbolSelectorWidget*>( sender() ) )
89+
{
90+
mComposerPolygon->setPolygonStyleSymbol( dynamic_cast< QgsFillSymbol* >( w->symbol() ) );
91+
mComposerPolygon->update();
92+
}
9193
}
9294

9395
void QgsComposerPolygonWidget::cleanUpStyleSelector( QgsPanelWidget* container )

src/app/composer/qgscomposerpolylinewidget.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ void QgsComposerPolylineWidget::setGuiElementValues()
7878

7979
void QgsComposerPolylineWidget::updateStyleFromWidget()
8080
{
81-
QgsSymbolSelectorWidget* w = qobject_cast<QgsSymbolSelectorWidget*>( sender() );
82-
mComposerPolyline->setPolylineStyleSymbol( dynamic_cast< QgsLineSymbol* >( w->symbol() ) );
83-
mComposerPolyline->update();
81+
if ( QgsSymbolSelectorWidget* w = qobject_cast<QgsSymbolSelectorWidget*>( sender() ) )
82+
{
83+
mComposerPolyline->setPolylineStyleSymbol( dynamic_cast< QgsLineSymbol* >( w->symbol() ) );
84+
mComposerPolyline->update();
85+
}
8486
}
8587

8688
void QgsComposerPolylineWidget::cleanUpStyleSelector( QgsPanelWidget* container )

src/app/composer/qgscomposershapewidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ void QgsComposerShapeWidget::toggleRadiusSpin( const QString& shapeText )
182182

183183
void QgsComposerShapeWidget::updateSymbolFromWidget()
184184
{
185-
QgsSymbolSelectorWidget* w = qobject_cast<QgsSymbolSelectorWidget*>( sender() );
186-
mComposerShape->setShapeStyleSymbol( dynamic_cast< QgsFillSymbol* >( w->symbol() ) );
185+
if ( QgsSymbolSelectorWidget* w = qobject_cast<QgsSymbolSelectorWidget*>( sender() ) )
186+
mComposerShape->setShapeStyleSymbol( dynamic_cast< QgsFillSymbol* >( w->symbol() ) );
187187
}
188188

189189
void QgsComposerShapeWidget::cleanUpSymbolSelector( QgsPanelWidget* container )

src/app/composer/qgscompositionwidget.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,11 @@ void QgsCompositionWidget::updateVariables()
199199

200200
void QgsCompositionWidget::updateStyleFromWidget()
201201
{
202-
QgsSymbolSelectorWidget* w = qobject_cast<QgsSymbolSelectorWidget*>( sender() );
203-
mComposition->setPageStyleSymbol( dynamic_cast< QgsFillSymbol* >( w->symbol() ) );
204-
mComposition->update();
202+
if ( QgsSymbolSelectorWidget* w = qobject_cast<QgsSymbolSelectorWidget*>( sender() ) )
203+
{
204+
mComposition->setPageStyleSymbol( dynamic_cast< QgsFillSymbol* >( w->symbol() ) );
205+
mComposition->update();
206+
}
205207
}
206208

207209
void QgsCompositionWidget::cleanUpStyleSelector( QgsPanelWidget* container )

src/core/composer/qgscomposershape.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,11 @@ void QgsComposerShape::createDefaultShapeStyleSymbol()
104104
properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
105105
mShapeStyleSymbol = QgsFillSymbol::createSimple( properties );
106106

107-
QgsRenderContext rc = QgsComposerUtils::createRenderContextForMap( mComposition->referenceMap(), nullptr, mComposition->printResolution() );
108-
mMaxSymbolBleed = ( 25.4 / mComposition->printResolution() ) * QgsSymbolLayerUtils::estimateMaxSymbolBleed( mShapeStyleSymbol, rc );
107+
if ( mComposition )
108+
{
109+
QgsRenderContext rc = QgsComposerUtils::createRenderContextForMap( mComposition->referenceMap(), nullptr, mComposition->printResolution() );
110+
mMaxSymbolBleed = ( 25.4 / mComposition->printResolution() ) * QgsSymbolLayerUtils::estimateMaxSymbolBleed( mShapeStyleSymbol, rc );
111+
}
109112

110113
updateBoundingRect();
111114

src/core/qgsexpression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2765,7 +2765,7 @@ static QVariant fcnProject( const QVariantList& values, const QgsExpressionConte
27652765
double azimuth = getDoubleValue( values.at( 2 ), parent );
27662766
double inclination = getDoubleValue( values.at( 3 ), parent );
27672767

2768-
const QgsPointV2* p = dynamic_cast<const QgsPointV2*>( geom.geometry() );
2768+
const QgsPointV2* p = static_cast<const QgsPointV2*>( geom.geometry() );
27692769
QgsPointV2 newPoint = p->project( distance, 180.0 * azimuth / M_PI, 180.0 * inclination / M_PI );
27702770

27712771
return QVariant::fromValue( QgsGeometry( new QgsPointV2( newPoint ) ) );

src/core/qgsprojectproperty.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ void QgsProjectPropertyKey::dump( int tabs ) const
321321
QgsDebugMsg( QString( "%1key: <%2> subkey: <%3>" )
322322
.arg( tabString,
323323
i.key(),
324-
dynamic_cast<QgsProjectPropertyKey*>( i.value() )->name() ) );
324+
static_cast<QgsProjectPropertyKey*>( i.value() )->name() ) );
325325
i.value()->dump( tabs + 1 );
326326
}
327327

src/core/raster/qgsrasterlayer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,20 +1108,20 @@ void QgsRasterLayer::refreshRendererIfNeeded( QgsRasterRenderer* rasterRenderer,
11081108

11091109
// Update main renderer so that the legends get updated
11101110
if ( singleBandRenderer )
1111-
dynamic_cast<QgsSingleBandGrayRenderer*>( renderer() )->setContrastEnhancement( new QgsContrastEnhancement( * singleBandRenderer->contrastEnhancement() ) );
1111+
static_cast<QgsSingleBandGrayRenderer*>( renderer() )->setContrastEnhancement( new QgsContrastEnhancement( * singleBandRenderer->contrastEnhancement() ) );
11121112
else if ( multiBandRenderer )
11131113
{
11141114
if ( multiBandRenderer->redContrastEnhancement() )
11151115
{
1116-
dynamic_cast<QgsMultiBandColorRenderer*>( renderer() )->setRedContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->redContrastEnhancement() ) );
1116+
static_cast<QgsMultiBandColorRenderer*>( renderer() )->setRedContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->redContrastEnhancement() ) );
11171117
}
11181118
if ( multiBandRenderer->greenContrastEnhancement() )
11191119
{
1120-
dynamic_cast<QgsMultiBandColorRenderer*>( renderer() )->setGreenContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->greenContrastEnhancement() ) );
1120+
static_cast<QgsMultiBandColorRenderer*>( renderer() )->setGreenContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->greenContrastEnhancement() ) );
11211121
}
11221122
if ( multiBandRenderer->blueContrastEnhancement() )
11231123
{
1124-
dynamic_cast<QgsMultiBandColorRenderer*>( renderer() )->setBlueContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->blueContrastEnhancement() ) );
1124+
static_cast<QgsMultiBandColorRenderer*>( renderer() )->setBlueContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->blueContrastEnhancement() ) );
11251125
}
11261126
}
11271127

src/server/qgswmsprojectparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ void QgsWmsProjectParser::addOWSLayers( QDomDocument &doc,
15571557
}
15581558

15591559
QString layerName = currentLayer->name();
1560-
if ( mProjectParser && mProjectParser->useLayerIds() )
1560+
if ( mProjectParser->useLayerIds() )
15611561
layerName = currentLayer->id();
15621562
else if ( !currentLayer->shortName().isEmpty() )
15631563
layerName = currentLayer->shortName();

tests/src/core/testqgsproperty.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,9 @@ void TestQgsProperty::propertyTransformer()
583583
//install into property and test evaluation
584584
QgsProperty p1;
585585
p1.setTransformer( new TestTransformer( 10, 20 ) );
586-
QCOMPARE( dynamic_cast< const TestTransformer* >( p1.transformer() )->minValue(), 10.0 );
587-
QCOMPARE( dynamic_cast< const TestTransformer* >( p1.transformer() )->maxValue(), 20.0 );
586+
QVERIFY( dynamic_cast< const TestTransformer* >( p1.transformer() ) );
587+
QCOMPARE( static_cast< const TestTransformer* >( p1.transformer() )->minValue(), 10.0 );
588+
QCOMPARE( static_cast< const TestTransformer* >( p1.transformer() )->maxValue(), 20.0 );
588589
p1.setStaticValue( QVariant( QVariant::Double ) );
589590
QCOMPARE( p1.value( context, -99 ).toDouble(), -1.0 );
590591
p1.setStaticValue( 11.0 );
@@ -772,33 +773,34 @@ void TestQgsProperty::colorRampTransformer()
772773
QCOMPARE( r1.minValue(), 15.0 );
773774
QCOMPARE( r1.maxValue(), 25.0 );
774775
QCOMPARE( r1.nullColor(), QColor( 100, 150, 200 ) );
775-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( r1.colorRamp() )->color1(), QColor( 10, 20, 30 ) );
776-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( r1.colorRamp() )->color2(), QColor( 200, 190, 180 ) );
776+
QVERIFY( dynamic_cast< QgsGradientColorRamp* >( r1.colorRamp() ) );
777+
QCOMPARE( static_cast< QgsGradientColorRamp* >( r1.colorRamp() )->color1(), QColor( 10, 20, 30 ) );
778+
QCOMPARE( static_cast< QgsGradientColorRamp* >( r1.colorRamp() )->color2(), QColor( 200, 190, 180 ) );
777779

778780
// test cloning
779781
QScopedPointer< QgsColorRampTransformer > r2( t1.clone() );
780782
QCOMPARE( r2->minValue(), 15.0 );
781783
QCOMPARE( r2->maxValue(), 25.0 );
782784
QCOMPARE( r2->nullColor(), QColor( 100, 150, 200 ) );
783-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( r2->colorRamp() )->color1(), QColor( 10, 20, 30 ) );
784-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( r2->colorRamp() )->color2(), QColor( 200, 190, 180 ) );
785+
QCOMPARE( static_cast< QgsGradientColorRamp* >( r2->colorRamp() )->color1(), QColor( 10, 20, 30 ) );
786+
QCOMPARE( static_cast< QgsGradientColorRamp* >( r2->colorRamp() )->color2(), QColor( 200, 190, 180 ) );
785787

786788
// copy constructor
787789
QgsColorRampTransformer r3( t1 );
788790
QCOMPARE( r3.minValue(), 15.0 );
789791
QCOMPARE( r3.maxValue(), 25.0 );
790792
QCOMPARE( r3.nullColor(), QColor( 100, 150, 200 ) );
791-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( r3.colorRamp() )->color1(), QColor( 10, 20, 30 ) );
792-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( r3.colorRamp() )->color2(), QColor( 200, 190, 180 ) );
793+
QCOMPARE( static_cast< QgsGradientColorRamp* >( r3.colorRamp() )->color1(), QColor( 10, 20, 30 ) );
794+
QCOMPARE( static_cast< QgsGradientColorRamp* >( r3.colorRamp() )->color2(), QColor( 200, 190, 180 ) );
793795

794796
// assignment operator
795797
QgsColorRampTransformer r4;
796798
r4 = t1;
797799
QCOMPARE( r4.minValue(), 15.0 );
798800
QCOMPARE( r4.maxValue(), 25.0 );
799801
QCOMPARE( r4.nullColor(), QColor( 100, 150, 200 ) );
800-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( r4.colorRamp() )->color1(), QColor( 10, 20, 30 ) );
801-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( r4.colorRamp() )->color2(), QColor( 200, 190, 180 ) );
802+
QCOMPARE( static_cast< QgsGradientColorRamp* >( r4.colorRamp() )->color1(), QColor( 10, 20, 30 ) );
803+
QCOMPARE( static_cast< QgsGradientColorRamp* >( r4.colorRamp() )->color2(), QColor( 200, 190, 180 ) );
802804

803805
//test various min/max value/color and scaling methods
804806

@@ -811,7 +813,7 @@ void TestQgsProperty::colorRampTransformer()
811813
t.setNullColor( QColor( 1, 10, 11, 21 ) );
812814
QCOMPARE( t.nullColor(), QColor( 1, 10, 11, 21 ) );
813815
t.setColorRamp( new QgsGradientColorRamp( QColor( 10, 20, 100 ), QColor( 100, 200, 200 ) ) );
814-
QCOMPARE( dynamic_cast< QgsGradientColorRamp* >( t.colorRamp() )->color1(), QColor( 10, 20, 100 ) );
816+
QCOMPARE( static_cast< QgsGradientColorRamp* >( t.colorRamp() )->color1(), QColor( 10, 20, 100 ) );
815817

816818
//test colors
817819
QCOMPARE( t.color( 50 ), QColor( 10, 20, 100 ) ); //out of range

tests/src/core/testqgsrasterlayer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,15 +701,16 @@ void TestQgsRasterLayer::testRefreshRendererIfNeeded()
701701
{
702702
QVERIFY2( mpLandsatRasterLayer->isValid(), "landsat.tif layer is not valid!" );
703703
mpLandsatRasterLayer->setContrastEnhancement( QgsContrastEnhancement::StretchToMinimumMaximum, QgsRasterMinMaxOrigin::MinMax );
704+
QVERIFY( dynamic_cast<QgsMultiBandColorRenderer*>( mpLandsatRasterLayer->renderer() ) );
704705
mMapSettings->setLayers( QList<QgsMapLayer*>() << mpLandsatRasterLayer );
705706
mMapSettings->setExtent( mpLandsatRasterLayer->extent() );
706-
double initMinVal = dynamic_cast<QgsMultiBandColorRenderer*>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
707+
double initMinVal = static_cast<QgsMultiBandColorRenderer*>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
707708

708709
// Should do nothing
709710
QgsRectangle newExtent = QgsRectangle( 785000, 3340000, 785100, 3340100 );
710711
mpLandsatRasterLayer->refreshRendererIfNeeded( mpLandsatRasterLayer->renderer(), newExtent );
711712
QCOMPARE( mpLandsatRasterLayer->renderer()->minMaxOrigin().limits(), QgsRasterMinMaxOrigin::MinMax );
712-
double minVal = dynamic_cast<QgsMultiBandColorRenderer*>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
713+
double minVal = static_cast<QgsMultiBandColorRenderer*>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
713714
QGSCOMPARENEAR( initMinVal, minVal, 1e-5 );
714715

715716
// Change to UpdatedCanvas
@@ -719,7 +720,7 @@ void TestQgsRasterLayer::testRefreshRendererIfNeeded()
719720
mpLandsatRasterLayer->renderer()->setMinMaxOrigin( mmo );
720721
QCOMPARE( mpLandsatRasterLayer->renderer()->minMaxOrigin().extent(), QgsRasterMinMaxOrigin::UpdatedCanvas );
721722
mpLandsatRasterLayer->refreshRendererIfNeeded( mpLandsatRasterLayer->renderer(), newExtent );
722-
double newMinVal = dynamic_cast<QgsMultiBandColorRenderer*>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
723+
double newMinVal = static_cast<QgsMultiBandColorRenderer*>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
723724
QGSCOMPARENOTNEAR( initMinVal, newMinVal, 1e-5 );
724725
}
725726

0 commit comments

Comments
 (0)