From 0e00079ff0a13a36ab19ec5611a1112ab9d739df Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 1 Feb 2024 13:13:44 +0100 Subject: [PATCH 1/2] OGRGeometryFactory::createGeometry(): do not assert on wkbUnkown input --- autotest/cpp/test_ogr.cpp | 5 +++++ ogr/ogrgeometryfactory.cpp | 3 +++ 2 files changed, 8 insertions(+) diff --git a/autotest/cpp/test_ogr.cpp b/autotest/cpp/test_ogr.cpp index 448b40b41aa6..0d3044633a28 100644 --- a/autotest/cpp/test_ogr.cpp +++ b/autotest/cpp/test_ogr.cpp @@ -425,6 +425,11 @@ TEST_F(test_ogr, geometry_get_point) } } +TEST_F(test_ogr, OGR_G_CreateGeometry_unknown) +{ + EXPECT_EQ(OGR_G_CreateGeometry(wkbUnknown), nullptr); +} + TEST_F(test_ogr, style_manager) { OGRStyleMgrH hSM = OGR_SM_Create(nullptr); diff --git a/ogr/ogrgeometryfactory.cpp b/ogr/ogrgeometryfactory.cpp index 0cf4395f98fa..8e54c8c9ca46 100644 --- a/ogr/ogrgeometryfactory.cpp +++ b/ogr/ogrgeometryfactory.cpp @@ -621,6 +621,9 @@ OGRGeometryFactory::createGeometry(OGRwkbGeometryType eGeometryType) poGeom = new (std::nothrow) OGRTriangulatedSurface(); break; + case wkbUnknown: + break; + default: CPLAssert(false); break; From 73d4ccb5f06b7299ca39119b1b6b47380e0a80a4 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 1 Feb 2024 13:05:45 +0100 Subject: [PATCH 2/2] OGRGeometryFactory::forceTo(): fix assertion with empty geometry and target type = unknown Found by CIFuzz: https://github.com/OSGeo/gdal/actions/runs/7740101693/job/21105197876?pr=9166 --- autotest/ogr/ogr_factory.py | 25 +++++++++++++++++++++++++ ogr/ogrgeometryfactory.cpp | 8 ++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/autotest/ogr/ogr_factory.py b/autotest/ogr/ogr_factory.py index 14a084a9de73..d1a7c1a8cf6e 100755 --- a/autotest/ogr/ogr_factory.py +++ b/autotest/ogr/ogr_factory.py @@ -902,6 +902,31 @@ def test_ogr_factory_8(): ogrtest.check_feature_geometry(dst_geom, exp_wkt) +############################################################################### +# Test forceTo() to wkbUnknown + + +@pytest.mark.parametrize( + "src_wkt,target_type,exp_wkt", + [ + ( + "POINT (1 2)", + ogr.wkbUnknown, + "POINT (1 2)", + ), + ( + "POINT EMPTY", + ogr.wkbUnknown, + "POINT EMPTY", + ), + ], +) +def test_ogr_factory_forceTo_unknown(src_wkt, target_type, exp_wkt): + src_geom = ogr.CreateGeometryFromWkt(src_wkt) + dst_geom = ogr.ForceTo(src_geom, target_type) + ogrtest.check_feature_geometry(dst_geom, exp_wkt) + + ############################################################################### # Test forceTo() diff --git a/ogr/ogrgeometryfactory.cpp b/ogr/ogrgeometryfactory.cpp index 8e54c8c9ca46..44647d2915c7 100644 --- a/ogr/ogrgeometryfactory.cpp +++ b/ogr/ogrgeometryfactory.cpp @@ -4554,6 +4554,10 @@ OGRGeometry *OGRGeometryFactory::forceTo(OGRGeometry *poGeom, if (poGeom == nullptr) return poGeom; + const OGRwkbGeometryType eTargetTypeFlat = wkbFlatten(eTargetType); + if (eTargetTypeFlat == wkbUnknown) + return poGeom; + if (poGeom->IsEmpty()) { OGRGeometry *poRet = createGeometry(eTargetType); @@ -4567,10 +4571,6 @@ OGRGeometry *OGRGeometryFactory::forceTo(OGRGeometry *poGeom, return poRet; } - const OGRwkbGeometryType eTargetTypeFlat = wkbFlatten(eTargetType); - if (eTargetTypeFlat == wkbUnknown) - return poGeom; - OGRwkbGeometryType eType = poGeom->getGeometryType(); OGRwkbGeometryType eTypeFlat = wkbFlatten(eType);