From fe25f91b46c7cf1ae694fccc53fe383059bc76ee Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 20 Jan 2025 00:51:18 +0100 Subject: [PATCH] OGRGeomFieldDefn copy cstor: avoid false positive warnings with cppcheck master, and add testing --- autotest/cpp/test_ogr.cpp | 30 ++++++++++++++++++++++++++++++ ogr/ogrgeomfielddefn.cpp | 13 +++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/autotest/cpp/test_ogr.cpp b/autotest/cpp/test_ogr.cpp index e6605d9f03b8..3faf8f0791b9 100644 --- a/autotest/cpp/test_ogr.cpp +++ b/autotest/cpp/test_ogr.cpp @@ -2483,6 +2483,36 @@ TEST_F(test_ogr, feature_defn_geomfields_iterator) EXPECT_EQ(i, oFDefn.GetGeomFieldCount()); } +// Test OGRGeomFieldDefn copy constructor +TEST_F(test_ogr, geom_field_defn_copy_constructor) +{ + { + OGRGeomFieldDefn oGeomFieldDefn("field1", wkbPoint); + oGeomFieldDefn.SetNullable(false); + OGRGeomFieldDefn oGeomFieldDefn2("field2", wkbLineString); + oGeomFieldDefn2 = oGeomFieldDefn; + EXPECT_TRUE(oGeomFieldDefn2.IsSame(&oGeomFieldDefn)); + } + + { + OGRSpatialReference oSRS; + oSRS.SetFromUserInput("WGS84"); + EXPECT_EQ(oSRS.GetReferenceCount(), 1); + OGRGeomFieldDefn oGeomFieldDefn("field1", wkbPoint); + oGeomFieldDefn.SetSpatialRef(&oSRS); + EXPECT_EQ(oSRS.GetReferenceCount(), 2); + OGRGeomFieldDefn oGeomFieldDefn2("field2", wkbLineString); + oGeomFieldDefn2 = oGeomFieldDefn; + EXPECT_EQ(oSRS.GetReferenceCount(), 3); + EXPECT_TRUE(oGeomFieldDefn2.IsSame(&oGeomFieldDefn)); + + // oGeomFieldDefn2 already points to oSRS + oGeomFieldDefn2 = oGeomFieldDefn; + EXPECT_EQ(oSRS.GetReferenceCount(), 3); + EXPECT_TRUE(oGeomFieldDefn2.IsSame(&oGeomFieldDefn)); + } +} + // Test GDALDataset QueryLoggerFunc callback TEST_F(test_ogr, GDALDatasetSetQueryLoggerFunc) { diff --git a/ogr/ogrgeomfielddefn.cpp b/ogr/ogrgeomfielddefn.cpp index 9942cf2c8ec1..5cee03bcfcf8 100644 --- a/ogr/ogrgeomfielddefn.cpp +++ b/ogr/ogrgeomfielddefn.cpp @@ -157,10 +157,15 @@ OGRGeomFieldDefn &OGRGeomFieldDefn::operator=(const OGRGeomFieldDefn &oOther) { if (&oOther != this) { - SetName(oOther.pszName); - SetType(oOther.eGeomType); - SetSpatialRef(oOther.poSRS); - SetNullable(oOther.bNullable); + CPLFree(pszName); + pszName = CPLStrdup(oOther.pszName); + eGeomType = oOther.eGeomType; + if (oOther.poSRS) + const_cast(oOther.poSRS)->Reference(); + if (poSRS) + const_cast(poSRS)->Dereference(); + poSRS = oOther.poSRS; + bNullable = oOther.bNullable; m_oCoordPrecision = oOther.m_oCoordPrecision; m_bSealed = oOther.m_bSealed; bIgnore = oOther.bIgnore;