Skip to content

Commit 493ed18

Browse files
authoredJan 13, 2025
Merge pull request OSGeo#11643 from rouault/coverity_fixes
Address medium and high severity new Coverity fixes
2 parents 11ff175 + cb061ec commit 493ed18

25 files changed

+195
-141
lines changed
 

Diff for: ‎alg/gdalwarpkernel.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -4383,7 +4383,9 @@ static void GWKComputeWeights(GDALResampleAlg eResample, int iMin, int iMax,
43834383

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

44054407
int j = jMin; // Used after for.
44064408
int jC = 0; // Used after for.
4407-
double dfAccumulatorWeightVertical = 0.0;
4409+
// Not zero, but as close as possible to it, to avoid potential division by
4410+
// zero at end of function
4411+
double dfAccumulatorWeightVertical = std::numeric_limits<double>::min();
44084412
for (; j + 2 < jMax; j += 4, jC += 4)
44094413
{
44104414
padfWeightsVertical[jC] = (j - dfDeltaY) * dfYScale;

Diff for: ‎apps/gdal_rasterize_lib.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,20 @@ static GDALDatasetH CreateOutputDataset(
842842
sEnvelop.MaxY = ceil(sEnvelop.MaxY / dfYRes) * dfYRes;
843843
}
844844

845+
if (dfXRes == 0 || dfYRes == 0)
846+
{
847+
CPLError(CE_Failure, CPLE_AppDefined, "Could not determine bounds");
848+
return nullptr;
849+
}
850+
845851
double adfProjection[6] = {sEnvelop.MinX, dfXRes, 0.0,
846852
sEnvelop.MaxY, 0.0, -dfYRes};
847853

848854
if (nXSize == 0 && nYSize == 0)
849855
{
856+
// coverity[divide_by_zero]
850857
const double dfXSize = 0.5 + (sEnvelop.MaxX - sEnvelop.MinX) / dfXRes;
858+
// coverity[divide_by_zero]
851859
const double dfYSize = 0.5 + (sEnvelop.MaxY - sEnvelop.MinY) / dfYRes;
852860
if (dfXSize > std::numeric_limits<int>::max() ||
853861
dfXSize < std::numeric_limits<int>::min() ||

Diff for: ‎apps/gdal_translate_lib.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
12771277
const bool bOutsizeExplicitlySet =
12781278
!(psOptions->nOXSizePixel == 0 && psOptions->dfOXSizePct == 0.0 &&
12791279
psOptions->nOYSizePixel == 0 && psOptions->dfOYSizePct == 0.0);
1280-
if (psOptions->dfXRes != 0.0)
1280+
if (psOptions->dfXRes != 0.0 && psOptions->dfYRes != 0.0)
12811281
{
12821282
if (!(bHasSrcGeoTransform && psOptions->nGCPCount == 0 &&
12831283
adfSrcGeoTransform[2] == 0.0 && adfSrcGeoTransform[4] == 0.0))

Diff for: ‎apps/gdaladdo.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ static bool PartialRefreshFromSourceExtent(
481481
}
482482
double dfNextCurPixels =
483483
dfCurPixels + static_cast<double>(region.nXSize) * region.nYSize;
484+
// coverity[divide_by_zero]
484485
void *pScaledProgress = GDALCreateScaledProgress(
485486
dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
486487
pfnProgress, pProgressArg);

Diff for: ‎apps/gdalbuildvrt_lib.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ static int GetSrcDstWin(DatasetProperty *psDP, double we_res, double ns_res,
127127
double *pdfDstYOff, double *pdfDstXSize,
128128
double *pdfDstYSize)
129129
{
130+
if (we_res == 0 || ns_res == 0)
131+
{
132+
// should not happen. to please Coverity
133+
return FALSE;
134+
}
135+
130136
/* Check that the destination bounding box intersects the source bounding
131137
* box */
132138
if (psDP->adfGeoTransform[GEOTRSFRM_TOPLEFT_X] +

Diff for: ‎apps/ogrlineref.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,13 @@ static OGRErr GetPosition(OGRLayer *const poPkLayer, double dfX, double dfY,
10931093
// Get real distance
10941094
const double dfRealDist = Project(pCloserPart, &pt);
10951095
delete pCloserPart;
1096+
if (dfScale == 0)
1097+
{
1098+
fprintf(stderr, _("dfScale == 0.\n"));
1099+
return OGRERR_FAILURE;
1100+
}
10961101
// Compute reference distance
1102+
// coverity[divide_by_zero]
10971103
const double dfRefDist = dfBeg + dfRealDist / dfScale;
10981104
if (bQuiet)
10991105
{

Diff for: ‎frmts/envisat/EnvisatFile.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -1807,16 +1807,20 @@ int S_NameValueList_Parse(const char *text, int text_offset, int *entry_count,
18071807
/*
18081808
* Add the entry to the name/value list.
18091809
*/
1810-
(*entry_count)++;
1811-
*entries = (EnvisatNameValue **)CPLRealloc(
1812-
*entries, *entry_count * sizeof(EnvisatNameValue *));
1813-
1814-
if (*entries == NULL)
1810+
EnvisatNameValue **newEntries = VSI_REALLOC_VERBOSE(
1811+
*entries, (*entry_count + 1) * sizeof(EnvisatNameValue *));
1812+
if (!newEntries)
18151813
{
18161814
*entry_count = 0;
1815+
CPLFree(entry->key);
1816+
CPLFree(entry->value);
1817+
CPLFree(entry->literal_line);
1818+
CPLFree(entry->units);
18171819
CPLFree(entry);
18181820
return FAILURE;
18191821
}
1822+
(*entry_count)++;
1823+
*entries = newEntries;
18201824

18211825
(*entries)[*entry_count - 1] = entry;
18221826
}

Diff for: ‎frmts/gtiff/libgeotiff/geotiff_proj4.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ int GTIFSetFromProj4( GTIF *gtif, const char *proj4 )
221221
dfSemiMajor = OSR_GDV(papszNV,"a",0.0);
222222
dfSemiMinor = OSR_GDV(papszNV,"b",0.0);
223223
dfInvFlattening = OSR_GDV(papszNV,"rf",0.0);
224-
if( dfSemiMinor != 0.0 && dfInvFlattening == 0.0 )
224+
if( dfSemiMajor != 0.0 && dfSemiMinor != 0.0 && dfInvFlattening == 0.0 )
225225
dfInvFlattening = -1.0 / (dfSemiMinor/dfSemiMajor - 1.0);
226226
}
227227

Diff for: ‎frmts/gxf/gxf_ogcwkt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void OGCWKTSetProj(char *pszProjection, size_t nProjectionSize,
247247
char *GXFGetMapProjectionAsOGCWKT(GXFHandle hGXF)
248248

249249
{
250-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
250+
GXFInfo_t *psGXF = hGXF;
251251
char **papszMethods = NULL;
252252
char szWKT[1024 + 32];
253253
char szGCS[512];

Diff for: ‎frmts/gxf/gxf_proj4.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
char *GXFGetMapProjectionAsPROJ4(GXFHandle hGXF)
5555

5656
{
57-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
57+
GXFInfo_t *psGXF = hGXF;
5858
char **papszMethods = NULL;
5959
char szPROJ4[512] = {0};
6060

@@ -563,7 +563,7 @@ CPLErr GXFGetPROJ4Position(GXFHandle hGXF, double *pdfXOrigin,
563563
double *pdfYPixelSize, double *pdfRotation)
564564

565565
{
566-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
566+
GXFInfo_t *psGXF = hGXF;
567567
char *pszProj;
568568

569569
/* -------------------------------------------------------------------- */

Diff for: ‎frmts/gxf/gxfopen.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ GXFHandle GXFOpen(const char *pszFilename)
412412
void GXFClose(GXFHandle hGXF)
413413

414414
{
415-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
415+
GXFInfo_t *psGXF = hGXF;
416416

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

635635
{
636-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
636+
GXFInfo_t *psGXF = hGXF;
637637
CPLErr nErr;
638638
int iRawScanline;
639639

@@ -698,7 +698,7 @@ CPLErr GXFGetScanline(GXFHandle hGXF, int iScanline, double *padfLineBuf)
698698
CPLErr GXFGetRawScanline(GXFHandle hGXF, int iScanline, double *padfLineBuf)
699699

700700
{
701-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
701+
GXFInfo_t *psGXF = hGXF;
702702
CPLErr eErr;
703703

704704
/* -------------------------------------------------------------------- */
@@ -754,7 +754,7 @@ CPLErr GXFGetRawScanline(GXFHandle hGXF, int iScanline, double *padfLineBuf)
754754
static void GXFScanForZMinMax(GXFHandle hGXF)
755755

756756
{
757-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
757+
GXFInfo_t *psGXF = hGXF;
758758
int iLine, iPixel;
759759
double *padfScanline;
760760

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

843843
{
844-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
844+
GXFInfo_t *psGXF = hGXF;
845845

846846
if (pnXSize != NULL)
847847
*pnXSize = psGXF->nRawXSize;
@@ -889,7 +889,7 @@ CPLErr GXFGetRawInfo(GXFHandle hGXF, int *pnXSize, int *pnYSize, int *pnSense,
889889
char **GXFGetMapProjection(GXFHandle hGXF)
890890

891891
{
892-
return (((GXFInfo_t *)hGXF)->papszMapProjection);
892+
return ((hGXF)->papszMapProjection);
893893
}
894894

895895
/************************************************************************/
@@ -911,7 +911,7 @@ char **GXFGetMapProjection(GXFHandle hGXF)
911911
char **GXFGetMapDatumTransform(GXFHandle hGXF)
912912

913913
{
914-
return (((GXFInfo_t *)hGXF)->papszMapDatumTransform);
914+
return ((hGXF)->papszMapDatumTransform);
915915
}
916916

917917
/************************************************************************/
@@ -948,7 +948,7 @@ CPLErr GXFGetRawPosition(GXFHandle hGXF, double *pdfXOrigin, double *pdfYOrigin,
948948
double *pdfRotation)
949949

950950
{
951-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
951+
GXFInfo_t *psGXF = hGXF;
952952

953953
if (pdfXOrigin != NULL)
954954
*pdfXOrigin = psGXF->dfXOrigin;
@@ -1004,7 +1004,7 @@ CPLErr GXFGetPosition(GXFHandle hGXF, double *pdfXOrigin, double *pdfYOrigin,
10041004
double *pdfRotation)
10051005

10061006
{
1007-
GXFInfo_t *psGXF = (GXFInfo_t *)hGXF;
1007+
GXFInfo_t *psGXF = hGXF;
10081008
double dfCXOrigin, dfCYOrigin, dfCXPixelSize, dfCYPixelSize;
10091009

10101010
switch (psGXF->nSense)

Diff for: ‎frmts/gxf/gxfopen.h

+41-40
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,49 @@
2626
#include "cpl_conv.h"
2727
#include "cpl_string.h"
2828

29+
/* -------------------------------------------------------------------- */
30+
/* This is consider to be a private structure. */
31+
/* -------------------------------------------------------------------- */
32+
struct GXFInfo_t
33+
{
34+
VSILFILE *fp;
35+
36+
int nRawXSize;
37+
int nRawYSize;
38+
int nSense; /* GXFS_ codes */
39+
int nGType; /* 0 is uncompressed */
40+
41+
double dfXPixelSize;
42+
double dfYPixelSize;
43+
double dfRotation;
44+
double dfXOrigin; /* lower left corner */
45+
double dfYOrigin; /* lower left corner */
46+
47+
char szDummy[64];
48+
double dfSetDummyTo;
49+
50+
char *pszTitle;
51+
52+
double dfTransformScale;
53+
double dfTransformOffset;
54+
char *pszTransformName;
55+
56+
char **papszMapProjection;
57+
char **papszMapDatumTransform;
58+
59+
char *pszUnitName;
60+
double dfUnitToMeter;
61+
62+
double dfZMaximum;
63+
double dfZMinimum;
64+
65+
vsi_l_offset *panRawLineOffset;
66+
};
67+
typedef struct GXFInfo_t GXFInfo_t;
68+
2969
CPL_C_START
3070

31-
typedef void *GXFHandle;
71+
typedef struct GXFInfo_t *GXFHandle;
3272

3373
GXFHandle GXFOpen(const char *pszFilename);
3474

@@ -65,43 +105,4 @@ void GXFClose(GXFHandle hGXF);
65105

66106
CPL_C_END
67107

68-
/* -------------------------------------------------------------------- */
69-
/* This is consider to be a private structure. */
70-
/* -------------------------------------------------------------------- */
71-
typedef struct
72-
{
73-
VSILFILE *fp;
74-
75-
int nRawXSize;
76-
int nRawYSize;
77-
int nSense; /* GXFS_ codes */
78-
int nGType; /* 0 is uncompressed */
79-
80-
double dfXPixelSize;
81-
double dfYPixelSize;
82-
double dfRotation;
83-
double dfXOrigin; /* lower left corner */
84-
double dfYOrigin; /* lower left corner */
85-
86-
char szDummy[64];
87-
double dfSetDummyTo;
88-
89-
char *pszTitle;
90-
91-
double dfTransformScale;
92-
double dfTransformOffset;
93-
char *pszTransformName;
94-
95-
char **papszMapProjection;
96-
char **papszMapDatumTransform;
97-
98-
char *pszUnitName;
99-
double dfUnitToMeter;
100-
101-
double dfZMaximum;
102-
double dfZMinimum;
103-
104-
vsi_l_offset *panRawLineOffset;
105-
} GXFInfo_t;
106-
107108
#endif /* ndef GXFOPEN_H_INCLUDED */

Diff for: ‎frmts/ilwis/ilwisdataset.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,9 @@ int ValueRange::iRaw(double rValueIn) const
21532153
{
21542154
if (rValueIn == rUNDEF) // || !fContains(rValue))
21552155
return iUNDEF;
2156-
const double rEpsilon = _rStep == 0.0 ? 1e-6 : _rStep / 3.0;
2156+
if (_rStep == 0.0)
2157+
return iUNDEF;
2158+
const double rEpsilon = _rStep / 3.0;
21572159
if (rValueIn - get_rLo() < -rEpsilon) // take a little rounding tolerance
21582160
return iUNDEF;
21592161
else if (rValueIn - get_rHi() >

Diff for: ‎frmts/mrf/marfa_dataset.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ CPLErr MRFDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
316316
config, "Rsets.scale",
317317
CPLOPrintf("%d", panOverviewList[0]).c_str()),
318318
nullptr);
319+
if (scale == 0.0)
320+
{
321+
CPLError(CE_Failure, CPLE_IllegalArg,
322+
"Invalid Rsets.scale value");
323+
throw CE_Failure;
324+
}
319325

320326
if (static_cast<int>(scale) != 2 &&
321327
(EQUALN("Avg", pszResampling, 3) ||

Diff for: ‎frmts/mrf/mrf_band.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ static int ZPack(const buf_mgr &src, buf_mgr &dst, int flags)
208208

209209
err = deflateInit2(&stream, level, Z_DEFLATED, wb, memlevel, strategy);
210210
if (err != Z_OK)
211+
{
212+
deflateEnd(&stream);
211213
return err;
214+
}
212215

213216
err = deflate(&stream, Z_FINISH);
214217
if (err != Z_STREAM_END)

0 commit comments

Comments
 (0)
Failed to load comments.