From 6ccef058a117b647d30da79ddf0f88da5e17dd65 Mon Sep 17 00:00:00 2001 From: AbelPau Date: Mon, 3 Feb 2025 10:57:37 +0100 Subject: [PATCH] MiraMonVector: Fix issue 393742177 oss-fuzz Fixing a memory leak that happens in the case where polygon zero creates the database but some error occurs in the creation of the polygon zero. The database must be destroyed and created again next time the polygon zero is created. It's been reproduced in the case of skipFailures==true. --- ogr/ogrsf_frmts/miramon/mm_wrlayr.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ogr/ogrsf_frmts/miramon/mm_wrlayr.c b/ogr/ogrsf_frmts/miramon/mm_wrlayr.c index 3557731fd13f..54f53e9c1287 100644 --- a/ogr/ogrsf_frmts/miramon/mm_wrlayr.c +++ b/ogr/ogrsf_frmts/miramon/mm_wrlayr.c @@ -3729,6 +3729,7 @@ static int MMCreateFeaturePolOrArc(struct MiraMonVectLayerInfo *hMiraMonLayer, MM_N_VERTICES_TYPE nPolVertices = 0; MM_BOOLEAN bReverseArc; int prevCoord = -1; + MM_BOOLEAN bPolZeroJustCreated = FALSE; if (!hMiraMonLayer) return MM_FATAL_ERROR_WRITING_FEATURES; @@ -3824,7 +3825,16 @@ static int MMCreateFeaturePolOrArc(struct MiraMonVectLayerInfo *hMiraMonLayer, // Universal polygon have a record with ID_GRAFIC=0 and blancs if (MMAddPolygonRecordToMMDB(hMiraMonLayer, nullptr, 0, 0, nullptr)) + { + // Rare case where polygon zero creates the database + // but some error occurs in the creation of the polygon zero. + // The database must be destroyed and created again + // next time the polygon zero is created. + MMCloseMMBD_XP(hMiraMonLayer); + MMDestroyMMDB(hMiraMonLayer); return MM_FATAL_ERROR_WRITING_FEATURES; + } + bPolZeroJustCreated = TRUE; } } @@ -3967,7 +3977,18 @@ static int MMCreateFeaturePolOrArc(struct MiraMonVectLayerInfo *hMiraMonLayer, pCoord = hMMFeature->pCoord; if (!pCoord) + { + if (bPolZeroJustCreated) + { + // Rare case where polygon zero creates the database + // but some error occurs in the creation of the polygon zero. + // The database must be destroyed and created again + // next time the polygon zero is created. + MMCloseMMBD_XP(hMiraMonLayer); + MMDestroyMMDB(hMiraMonLayer); + } return MM_FATAL_ERROR_WRITING_FEATURES; + } // Doing real job for (nIPart = 0; nIPart < hMMFeature->nNRings; nIPart++,