Skip to content

Commit

Permalink
MiraMonVector: fix memory leak on corrupted products (oss-fuzz 369216…
Browse files Browse the repository at this point in the history
…702) (#10872)
  • Loading branch information
AbelPau authored Sep 25, 2024
1 parent 388d97f commit 5ed310e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 15 additions & 0 deletions ogr/ogrsf_frmts/miramon/mm_wrlayr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6255,6 +6255,11 @@ int MMCreateMMDB(struct MiraMonVectLayerInfo *hMiraMonLayer,
hMiraMonLayer->nSRSType = MM_SRS_LAYER_IS_GEOGRAPHIC_TYPE;
}

// Before allocating new memory, there might be some previously allocated but unused memory.
// Let's free that memory first.
if (hMiraMonLayer->MMArc.MMAdmDB.pMMBDXP)
MM_ReleaseDBFHeader(&hMiraMonLayer->MMArc.MMAdmDB.pMMBDXP);

if (hMiraMonLayer->bIsPoint)
{
if (hMiraMonLayer->pLayerDB)
Expand Down Expand Up @@ -6293,6 +6298,11 @@ int MMCreateMMDB(struct MiraMonVectLayerInfo *hMiraMonLayer,
: 9)))
return 1;

// Before allocating new memory, there might be some previously allocated but unused memory.
// Let's free that memory first.
if (hMiraMonLayer->MMArc.MMNode.MMAdmDB.pMMBDXP)
MM_ReleaseDBFHeader(&hMiraMonLayer->MMArc.MMNode.MMAdmDB.pMMBDXP);

pBD_XP_Aux = hMiraMonLayer->MMArc.MMNode.MMAdmDB.pMMBDXP =
MM_CreateDBFHeader(3, hMiraMonLayer->nCharSet);

Expand Down Expand Up @@ -6326,6 +6336,11 @@ int MMCreateMMDB(struct MiraMonVectLayerInfo *hMiraMonLayer,
: 12)))
return 1;

// Before allocating new memory, there might be some previously allocated but unused memory.
// Let's free that memory first.
if (hMiraMonLayer->MMArc.MMNode.MMAdmDB.pMMBDXP)
MM_ReleaseDBFHeader(&hMiraMonLayer->MMArc.MMNode.MMAdmDB.pMMBDXP);

pBD_XP_Aux = hMiraMonLayer->MMPolygon.MMArc.MMAdmDB.pMMBDXP =
MM_CreateDBFHeader(5, hMiraMonLayer->nCharSet);

Expand Down
21 changes: 19 additions & 2 deletions ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ OGRMiraMonLayer::~OGRMiraMonLayer()
if (MMCloseLayer(&hMiraMonLayerPOL))
{
CPLDebugOnly("MiraMon", "Error closing polygons layer");

// In case of closing we need to destroy memory
MMDestroyLayer(&hMiraMonLayerPOL);
}
if (hMiraMonLayerPOL.TopHeader.nElemCount)
{
Expand All @@ -548,6 +551,9 @@ OGRMiraMonLayer::~OGRMiraMonLayer()
if (MMCloseLayer(&hMiraMonLayerARC))
{
CPLDebugOnly("MiraMon", "Error closing arcs layer");

// In case of closing we need to destroy memory
MMDestroyLayer(&hMiraMonLayerARC);
}
if (hMiraMonLayerARC.TopHeader.nElemCount)
{
Expand All @@ -570,6 +576,9 @@ OGRMiraMonLayer::~OGRMiraMonLayer()
if (MMCloseLayer(&hMiraMonLayerPNT))
{
CPLDebugOnly("MiraMon", "Error closing points layer");

// In case of closing we need to destroy memory
MMDestroyLayer(&hMiraMonLayerPNT);
}
if (hMiraMonLayerPNT.TopHeader.nElemCount)
{
Expand All @@ -593,7 +602,11 @@ OGRMiraMonLayer::~OGRMiraMonLayer()
{
CPLDebugOnly("MiraMon", "Closing MiraMon DBF table ...");
}
MMCloseLayer(&hMiraMonLayerReadOrNonGeom);
if (MMCloseLayer(&hMiraMonLayerReadOrNonGeom))
{
// In case of closing we need to destroy memory
MMDestroyLayer(&hMiraMonLayerReadOrNonGeom);
}
if (hMiraMonLayerReadOrNonGeom.ReadOrWrite == MM_WRITING_MODE)
{
CPLDebugOnly("MiraMon", "MiraMon DBF table closed");
Expand All @@ -610,7 +623,11 @@ OGRMiraMonLayer::~OGRMiraMonLayer()
{
CPLDebugOnly("MiraMon", "Closing MiraMon layer ...");
}
MMCloseLayer(&hMiraMonLayerReadOrNonGeom);
if (MMCloseLayer(&hMiraMonLayerReadOrNonGeom))
{
// In case of closing we need to destroy memory
MMDestroyLayer(&hMiraMonLayerReadOrNonGeom);
}
if (hMiraMonLayerReadOrNonGeom.ReadOrWrite == MM_WRITING_MODE)
{
CPLDebugOnly("MiraMon", "MiraMon layer closed");
Expand Down

0 comments on commit 5ed310e

Please sign in to comment.