diff --git a/alg/gdalwarpkernel.cpp b/alg/gdalwarpkernel.cpp index 37aedda5ff4d..e5ce23d4b43d 100644 --- a/alg/gdalwarpkernel.cpp +++ b/alg/gdalwarpkernel.cpp @@ -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::min(); for (; i + 2 < iMax; i += 4, iC += 4) { padfWeightsHorizontal[iC] = (i - dfDeltaX) * dfXScale; @@ -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::min(); for (; j + 2 < jMax; j += 4, jC += 4) { padfWeightsVertical[jC] = (j - dfDeltaY) * dfYScale; diff --git a/apps/gdal_rasterize_lib.cpp b/apps/gdal_rasterize_lib.cpp index cc247a71a596..8f59fa6fdb0b 100644 --- a/apps/gdal_rasterize_lib.cpp +++ b/apps/gdal_rasterize_lib.cpp @@ -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::max() || dfXSize < std::numeric_limits::min() || diff --git a/apps/gdal_translate_lib.cpp b/apps/gdal_translate_lib.cpp index 133d87617913..1f192cc7d493 100644 --- a/apps/gdal_translate_lib.cpp +++ b/apps/gdal_translate_lib.cpp @@ -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)) diff --git a/apps/gdaladdo.cpp b/apps/gdaladdo.cpp index 54ba6e093e6f..640574ee0b9c 100644 --- a/apps/gdaladdo.cpp +++ b/apps/gdaladdo.cpp @@ -481,6 +481,7 @@ static bool PartialRefreshFromSourceExtent( } double dfNextCurPixels = dfCurPixels + static_cast(region.nXSize) * region.nYSize; + // coverity[divide_by_zero] void *pScaledProgress = GDALCreateScaledProgress( dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels, pfnProgress, pProgressArg); diff --git a/apps/gdalbuildvrt_lib.cpp b/apps/gdalbuildvrt_lib.cpp index 296e376a73cf..309da017eb6e 100644 --- a/apps/gdalbuildvrt_lib.cpp +++ b/apps/gdalbuildvrt_lib.cpp @@ -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] + diff --git a/apps/ogrlineref.cpp b/apps/ogrlineref.cpp index ea05c4504b26..91a1274ab477 100644 --- a/apps/ogrlineref.cpp +++ b/apps/ogrlineref.cpp @@ -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) { diff --git a/frmts/envisat/EnvisatFile.c b/frmts/envisat/EnvisatFile.c index cac22675b3fb..83b6273d88e4 100644 --- a/frmts/envisat/EnvisatFile.c +++ b/frmts/envisat/EnvisatFile.c @@ -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; } diff --git a/frmts/gtiff/libgeotiff/geotiff_proj4.c b/frmts/gtiff/libgeotiff/geotiff_proj4.c index 2f6665de1bfa..2cb3bd8dd94e 100644 --- a/frmts/gtiff/libgeotiff/geotiff_proj4.c +++ b/frmts/gtiff/libgeotiff/geotiff_proj4.c @@ -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); } diff --git a/frmts/gxf/gxf_ogcwkt.c b/frmts/gxf/gxf_ogcwkt.c index d5f66dd770df..541175e6001a 100644 --- a/frmts/gxf/gxf_ogcwkt.c +++ b/frmts/gxf/gxf_ogcwkt.c @@ -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]; diff --git a/frmts/gxf/gxf_proj4.c b/frmts/gxf/gxf_proj4.c index dfa3de3b1436..74615e41b7a9 100644 --- a/frmts/gxf/gxf_proj4.c +++ b/frmts/gxf/gxf_proj4.c @@ -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}; @@ -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; /* -------------------------------------------------------------------- */ diff --git a/frmts/gxf/gxfopen.c b/frmts/gxf/gxfopen.c index bac9178bb54b..cd8f4793ea06 100644 --- a/frmts/gxf/gxfopen.c +++ b/frmts/gxf/gxfopen.c @@ -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); @@ -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; @@ -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; /* -------------------------------------------------------------------- */ @@ -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; @@ -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; @@ -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); } /************************************************************************/ @@ -911,7 +911,7 @@ char **GXFGetMapProjection(GXFHandle hGXF) char **GXFGetMapDatumTransform(GXFHandle hGXF) { - return (((GXFInfo_t *)hGXF)->papszMapDatumTransform); + return ((hGXF)->papszMapDatumTransform); } /************************************************************************/ @@ -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; @@ -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) diff --git a/frmts/gxf/gxfopen.h b/frmts/gxf/gxfopen.h index da485374df09..cc92a11d7525 100644 --- a/frmts/gxf/gxfopen.h +++ b/frmts/gxf/gxfopen.h @@ -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); @@ -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 */ diff --git a/frmts/ilwis/ilwisdataset.cpp b/frmts/ilwis/ilwisdataset.cpp index d404a9d914ff..60ac1a4c1853 100644 --- a/frmts/ilwis/ilwisdataset.cpp +++ b/frmts/ilwis/ilwisdataset.cpp @@ -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() > diff --git a/frmts/mrf/marfa_dataset.cpp b/frmts/mrf/marfa_dataset.cpp index 6af454ae1702..0a1596f96505 100644 --- a/frmts/mrf/marfa_dataset.cpp +++ b/frmts/mrf/marfa_dataset.cpp @@ -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(scale) != 2 && (EQUALN("Avg", pszResampling, 3) || diff --git a/frmts/mrf/mrf_band.cpp b/frmts/mrf/mrf_band.cpp index cdc51d9be3ef..2d11ce5fc9dc 100644 --- a/frmts/mrf/mrf_band.cpp +++ b/frmts/mrf/mrf_band.cpp @@ -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) diff --git a/frmts/ogcapi/gdalogcapidataset.cpp b/frmts/ogcapi/gdalogcapidataset.cpp index d532c5476325..66b141613cff 100644 --- a/frmts/ogcapi/gdalogcapidataset.cpp +++ b/frmts/ogcapi/gdalogcapidataset.cpp @@ -80,7 +80,7 @@ class OGCAPIDataset final : public GDALDataset bool InitFromFile(GDALOpenInfo *poOpenInfo); bool InitFromURL(GDALOpenInfo *poOpenInfo); - void ProcessScale(const CPLJSONObject &oScaleDenominator, + bool ProcessScale(const CPLJSONObject &oScaleDenominator, const double dfXMin, const double dfYMin, const double dfXMax, const double dfYMax); bool InitFromCollection(GDALOpenInfo *poOpenInfo, CPLJSONDocument &oDoc); @@ -753,7 +753,7 @@ bool OGCAPIDataset::InitFromFile(GDALOpenInfo *poOpenInfo) /* ProcessScale() */ /************************************************************************/ -void OGCAPIDataset::ProcessScale(const CPLJSONObject &oScaleDenominator, +bool OGCAPIDataset::ProcessScale(const CPLJSONObject &oScaleDenominator, const double dfXMin, const double dfYMin, const double dfXMax, const double dfYMax) @@ -765,6 +765,8 @@ void OGCAPIDataset::ProcessScale(const CPLJSONObject &oScaleDenominator, constexpr double HALF_CIRCUMFERENCE = 6378137 * M_PI; dfRes = dfScaleDenominator / ((HALF_CIRCUMFERENCE / 180) / 0.28e-3); } + if (dfRes == 0.0) + return false; double dfXSize = (dfXMax - dfXMin) / dfRes; double dfYSize = (dfYMax - dfYMin) / dfRes; @@ -780,6 +782,8 @@ void OGCAPIDataset::ProcessScale(const CPLJSONObject &oScaleDenominator, m_adfGeoTransform[1] = (dfXMax - dfXMin) / nRasterXSize; m_adfGeoTransform[3] = dfYMax; m_adfGeoTransform[5] = -(dfYMax - dfYMin) / nRasterYSize; + + return true; } /************************************************************************/ @@ -831,7 +835,8 @@ bool OGCAPIDataset::InitFromCollection(GDALOpenInfo *poOpenInfo, auto oScaleDenominator = oRoot["scaleDenominator"]; - ProcessScale(oScaleDenominator, dfXMin, dfYMin, dfXMax, dfYMax); + if (!ProcessScale(oScaleDenominator, dfXMin, dfYMin, dfXMax, dfYMax)) + return false; bool bFoundMap = false; diff --git a/frmts/stacit/stacitdataset.cpp b/frmts/stacit/stacitdataset.cpp index 67890135c26f..993b67ca3dd8 100644 --- a/frmts/stacit/stacitdataset.cpp +++ b/frmts/stacit/stacitdataset.cpp @@ -458,6 +458,12 @@ bool STACITDataset::SetupDataset( } // Set raster size + if (dfXRes == 0 || dfYRes == 0) + { + CPLError(CE_Failure, CPLE_AppDefined, + "Invalid computed dataset dimensions"); + return false; + } double dfXSize = std::round((dfXMax - dfXMin) / dfXRes); double dfYSize = std::round((dfYMax - dfYMin) / dfYRes); if (dfXSize <= 0 || dfYSize <= 0 || dfXSize > INT_MAX || dfYSize > INT_MAX) diff --git a/frmts/usgsdem/usgsdem_create.cpp b/frmts/usgsdem/usgsdem_create.cpp index 91b865f991f5..8083dbcda336 100644 --- a/frmts/usgsdem/usgsdem_create.cpp +++ b/frmts/usgsdem/usgsdem_create.cpp @@ -1390,6 +1390,7 @@ GDALDataset *USGSDEMCreateCopy(const char *pszFilename, GDALDataset *poSrcDS, { CPLError(CE_Failure, CPLE_AppDefined, "Source dataset dimensions must be at least 2x2."); + USGSDEMWriteCleanup(&sWInfo); return nullptr; } diff --git a/frmts/vrt/pixelfunctions.cpp b/frmts/vrt/pixelfunctions.cpp index e7a4bc519b65..72e028719691 100644 --- a/frmts/vrt/pixelfunctions.cpp +++ b/frmts/vrt/pixelfunctions.cpp @@ -766,6 +766,7 @@ static CPLErr DivPixelFunc(void **papoSources, int nSources, void *pData, for (int iCol = 0; iCol < nXSize; ++iCol, ++ii) { const double dfVal = GetSrcVal(papoSources[1], eSrcType, ii); + // coverity[divide_by_zero] double dfPixVal = dfVal == 0 ? std::numeric_limits::infinity() @@ -914,6 +915,7 @@ static CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, // Source raster pixels may be obtained with GetSrcVal macro. // Not complex. const double dfVal = GetSrcVal(papoSources[0], eSrcType, ii); + // coverity[divide_by_zero] const double dfPixVal = dfVal == 0 ? std::numeric_limits::infinity() : dfK / dfVal; @@ -1482,6 +1484,7 @@ static CPLErr NormDiffPixelFunc(void **papoSources, int nSources, void *pData, const double dfDenom = (dfLeftVal + dfRightVal); + // coverity[divide_by_zero] const double dfPixVal = dfDenom == 0 ? std::numeric_limits::infinity() : (dfLeftVal - dfRightVal) / dfDenom; diff --git a/frmts/vrt/vrtdataset.cpp b/frmts/vrt/vrtdataset.cpp index 149ba251c83b..2f3244f89e07 100644 --- a/frmts/vrt/vrtdataset.cpp +++ b/frmts/vrt/vrtdataset.cpp @@ -1064,7 +1064,7 @@ GDALDataset *VRTDataset::OpenVRTProtocol(const char *pszSpec) if (nSubdatasets > 0) { bool bFound = false; - for (int j = 0; j < nSubdatasets; j += 2) + for (int j = 0; j < nSubdatasets && papszSubdatasets[j]; j += 2) { const std::string osSubdatasetSource( strstr(papszSubdatasets[j], "=") + 1); diff --git a/gcore/gdaldefaultoverviews.cpp b/gcore/gdaldefaultoverviews.cpp index 3860f0d32a6f..d0dd813201cf 100644 --- a/gcore/gdaldefaultoverviews.cpp +++ b/gcore/gdaldefaultoverviews.cpp @@ -830,9 +830,16 @@ CPLErr GDALDefaultOverviews::BuildOverviews( 0, (HaveMaskFile() && poMaskDS) ? double(nBands) / (nBands + 1) : 1, pfnProgress, pProgressData); + const auto AvoidZero = [](double x) + { + if (x == 0) + return 1.0; + return x; + }; + void *pScaledProgress = GDALCreateScaledProgress( - 0, dfAreaNewOverviews / dfAreaRefreshedOverviews, GDALScaledProgress, - pScaledOverviewWithoutMask); + 0, dfAreaNewOverviews / AvoidZero(dfAreaRefreshedOverviews), + GDALScaledProgress, pScaledOverviewWithoutMask); if (bOvrIsAux) { #ifdef NO_HFA_SUPPORT @@ -973,7 +980,7 @@ CPLErr GDALDefaultOverviews::BuildOverviews( if (nNewOverviews > 0) { const double dfOffset = - dfAreaNewOverviews / dfAreaRefreshedOverviews; + dfAreaNewOverviews / AvoidZero(dfAreaRefreshedOverviews); const double dfScale = 1.0 - dfOffset; pScaledProgress = GDALCreateScaledProgress( dfOffset + dfScale * iBand / nBands, diff --git a/gcore/gdalexif.cpp b/gcore/gdalexif.cpp index e4dd9fef210e..872f1f44a4f7 100644 --- a/gcore/gdalexif.cpp +++ b/gcore/gdalexif.cpp @@ -974,23 +974,32 @@ static GByte *ParseUndefined(const char *pszVal, GUInt32 *pnLength) } /************************************************************************/ -/* EXIFTagSort() */ +/* TagValue */ /************************************************************************/ struct TagValue { - GUInt16 tag; - GDALEXIFTIFFDataType datatype; - GByte *pabyVal; - GUInt32 nLength; - GUInt32 nLengthBytes; - int nRelOffset; -}; + GUInt16 tag = 0; + GDALEXIFTIFFDataType datatype = TIFF_NOTYPE; + std::unique_ptr pabyVal{}; + GUInt32 nLength = 0; + GUInt32 nLengthBytes = 0; + int nRelOffset = 0; -static bool EXIFTagSort(const TagValue &a, const TagValue &b) -{ - return a.tag <= b.tag; -} + TagValue() = default; + + TagValue(TagValue &&) = default; + TagValue &operator=(TagValue &&) = default; + + bool operator<(const TagValue &other) const + { + return tag < other.tag; + } + + private: + TagValue(const TagValue &) = delete; + TagValue &operator=(const TagValue &) = delete; +}; /************************************************************************/ /* GetNumDenomFromDouble() */ @@ -1149,9 +1158,6 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, TagValue tag; tag.tag = tagdescArray[i].tag; tag.datatype = tagdescArray[i].datatype; - tag.pabyVal = nullptr; - tag.nLength = 0; - tag.nLengthBytes = 0; tag.nRelOffset = -1; if (tag.datatype == TIFF_ASCII) @@ -1159,8 +1165,8 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, if (tagdescArray[i].length == 0 || strlen(pszValue) + 1 == tagdescArray[i].length) { - tag.pabyVal = - reinterpret_cast(CPLStrdup(pszValue)); + tag.pabyVal.reset( + reinterpret_cast(CPLStrdup(pszValue))); tag.nLength = 1 + static_cast(strlen(pszValue)); } else if (strlen(pszValue) >= tagdescArray[i].length) @@ -1168,20 +1174,20 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, CPLError(CE_Warning, CPLE_AppDefined, "Value of %s will be truncated", tagdescArray[i].name); - tag.pabyVal = reinterpret_cast( - CPLMalloc(tagdescArray[i].length)); - memcpy(tag.pabyVal, pszValue, tagdescArray[i].length); + tag.pabyVal.reset(reinterpret_cast( + CPLMalloc(tagdescArray[i].length))); + memcpy(tag.pabyVal.get(), pszValue, tagdescArray[i].length); tag.nLength = tagdescArray[i].length; - tag.pabyVal[tag.nLength - 1] = '\0'; + (tag.pabyVal.get())[tag.nLength - 1] = '\0'; } else { - tag.pabyVal = reinterpret_cast( - CPLMalloc(tagdescArray[i].length)); - memset(tag.pabyVal, ' ', tagdescArray[i].length); - memcpy(tag.pabyVal, pszValue, strlen(pszValue)); + tag.pabyVal.reset(reinterpret_cast( + CPLMalloc(tagdescArray[i].length))); + memset(tag.pabyVal.get(), ' ', tagdescArray[i].length); + memcpy(tag.pabyVal.get(), pszValue, strlen(pszValue)); tag.nLength = tagdescArray[i].length; - tag.pabyVal[tag.nLength - 1] = '\0'; + (tag.pabyVal.get())[tag.nLength - 1] = '\0'; } tag.nLengthBytes = tag.nLength; } @@ -1189,7 +1195,8 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, tag.datatype == TIFF_UNDEFINED) { GUInt32 nValLength = 0; - GByte *pabyVal = ParseUndefined(pszValue, &nValLength); + std::unique_ptr pabyVal( + ParseUndefined(pszValue, &nValLength)); if (tagdescArray[i].length == 0 || nValLength == tagdescArray[i].length) { @@ -1197,20 +1204,19 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, strncmp(pszValue, "0x", 2) != 0) // EXIF_UserComment { const char *pszRealVal = - reinterpret_cast(pabyVal); + reinterpret_cast(pabyVal.get()); const int nValueLen = static_cast(strlen(pszRealVal)); // 8 first bytes are the character code // Set them to 0 to mean undefined - tag.pabyVal = - static_cast(CPLCalloc(1, 8 + nValueLen)); + tag.pabyVal.reset( + static_cast(CPLCalloc(1, 8 + nValueLen))); tag.nLength = 8 + nValueLen; - memcpy(tag.pabyVal + 8, pszRealVal, nValueLen); - CPLFree(pabyVal); + memcpy(tag.pabyVal.get() + 8, pszRealVal, nValueLen); } else { - tag.pabyVal = pabyVal; + tag.pabyVal = std::move(pabyVal); tag.nLength = nValLength; } } @@ -1219,14 +1225,14 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, CPLError(CE_Warning, CPLE_AppDefined, "Value of %s will be truncated", tagdescArray[i].name); - tag.pabyVal = pabyVal; + tag.pabyVal = std::move(pabyVal); tag.nLength = tagdescArray[i].length; } else { - tag.pabyVal = reinterpret_cast( - CPLRealloc(pabyVal, tagdescArray[i].length)); - memset(tag.pabyVal + nValLength, '\0', + tag.pabyVal.reset(reinterpret_cast( + CPLRealloc(pabyVal.release(), tagdescArray[i].length))); + memset(tag.pabyVal.get() + nValLength, '\0', tagdescArray[i].length - nValLength); tag.nLength = tagdescArray[i].length; } @@ -1260,18 +1266,18 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, tag.nLength = (tagdescArray[i].length == 0) ? nTokens : tagdescArray[i].length; - tag.pabyVal = reinterpret_cast(CPLCalloc( - 1, cpl::fits_on(nDataTypeSize * tag.nLength))); + tag.pabyVal.reset(reinterpret_cast(CPLCalloc( + 1, cpl::fits_on(nDataTypeSize * tag.nLength)))); GUInt32 nOffset = 0; for (GUInt32 j = 0; j < std::min(nTokens, tag.nLength); j++) { GUInt32 nVal = atoi(papszTokens[j]); if (tag.datatype == TIFF_SHORT) - WriteLEUInt16(tag.pabyVal, nOffset, + WriteLEUInt16(tag.pabyVal.get(), nOffset, static_cast(nVal)); else - WriteLEUInt32(tag.pabyVal, nOffset, nVal); + WriteLEUInt32(tag.pabyVal.get(), nOffset, nVal); } CSLDestroy(papszTokens); @@ -1305,8 +1311,8 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, tag.nLength = (tagdescArray[i].length == 0) ? nTokens : tagdescArray[i].length; - tag.pabyVal = reinterpret_cast( - CPLCalloc(1, nDataTypeSize * tag.nLength)); + tag.pabyVal.reset(reinterpret_cast( + CPLCalloc(1, nDataTypeSize * tag.nLength))); GUInt32 nOffset = 0; for (GUInt32 j = 0; j < std::min(nTokens, tag.nLength); j++) @@ -1324,8 +1330,8 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, tagdescArray[i].name); } - WriteLEUInt32(tag.pabyVal, nOffset, nNum); - WriteLEUInt32(tag.pabyVal, nOffset, nDenom); + WriteLEUInt32(tag.pabyVal.get(), nOffset, nNum); + WriteLEUInt32(tag.pabyVal.get(), nOffset, nDenom); } CSLDestroy(papszTokens); @@ -1346,14 +1352,14 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, tag.nRelOffset = nRelOffset; nRelOffset += tag.nLengthBytes + (tag.nLengthBytes % 1); } - tags.push_back(tag); + tags.push_back(std::move(tag)); } } CPLFree(pszKey); } // Sort tags by ascending order - std::sort(tags.begin(), tags.end(), EXIFTagSort); + std::sort(tags.begin(), tags.end()); #ifdef notdef if (location == EXIF_IFD && @@ -1363,11 +1369,11 @@ static std::vector EXIFFormatTagValue(char **papszEXIFMetadata, TagValue tag; tag.tag = EXIF_VERSION; tag.datatype = TIFF_UNDEFINED; - tag.pabyVal = reinterpret_cast(CPLStrdup("0231")); + tag.pabyVal.reset(reinterpret_cast(CPLStrdup("0231"))); tag.nLength = 4; tag.nLengthBytes = 4; tag.nRelOffset = -1; - tags.push_back(tag); + tags.push_back(std::move(tag)); } #endif @@ -1404,30 +1410,18 @@ static void WriteTags(GByte *pabyData, GUInt32 &nBufferOff, if (tag.nRelOffset < 0) { CPLAssert(tag.nLengthBytes <= 4); - memcpy(pabyData + nBufferOff, tag.pabyVal, tag.nLengthBytes); + memcpy(pabyData + nBufferOff, tag.pabyVal.get(), tag.nLengthBytes); nBufferOff += 4; } else { WriteLEUInt32(pabyData, nBufferOff, tag.nRelOffset + offsetIFDData); memcpy(pabyData + EXIF_HEADER_SIZE + tag.nRelOffset + offsetIFDData, - tag.pabyVal, tag.nLengthBytes); + tag.pabyVal.get(), tag.nLengthBytes); } } } -/************************************************************************/ -/* FreeTags() */ -/************************************************************************/ - -static void FreeTags(std::vector &tags) -{ - for (auto &tag : tags) - { - CPLFree(tag.pabyVal); - } -} - /************************************************************************/ /* EXIFCreate() */ /************************************************************************/ @@ -1516,9 +1510,6 @@ GByte *EXIFCreate(char **papszEXIFMetadata, GByte *pabyThumbnail, } if (pabyData == nullptr) { - FreeTags(mainTags); - FreeTags(exifTags); - FreeTags(gpsTags); return nullptr; } @@ -1640,10 +1631,6 @@ GByte *EXIFCreate(char **papszEXIFMetadata, GByte *pabyThumbnail, if (pabyThumbnail != nullptr && nThumbnailSize) memcpy(pabyData + nBufferOff, pabyThumbnail, nThumbnailSize); - FreeTags(mainTags); - FreeTags(exifTags); - FreeTags(gpsTags); - *pnOutBufferSize = nBufferSize; return pabyData; } diff --git a/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp b/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp index 5f289d8c6da5..b4485c61a137 100644 --- a/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp +++ b/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp @@ -7422,13 +7422,13 @@ OGRErr OGRGeoPackageTableLayer::AlterGeomFieldDefn( const int nNewSRID = m_poDS->GetSrsId(poNewSRS.get()); // Replace the old SRID by the new ones in geometry blobs - uint32_t nNewSRID_LSB = nNewSRID; + int32_t nNewSRID_LSB = nNewSRID; CPL_LSBPTR32(&nNewSRID_LSB); GByte abySRID_LSB[5] = {0, 0, 0, 0}; memcpy(abySRID_LSB, &nNewSRID_LSB, 4); char *pszSRID_LSB_HEX = CPLBinaryToHex(4, abySRID_LSB); - uint32_t nNewSRID_MSB = nNewSRID; + int32_t nNewSRID_MSB = nNewSRID; CPL_MSBPTR32(&nNewSRID_MSB); GByte abySRID_MSB[5] = {0, 0, 0, 0}; memcpy(abySRID_MSB, &nNewSRID_MSB, 4); diff --git a/ogr/ogrutils.cpp b/ogr/ogrutils.cpp index 124447c1786f..0a1956bc8c42 100644 --- a/ogr/ogrutils.cpp +++ b/ogr/ogrutils.cpp @@ -2176,12 +2176,15 @@ OGRErr OGRReadWKBGeometryType(const unsigned char *pabyData, iRawType = wkbMultiSurface; } + // Below additions cannot occur due to clearing higher bits previously if (bIs3D) { + // coverity[overflow_const] iRawType += 1000; } if (bIsMeasured) { + // coverity[overflow_const] iRawType += 2000; } diff --git a/port/cpl_minizip_unzip.cpp b/port/cpl_minizip_unzip.cpp index a0846eae2caf..54ff1aaba27d 100644 --- a/port/cpl_minizip_unzip.cpp +++ b/port/cpl_minizip_unzip.cpp @@ -1508,6 +1508,7 @@ extern int ZEXPORT cpl_unzOpenCurrentFile3(unzFile file, int *method, pfile_in_zip_read_info->stream_initialised = 1; else { + TRYFREE(pfile_in_zip_read_info->read_buffer); TRYFREE(pfile_in_zip_read_info); return err; }