Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address medium and high severity new Coverity fixes #11643

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b36a0af
gdalexif.cpp: rework to avoid Coverity Scan complaining about possibl…
rouault Jan 12, 2025
c9bafeb
ENVISAT: fix memleak in error code path (CID 1589308, 1589295)
rouault Jan 12, 2025
765d4bf
GXF: hopefully avoid false positives about memleaks (CID 1589263, ...)
rouault Jan 12, 2025
344c063
cpl_unzOpenCurrentFile3(): fix memleak in error code path (CID 1589214)
rouault Jan 12, 2025
6f24572
MRF: fix memleak in error code path (CID 1589180)
rouault Jan 12, 2025
f9de9ed
USGSDEMCreateCopy(): fix memleak in error code path (CID 1589116)
rouault Jan 12, 2025
8c9e2f0
ogrlineref.cpp: avoid potential floating point division by zero (CID …
rouault Jan 12, 2025
3b88938
gdaladdo: avoid false positive warning about division by zero
rouault Jan 12, 2025
f47c436
OGCAPI: avoid unlikely floating point division by zero
rouault Jan 12, 2025
9fbea07
OGRReadWKBGeometryType(): suppress coverity false positive (CID 1589300)
rouault Jan 12, 2025
fef5be6
gdal_rasterize: avoid unlikely floating point division by zero (CID 1…
rouault Jan 12, 2025
7890dd8
gdal_translate: avoid unlikely floating point division by zero (CID …
rouault Jan 12, 2025
116797f
gdalbuildvrt: avoid Coverity Scan warning about division by zero (CID…
rouault Jan 12, 2025
1417b49
GTIFSetFromProj4(): avoid unlikely floating point division by zero (C…
rouault Jan 12, 2025
8b04004
MRF: avoid potential division by zero (CID 1589224)
rouault Jan 12, 2025
89f9f47
ILWIS: ValueRange::iRaw(): avoid potential divide by zero (CID 1589205)
rouault Jan 12, 2025
3fa720f
STACIT: avoid potential divide by zero (CID 1589194)
rouault Jan 12, 2025
71a39e7
GDALDefaultOverviews::BuildOverviews(): avoid potential divide by zer…
rouault Jan 12, 2025
709f18a
Avoid warning about nullptr dereference (CID 1589112)
rouault Jan 12, 2025
f0b351d
GWKComputeWeights(): avoid division by zero (CID 1589107)
rouault Jan 12, 2025
ab4f3be
GPKG: avoid Coverity Scan false positive overflow_const (CID 1589099)
rouault Jan 12, 2025
cb061ec
VRT: silence Coverity false positive (CID 1589208, 1589104, 1589098)
rouault Jan 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions alg/gdalwarpkernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4383,7 +4383,9 @@ static void GWKComputeWeights(GDALResampleAlg eResample, int iMin, int iMax,

int i = iMin; // Used after for.
int iC = 0; // Used after for.
double dfAccumulatorWeightHorizontal = 0.0;
// Not zero, but as close as possible to it, to avoid potential division by
// zero at end of function
double dfAccumulatorWeightHorizontal = std::numeric_limits<double>::min();
for (; i + 2 < iMax; i += 4, iC += 4)
{
padfWeightsHorizontal[iC] = (i - dfDeltaX) * dfXScale;
Expand All @@ -4404,7 +4406,9 @@ static void GWKComputeWeights(GDALResampleAlg eResample, int iMin, int iMax,

int j = jMin; // Used after for.
int jC = 0; // Used after for.
double dfAccumulatorWeightVertical = 0.0;
// Not zero, but as close as possible to it, to avoid potential division by
// zero at end of function
double dfAccumulatorWeightVertical = std::numeric_limits<double>::min();
for (; j + 2 < jMax; j += 4, jC += 4)
{
padfWeightsVertical[jC] = (j - dfDeltaY) * dfYScale;
Expand Down
8 changes: 8 additions & 0 deletions apps/gdal_rasterize_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,12 +842,20 @@ static GDALDatasetH CreateOutputDataset(
sEnvelop.MaxY = ceil(sEnvelop.MaxY / dfYRes) * dfYRes;
}

if (dfXRes == 0 || dfYRes == 0)
{
CPLError(CE_Failure, CPLE_AppDefined, "Could not determine bounds");
return nullptr;
}

double adfProjection[6] = {sEnvelop.MinX, dfXRes, 0.0,
sEnvelop.MaxY, 0.0, -dfYRes};

if (nXSize == 0 && nYSize == 0)
{
// coverity[divide_by_zero]
const double dfXSize = 0.5 + (sEnvelop.MaxX - sEnvelop.MinX) / dfXRes;
// coverity[divide_by_zero]
const double dfYSize = 0.5 + (sEnvelop.MaxY - sEnvelop.MinY) / dfYRes;
if (dfXSize > std::numeric_limits<int>::max() ||
dfXSize < std::numeric_limits<int>::min() ||
Expand Down
2 changes: 1 addition & 1 deletion apps/gdal_translate_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
const bool bOutsizeExplicitlySet =
!(psOptions->nOXSizePixel == 0 && psOptions->dfOXSizePct == 0.0 &&
psOptions->nOYSizePixel == 0 && psOptions->dfOYSizePct == 0.0);
if (psOptions->dfXRes != 0.0)
if (psOptions->dfXRes != 0.0 && psOptions->dfYRes != 0.0)
{
if (!(bHasSrcGeoTransform && psOptions->nGCPCount == 0 &&
adfSrcGeoTransform[2] == 0.0 && adfSrcGeoTransform[4] == 0.0))
Expand Down
1 change: 1 addition & 0 deletions apps/gdaladdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ static bool PartialRefreshFromSourceExtent(
}
double dfNextCurPixels =
dfCurPixels + static_cast<double>(region.nXSize) * region.nYSize;
// coverity[divide_by_zero]
void *pScaledProgress = GDALCreateScaledProgress(
dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
pfnProgress, pProgressArg);
Expand Down
6 changes: 6 additions & 0 deletions apps/gdalbuildvrt_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ static int GetSrcDstWin(DatasetProperty *psDP, double we_res, double ns_res,
double *pdfDstYOff, double *pdfDstXSize,
double *pdfDstYSize)
{
if (we_res == 0 || ns_res == 0)
{
// should not happen. to please Coverity
return FALSE;
}

/* Check that the destination bounding box intersects the source bounding
* box */
if (psDP->adfGeoTransform[GEOTRSFRM_TOPLEFT_X] +
Expand Down
6 changes: 6 additions & 0 deletions apps/ogrlineref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,13 @@ static OGRErr GetPosition(OGRLayer *const poPkLayer, double dfX, double dfY,
// Get real distance
const double dfRealDist = Project(pCloserPart, &pt);
delete pCloserPart;
if (dfScale == 0)
{
fprintf(stderr, _("dfScale == 0.\n"));
return OGRERR_FAILURE;
}
// Compute reference distance
// coverity[divide_by_zero]
const double dfRefDist = dfBeg + dfRealDist / dfScale;
if (bQuiet)
{
Expand Down
14 changes: 9 additions & 5 deletions frmts/envisat/EnvisatFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1807,16 +1807,20 @@ int S_NameValueList_Parse(const char *text, int text_offset, int *entry_count,
/*
* Add the entry to the name/value list.
*/
(*entry_count)++;
*entries = (EnvisatNameValue **)CPLRealloc(
*entries, *entry_count * sizeof(EnvisatNameValue *));

if (*entries == NULL)
EnvisatNameValue **newEntries = VSI_REALLOC_VERBOSE(
*entries, (*entry_count + 1) * sizeof(EnvisatNameValue *));
if (!newEntries)
{
*entry_count = 0;
CPLFree(entry->key);
CPLFree(entry->value);
CPLFree(entry->literal_line);
CPLFree(entry->units);
CPLFree(entry);
return FAILURE;
}
(*entry_count)++;
*entries = newEntries;

(*entries)[*entry_count - 1] = entry;
}
Expand Down
2 changes: 1 addition & 1 deletion frmts/gtiff/libgeotiff/geotiff_proj4.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ int GTIFSetFromProj4( GTIF *gtif, const char *proj4 )
dfSemiMajor = OSR_GDV(papszNV,"a",0.0);
dfSemiMinor = OSR_GDV(papszNV,"b",0.0);
dfInvFlattening = OSR_GDV(papszNV,"rf",0.0);
if( dfSemiMinor != 0.0 && dfInvFlattening == 0.0 )
if( dfSemiMajor != 0.0 && dfSemiMinor != 0.0 && dfInvFlattening == 0.0 )
dfInvFlattening = -1.0 / (dfSemiMinor/dfSemiMajor - 1.0);
}

Expand Down
2 changes: 1 addition & 1 deletion frmts/gxf/gxf_ogcwkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static void OGCWKTSetProj(char *pszProjection, size_t nProjectionSize,
char *GXFGetMapProjectionAsOGCWKT(GXFHandle hGXF)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;
char **papszMethods = NULL;
char szWKT[1024 + 32];
char szGCS[512];
Expand Down
4 changes: 2 additions & 2 deletions frmts/gxf/gxf_proj4.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
char *GXFGetMapProjectionAsPROJ4(GXFHandle hGXF)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;
char **papszMethods = NULL;
char szPROJ4[512] = {0};

Expand Down Expand Up @@ -563,7 +563,7 @@ CPLErr GXFGetPROJ4Position(GXFHandle hGXF, double *pdfXOrigin,
double *pdfYPixelSize, double *pdfRotation)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;
char *pszProj;

/* -------------------------------------------------------------------- */
Expand Down
18 changes: 9 additions & 9 deletions frmts/gxf/gxfopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ GXFHandle GXFOpen(const char *pszFilename)
void GXFClose(GXFHandle hGXF)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;

CPLFree(psGXF->panRawLineOffset);
CPLFree(psGXF->pszUnitName);
Expand Down Expand Up @@ -633,7 +633,7 @@ static CPLErr GXFReadRawScanlineFrom(GXFInfo_t *psGXF, vsi_l_offset iOffset,
CPLErr GXFGetScanline(GXFHandle hGXF, int iScanline, double *padfLineBuf)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;
CPLErr nErr;
int iRawScanline;

Expand Down Expand Up @@ -698,7 +698,7 @@ CPLErr GXFGetScanline(GXFHandle hGXF, int iScanline, double *padfLineBuf)
CPLErr GXFGetRawScanline(GXFHandle hGXF, int iScanline, double *padfLineBuf)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;
CPLErr eErr;

/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -754,7 +754,7 @@ CPLErr GXFGetRawScanline(GXFHandle hGXF, int iScanline, double *padfLineBuf)
static void GXFScanForZMinMax(GXFHandle hGXF)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;
int iLine, iPixel;
double *padfScanline;

Expand Down Expand Up @@ -841,7 +841,7 @@ CPLErr GXFGetRawInfo(GXFHandle hGXF, int *pnXSize, int *pnYSize, int *pnSense,
double *pdfZMin, double *pdfZMax, double *pdfDummy)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;

if (pnXSize != NULL)
*pnXSize = psGXF->nRawXSize;
Expand Down Expand Up @@ -889,7 +889,7 @@ CPLErr GXFGetRawInfo(GXFHandle hGXF, int *pnXSize, int *pnYSize, int *pnSense,
char **GXFGetMapProjection(GXFHandle hGXF)

{
return (((GXFInfo_t *)hGXF)->papszMapProjection);
return ((hGXF)->papszMapProjection);
}

/************************************************************************/
Expand All @@ -911,7 +911,7 @@ char **GXFGetMapProjection(GXFHandle hGXF)
char **GXFGetMapDatumTransform(GXFHandle hGXF)

{
return (((GXFInfo_t *)hGXF)->papszMapDatumTransform);
return ((hGXF)->papszMapDatumTransform);
}

/************************************************************************/
Expand Down Expand Up @@ -948,7 +948,7 @@ CPLErr GXFGetRawPosition(GXFHandle hGXF, double *pdfXOrigin, double *pdfYOrigin,
double *pdfRotation)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;

if (pdfXOrigin != NULL)
*pdfXOrigin = psGXF->dfXOrigin;
Expand Down Expand Up @@ -1004,7 +1004,7 @@ CPLErr GXFGetPosition(GXFHandle hGXF, double *pdfXOrigin, double *pdfYOrigin,
double *pdfRotation)

{
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
GXFInfo_t *psGXF = hGXF;
double dfCXOrigin, dfCYOrigin, dfCXPixelSize, dfCYPixelSize;

switch (psGXF->nSense)
Expand Down
81 changes: 41 additions & 40 deletions frmts/gxf/gxfopen.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,49 @@
#include "cpl_conv.h"
#include "cpl_string.h"

/* -------------------------------------------------------------------- */
/* This is consider to be a private structure. */
/* -------------------------------------------------------------------- */
struct GXFInfo_t
{
VSILFILE *fp;

int nRawXSize;
int nRawYSize;
int nSense; /* GXFS_ codes */
int nGType; /* 0 is uncompressed */

double dfXPixelSize;
double dfYPixelSize;
double dfRotation;
double dfXOrigin; /* lower left corner */
double dfYOrigin; /* lower left corner */

char szDummy[64];
double dfSetDummyTo;

char *pszTitle;

double dfTransformScale;
double dfTransformOffset;
char *pszTransformName;

char **papszMapProjection;
char **papszMapDatumTransform;

char *pszUnitName;
double dfUnitToMeter;

double dfZMaximum;
double dfZMinimum;

vsi_l_offset *panRawLineOffset;
};
typedef struct GXFInfo_t GXFInfo_t;

CPL_C_START

typedef void *GXFHandle;
typedef struct GXFInfo_t *GXFHandle;

GXFHandle GXFOpen(const char *pszFilename);

Expand Down Expand Up @@ -65,43 +105,4 @@ void GXFClose(GXFHandle hGXF);

CPL_C_END

/* -------------------------------------------------------------------- */
/* This is consider to be a private structure. */
/* -------------------------------------------------------------------- */
typedef struct
{
VSILFILE *fp;

int nRawXSize;
int nRawYSize;
int nSense; /* GXFS_ codes */
int nGType; /* 0 is uncompressed */

double dfXPixelSize;
double dfYPixelSize;
double dfRotation;
double dfXOrigin; /* lower left corner */
double dfYOrigin; /* lower left corner */

char szDummy[64];
double dfSetDummyTo;

char *pszTitle;

double dfTransformScale;
double dfTransformOffset;
char *pszTransformName;

char **papszMapProjection;
char **papszMapDatumTransform;

char *pszUnitName;
double dfUnitToMeter;

double dfZMaximum;
double dfZMinimum;

vsi_l_offset *panRawLineOffset;
} GXFInfo_t;

#endif /* ndef GXFOPEN_H_INCLUDED */
4 changes: 3 additions & 1 deletion frmts/ilwis/ilwisdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2153,7 +2153,9 @@ int ValueRange::iRaw(double rValueIn) const
{
if (rValueIn == rUNDEF) // || !fContains(rValue))
return iUNDEF;
const double rEpsilon = _rStep == 0.0 ? 1e-6 : _rStep / 3.0;
if (_rStep == 0.0)
return iUNDEF;
const double rEpsilon = _rStep / 3.0;
if (rValueIn - get_rLo() < -rEpsilon) // take a little rounding tolerance
return iUNDEF;
else if (rValueIn - get_rHi() >
Expand Down
6 changes: 6 additions & 0 deletions frmts/mrf/marfa_dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ CPLErr MRFDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
config, "Rsets.scale",
CPLOPrintf("%d", panOverviewList[0]).c_str()),
nullptr);
if (scale == 0.0)
{
CPLError(CE_Failure, CPLE_IllegalArg,
"Invalid Rsets.scale value");
throw CE_Failure;
}

if (static_cast<int>(scale) != 2 &&
(EQUALN("Avg", pszResampling, 3) ||
Expand Down
3 changes: 3 additions & 0 deletions frmts/mrf/mrf_band.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ static int ZPack(const buf_mgr &src, buf_mgr &dst, int flags)

err = deflateInit2(&stream, level, Z_DEFLATED, wb, memlevel, strategy);
if (err != Z_OK)
{
deflateEnd(&stream);
return err;
}

err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END)
Expand Down
Loading
Loading