Skip to content

Commit

Permalink
Merge pull request OSGeo#11498 from rouault/cppckeck_nullPointerOutOf…
Browse files Browse the repository at this point in the history
…Memory

Fix cppcheck nullPointerOutOfMemory warnings
  • Loading branch information
rouault authored Dec 18, 2024
2 parents 51ce0b6 + 0ceac29 commit c9955a9
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 141 deletions.
5 changes: 5 additions & 0 deletions frmts/grib/degrib/degrib/degrib1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,11 @@ int GRIB1_RefTime (VSILFILE *fp, uInt4 gribLen, double *refTime)
return -1;
}
pds = (uChar *) malloc (sectLen * sizeof (uChar));
if(!pds)
{
errSprintf("Out of memory");
return -1;
}
*pds = *temp;
pds[1] = temp[1];
pds[2] = temp[2];
Expand Down
8 changes: 8 additions & 0 deletions frmts/grib/degrib/degrib/metaparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ static int ParseSect2_Wx (float *rdat, sInt4 nrdat, sInt4 *idat,

buffLen = 0;
buffer = (char *) malloc ((nidat + 1) * sizeof (char));
if( !buffer ) {
errSprintf ("Out of memory\n");
return -1;
}
while (groupLen > 0) {
for (j = 0; j < groupLen; j++) {
buffer[buffLen] = (char) idat[loc];
Expand Down Expand Up @@ -600,6 +604,10 @@ static int ParseSect2_Hazard (float *rdat, sInt4 nrdat, sInt4 *idat,

buffLen = 0;
buffer = (char *) malloc ((nidat + 1) * sizeof (char));
if (!buffer) {
errSprintf ("Out of memory\n");
return -1;
}
while (groupLen > 0) {
for (j = 0; j < groupLen; j++) {
buffer[buffLen] = (char) idat[loc];
Expand Down
9 changes: 5 additions & 4 deletions frmts/iso8211/8211createfromxml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ int main(int nArgc, char *papszArgv[])
{
pszValue += 2;
int nDataLen = (int)strlen(pszValue) / 2;
char *pabyData = (char *)malloc(nDataLen);
char *pabyData = (char *)CPLMalloc(nDataLen);
for (int i = 0; i < nDataLen; i++)
{
char c;
Expand All @@ -281,7 +281,7 @@ int main(int nArgc, char *papszArgv[])
}
poRec->SetFieldRaw(poField, nFieldOcc,
(const char *)pabyData, nDataLen);
free(pabyData);
CPLFree(pabyData);
}
else
{
Expand Down Expand Up @@ -330,7 +330,8 @@ int main(int nArgc, char *papszArgv[])
pszSubfieldValue += 2;
int nDataLen =
(int)strlen(pszSubfieldValue) / 2;
char *pabyData = (char *)malloc(nDataLen);
char *pabyData =
(char *)CPLMalloc(nDataLen);
for (int i = 0; i < nDataLen; i++)
{
int nHigh;
Expand All @@ -351,7 +352,7 @@ int main(int nArgc, char *papszArgv[])
pszFieldName, nFieldOcc,
pszSubfieldName, nOcc, pabyData,
nDataLen);
free(pabyData);
CPLFree(pabyData);
}
}
psSubfieldIter = psSubfieldIter->psNext;
Expand Down
12 changes: 10 additions & 2 deletions frmts/northwood/grcdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,11 @@ NWT_GRCDataset::~NWT_GRCDataset()
CSLDestroy(papszCategories);

NWT_GRCDataset::FlushCache(true);
pGrd->fp = nullptr; // this prevents nwtCloseGrid from closing the fp
nwtCloseGrid(pGrd);
if (pGrd)
{
pGrd->fp = nullptr; // this prevents nwtCloseGrid from closing the fp
nwtCloseGrid(pGrd);
}

if (fp != nullptr)
VSIFCloseL(fp);
Expand Down Expand Up @@ -326,6 +329,11 @@ GDALDataset *NWT_GRCDataset::Open(GDALOpenInfo *poOpenInfo)
VSIFSeekL(poDS->fp, 0, SEEK_SET);
VSIFReadL(poDS->abyHeader, 1, 1024, poDS->fp);
poDS->pGrd = static_cast<NWT_GRID *>(malloc(sizeof(NWT_GRID)));
if (!poDS->pGrd)
{
delete poDS;
return nullptr;
}

poDS->pGrd->fp = poDS->fp;

Expand Down
19 changes: 16 additions & 3 deletions frmts/northwood/grddataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,11 @@ NWT_GRDDataset::~NWT_GRDDataset()
{
NWT_GRDDataset::FlushCache(true);
}
pGrd->fp = nullptr; // this prevents nwtCloseGrid from closing the fp
nwtCloseGrid(pGrd);
if (pGrd)
{
pGrd->fp = nullptr; // this prevents nwtCloseGrid from closing the fp
nwtCloseGrid(pGrd);
}
if (m_poSRS)
m_poSRS->Release();

Expand All @@ -461,7 +464,7 @@ NWT_GRDDataset::~NWT_GRDDataset()
CPLErr NWT_GRDDataset::FlushCache(bool bAtClosing)
{
// Ensure the header and TAB file are up to date
if (bUpdateHeader)
if (bUpdateHeader && pGrd)
{
#ifndef NO_MITAB_SUPPORT
UpdateHeader();
Expand Down Expand Up @@ -625,6 +628,11 @@ GDALDataset *NWT_GRDDataset::Open(GDALOpenInfo *poOpenInfo)
VSIFSeekL(poDS->fp, 0, SEEK_SET);
VSIFReadL(poDS->abyHeader, 1, 1024, poDS->fp);
poDS->pGrd = reinterpret_cast<NWT_GRID *>(calloc(1, sizeof(NWT_GRID)));
if (!poDS->pGrd)
{
delete poDS;
return nullptr;
}

poDS->pGrd->fp = poDS->fp;

Expand Down Expand Up @@ -902,6 +910,11 @@ GDALDataset *NWT_GRDDataset::Create(const char *pszFilename, int nXSize,
NWT_GRDDataset *poDS = new NWT_GRDDataset();
poDS->eAccess = GA_Update;
poDS->pGrd = static_cast<NWT_GRID *>(calloc(1, sizeof(NWT_GRID)));
if (!poDS->pGrd)
{
delete poDS;
return nullptr;
}

// We currently only support GRD grid types (could potentially support GRC
// in the papszParamList). Also only support GDT_Float32 as the data type.
Expand Down
28 changes: 26 additions & 2 deletions frmts/northwood/northwood.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,22 @@ int nwt_ParseHeader(NWT_GRID *pGrd, const unsigned char *nwtHeader)
CPL_LSBPTR16(&usTmp);
pGrd->stClassDict = reinterpret_cast<NWT_CLASSIFIED_DICT *>(
calloc(1, sizeof(NWT_CLASSIFIED_DICT)));
if (!pGrd->stClassDict)
{
return FALSE;
}

pGrd->stClassDict->nNumClassifiedItems = usTmp;

pGrd->stClassDict->stClassifiedItem =
reinterpret_cast<NWT_CLASSIFIED_ITEM **>(
calloc(pGrd->stClassDict->nNumClassifiedItems + 1,
sizeof(NWT_CLASSIFIED_ITEM *)));
if (!pGrd->stClassDict->stClassifiedItem)
{
pGrd->stClassDict->nNumClassifiedItems = 0;
return FALSE;
}

// load the dictionary
for (unsigned int iItem = 0;
Expand All @@ -187,6 +196,10 @@ int nwt_ParseHeader(NWT_GRID *pGrd, const unsigned char *nwtHeader)
pGrd->stClassDict->stClassifiedItem[iItem] =
reinterpret_cast<NWT_CLASSIFIED_ITEM *>(
calloc(1, sizeof(NWT_CLASSIFIED_ITEM)));
if (!psItem)
{
return FALSE;
}

unsigned char cTmp[256];
if (!VSIFReadL(&cTmp, 9, 1, pGrd->fp))
Expand Down Expand Up @@ -404,13 +417,24 @@ NWT_GRID *nwtOpenGrid(char *filename)
}

if (!VSIFReadL(nwtHeader, 1024, 1, fp))
{
VSIFCloseL(fp);
return nullptr;
}

if (nwtHeader[0] != 'H' || nwtHeader[1] != 'G' || nwtHeader[2] != 'P' ||
nwtHeader[3] != 'C')
{
VSIFCloseL(fp);
return nullptr;
}

NWT_GRID *pGrd = reinterpret_cast<NWT_GRID *>(calloc(1, sizeof(NWT_GRID)));
if (!pGrd)
{
VSIFCloseL(fp);
return nullptr;
}

if (nwtHeader[4] == '1')
pGrd->cFormat = 0x00; // grd - surface type
Expand All @@ -420,8 +444,8 @@ NWT_GRID *nwtOpenGrid(char *filename)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Unhandled Northwood format type = %0xd", nwtHeader[4]);
if (pGrd)
free(pGrd);
VSIFCloseL(fp);
free(pGrd);
return nullptr;
}

Expand Down
Loading

0 comments on commit c9955a9

Please sign in to comment.