From dbfa13bba970d7a3ea2861305349d2c7342ad78c Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Mon, 17 Oct 2022 16:29:36 +0200 Subject: [PATCH] Fix crash on empty polygon conversion to curve Closes GH-50466 in 3.22 branch Includes unit test --- src/core/geometry/qgspolygon.cpp | 11 +++++++---- tests/src/core/geometry/testqgspolygon.cpp | 5 ++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/core/geometry/qgspolygon.cpp b/src/core/geometry/qgspolygon.cpp index 104279704b74..6f46c70eca51 100644 --- a/src/core/geometry/qgspolygon.cpp +++ b/src/core/geometry/qgspolygon.cpp @@ -316,11 +316,14 @@ QgsPolygon *QgsPolygon::surfaceToPolygon() const QgsCurvePolygon *QgsPolygon::toCurveType() const { QgsCurvePolygon *curvePolygon = new QgsCurvePolygon(); - curvePolygon->setExteriorRing( mExteriorRing->clone() ); - int nInteriorRings = mInteriorRings.size(); - for ( int i = 0; i < nInteriorRings; ++i ) + if ( mExteriorRing ) { - curvePolygon->addInteriorRing( mInteriorRings.at( i )->clone() ); + curvePolygon->setExteriorRing( mExteriorRing->clone() ); + int nInteriorRings = mInteriorRings.size(); + for ( int i = 0; i < nInteriorRings; ++i ) + { + curvePolygon->addInteriorRing( mInteriorRings.at( i )->clone() ); + } } return curvePolygon; } diff --git a/tests/src/core/geometry/testqgspolygon.cpp b/tests/src/core/geometry/testqgspolygon.cpp index 70cb31551fc6..e472a7bb20f5 100644 --- a/tests/src/core/geometry/testqgspolygon.cpp +++ b/tests/src/core/geometry/testqgspolygon.cpp @@ -592,7 +592,10 @@ void TestQgsPolygon::polygon() QCOMPARE( *toP, p12 ); //toCurveType - std::unique_ptr< QgsCurvePolygon > curveType( p12.toCurveType() ); + std::unique_ptr< QgsCurvePolygon > curveType( QgsPolygon().toCurveType() ); + QCOMPARE( curveType->wkbType(), QgsWkbTypes::CurvePolygon ); + + curveType.reset( p12.toCurveType() ); QCOMPARE( curveType->wkbType(), QgsWkbTypes::CurvePolygonZM ); QCOMPARE( curveType->exteriorRing()->numPoints(), 5 ); QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::PointZM, 0, 0, 1, 5 ) );