Skip to content

Commit

Permalink
MSGN: fix memleak in error code path
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Nov 15, 2023
1 parent 8944c22 commit 3344a2b
Showing 1 changed file with 11 additions and 34 deletions.
45 changes: 11 additions & 34 deletions frmts/msgn/msgndataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,22 @@ GDALDataset *MSGNDataset::Open(GDALOpenInfo *poOpenInfo)
{
open_mode_type open_mode = MODE_VISIR;
GDALOpenInfo *open_info = poOpenInfo;
std::unique_ptr<GDALOpenInfo> poOpenInfoToFree;

if (!poOpenInfo->bStatOK)
{
if (STARTS_WITH_CI(poOpenInfo->pszFilename, "HRV:"))
{
open_info = new GDALOpenInfo(&poOpenInfo->pszFilename[4],
poOpenInfo->eAccess);
poOpenInfoToFree = std::make_unique<GDALOpenInfo>(
&poOpenInfo->pszFilename[4], poOpenInfo->eAccess);
open_info = poOpenInfoToFree.get();
open_mode = MODE_HRV;
}
else if (STARTS_WITH_CI(poOpenInfo->pszFilename, "RAD:"))
{
open_info = new GDALOpenInfo(&poOpenInfo->pszFilename[4],
poOpenInfo->eAccess);
poOpenInfoToFree = std::make_unique<GDALOpenInfo>(
&poOpenInfo->pszFilename[4], poOpenInfo->eAccess);
open_info = poOpenInfoToFree.get();
open_mode = MODE_RAD;
}
}
Expand All @@ -446,21 +449,13 @@ GDALDataset *MSGNDataset::Open(GDALOpenInfo *poOpenInfo)
/* -------------------------------------------------------------------- */
if (open_info->fpL == nullptr || open_info->nHeaderBytes < 50)
{
if (open_info != poOpenInfo)
{
delete open_info;
}
return nullptr;
}

/* check if this is a "NATIVE" MSG format image */
if (!STARTS_WITH_CI((char *)open_info->pabyHeader,
"FormatName : NATIVE"))
{
if (open_info != poOpenInfo)
{
delete open_info;
}
return nullptr;
}

Expand All @@ -472,10 +467,6 @@ GDALDataset *MSGNDataset::Open(GDALOpenInfo *poOpenInfo)
CPLError(CE_Failure, CPLE_NotSupported,
"The MSGN driver does not support update access to existing"
" datasets.\n");
if (open_info != poOpenInfo)
{
delete open_info;
}
return nullptr;
}

Expand All @@ -485,14 +476,10 @@ GDALDataset *MSGNDataset::Open(GDALOpenInfo *poOpenInfo)
VSILFILE *fp = VSIFOpenL(open_info->pszFilename, "rb");
if (fp == nullptr)
{
if (open_info != poOpenInfo)
{
delete open_info;
}
return nullptr;
}

MSGNDataset *poDS = new MSGNDataset();
auto poDS = std::make_unique<MSGNDataset>();

poDS->m_open_mode = open_mode;
poDS->fp = fp;
Expand All @@ -507,11 +494,6 @@ GDALDataset *MSGNDataset::Open(GDALOpenInfo *poOpenInfo)

if (!poDS->msg_reader_core->get_open_success())
{
if (open_info != poOpenInfo)
{
delete open_info;
}
delete poDS;
return nullptr;
}

Expand Down Expand Up @@ -675,8 +657,8 @@ GDALDataset *MSGNDataset::Open(GDALOpenInfo *poOpenInfo)
if (ok_to_add)
{
poDS->SetBand(band_count,
new MSGNRasterBand(poDS, band_count, open_mode,
i + 1,
new MSGNRasterBand(poDS.get(), band_count,
open_mode, i + 1,
i + 1 - missing_band_count));
band_map[band_count] = (unsigned char)(i + 1);
band_count++;
Expand Down Expand Up @@ -824,12 +806,7 @@ GDALDataset *MSGNDataset::Open(GDALOpenInfo *poOpenInfo)
poDS->msg_reader_core->get_col_start());
poDS->SetMetadataItem("Origin", field);

if (open_info != poOpenInfo)
{
delete open_info;
}

return poDS;
return poDS.release();
}

/************************************************************************/
Expand Down

0 comments on commit 3344a2b

Please sign in to comment.