From 0b98a862e7df5c56ea7373448b043130bc0dbcc3 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sat, 12 Oct 2024 16:34:42 +0200 Subject: [PATCH] cppcheck fixes with latest cppcheck master --- frmts/gif/gifabstractdataset.cpp | 3 +- frmts/gsg/gsagdataset.cpp | 1 + frmts/gtiff/gt_citation.cpp | 12 +++---- frmts/jp2lura/jp2lurarasterband.cpp | 8 ----- frmts/jpeg/jpgdataset.cpp | 7 ++-- frmts/ngsgeoid/ngsgeoiddataset.cpp | 4 +-- frmts/png/pngdataset.cpp | 3 +- frmts/webp/webpdataset.cpp | 1 + frmts/wms/minidriver_arcgis_server.cpp | 4 +-- gcore/gdal_misc.cpp | 4 +-- gcore/gdal_pam.h | 5 +-- gcore/gdaldriver.cpp | 2 +- gcore/gdaljp2abstractdataset.cpp | 4 +-- gcore/gdalmultidim.cpp | 10 +++--- gcore/gdalpamdataset.cpp | 2 +- gcore/gdalpamproxydb.cpp | 1 + gnm/gnmgraph.cpp | 6 +--- ogr/ogrfielddefn.cpp | 1 + ogr/ogrsf_frmts/generic/ogrunionlayer.cpp | 11 ++---- ogr/ogrsf_frmts/generic/ogrunionlayer.h | 42 +++++++++++------------ ogr/ogrsf_frmts/gmlas/ogrgmlaslayer.cpp | 37 ++++++++++---------- ogr/ogrsf_frmts/ntf/ntf_estlayers.cpp | 12 +++---- ogr/ogrsf_frmts/ntf/ntf_generic.cpp | 4 +-- ogr/ogrsf_frmts/oapif/ogroapifdriver.cpp | 1 + ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp | 5 ++- ogr/ogrsf_frmts/tiger/tigeraltname.cpp | 2 +- port/cpl_minizip_unzip.cpp | 5 +-- port/vsipreload.cpp | 18 +++++----- scripts/cppcheck.sh | 4 +++ 29 files changed, 105 insertions(+), 114 deletions(-) diff --git a/frmts/gif/gifabstractdataset.cpp b/frmts/gif/gifabstractdataset.cpp index f0d7e60a8e30..1d781a294709 100644 --- a/frmts/gif/gifabstractdataset.cpp +++ b/frmts/gif/gifabstractdataset.cpp @@ -166,13 +166,14 @@ void GIFAbstractDataset::CollectXMPMetadata() if (!osXMP.empty()) { /* Avoid setting the PAM dirty bit just for that */ - int nOldPamFlags = nPamFlags; + const int nOldPamFlags = nPamFlags; char *apszMDList[2]; apszMDList[0] = (char *)osXMP.c_str(); apszMDList[1] = nullptr; SetMetadata(apszMDList, "xml:XMP"); + // cppcheck-suppress redundantAssignment nPamFlags = nOldPamFlags; } diff --git a/frmts/gsg/gsagdataset.cpp b/frmts/gsg/gsagdataset.cpp index 5a5285466312..c4b3af4ab895 100644 --- a/frmts/gsg/gsagdataset.cpp +++ b/frmts/gsg/gsagdataset.cpp @@ -369,6 +369,7 @@ CPLErr GSAGRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) } else if (*szStart != '\0') { + // cppcheck-suppress redundantAssignment szEnd = szStart; while (!isspace((unsigned char)*szEnd) && *szEnd != '\0') szEnd++; diff --git a/frmts/gtiff/gt_citation.cpp b/frmts/gtiff/gt_citation.cpp index e98b70708d1e..77536ecd78b7 100644 --- a/frmts/gtiff/gt_citation.cpp +++ b/frmts/gtiff/gt_citation.cpp @@ -621,9 +621,7 @@ OGRBoolean CheckCitationKeyForStatePlaneUTM(GTIF *hGTIF, GTIFDefn *psDefn, if (GDALGTIFKeyGetASCII(hGTIF, GTCitationGeoKey, szCTString, sizeof(szCTString))) { - CPLString osLCCT = szCTString; - - osLCCT.tolower(); + const CPLString osLCCT = CPLString(szCTString).tolower(); if (strstr(osLCCT, "us") && strstr(osLCCT, "survey") && (strstr(osLCCT, "feet") || strstr(osLCCT, "foot"))) @@ -659,8 +657,9 @@ OGRBoolean CheckCitationKeyForStatePlaneUTM(GTIF *hGTIF, GTIFDefn *psDefn, if (poUnit != nullptr && poUnit->GetChildCount() >= 2) { - CPLString unitName = poUnit->GetChild(0)->GetValue(); - unitName.tolower(); + const CPLString unitName = + CPLString(poUnit->GetChild(0)->GetValue()) + .tolower(); if (strstr(units, "us_survey_feet")) { @@ -692,9 +691,8 @@ OGRBoolean CheckCitationKeyForStatePlaneUTM(GTIF *hGTIF, GTIFDefn *psDefn, GTIFGetUOMLengthInfo(psDefn->UOMLength, &pszUnitsName, nullptr); if (pszUnitsName) { - CPLString osLCCT = pszUnitsName; + const CPLString osLCCT = CPLString(pszUnitsName).tolower(); GTIFFreeMemory(pszUnitsName); - osLCCT.tolower(); if (strstr(osLCCT, "us") && strstr(osLCCT, "survey") && (strstr(osLCCT, "feet") || strstr(osLCCT, "foot"))) diff --git a/frmts/jp2lura/jp2lurarasterband.cpp b/frmts/jp2lura/jp2lurarasterband.cpp index dc7290b3398c..e392bfd09202 100644 --- a/frmts/jp2lura/jp2lurarasterband.cpp +++ b/frmts/jp2lura/jp2lurarasterband.cpp @@ -42,19 +42,11 @@ JP2LuraRasterBand::JP2LuraRasterBand(JP2LuraDataset *poDSIn, int nBandIn, { nBlockXSize = 2048; } - else - { - nBlockXSize = nRasterXSize; - } if (nRasterYSize >= 128) { nBlockYSize = 128; } - else - { - nBlockYSize = nRasterYSize; - } } if ((nBits % 8) != 0) diff --git a/frmts/jpeg/jpgdataset.cpp b/frmts/jpeg/jpgdataset.cpp index 23da3df223a6..a8425a65b39a 100644 --- a/frmts/jpeg/jpgdataset.cpp +++ b/frmts/jpeg/jpgdataset.cpp @@ -383,6 +383,7 @@ void JPGDatasetCommon::ReadXMPMetadata() char *apszMDList[2] = {pszXMP, nullptr}; SetMetadata(apszMDList, "xml:XMP"); + // cppcheck-suppress redundantAssignment nPamFlags = nOldPamFlags; } VSIFree(pszXMP); @@ -3036,7 +3037,7 @@ JPGDatasetCommon *JPGDataset::OpenStage2(JPGDatasetOpenArgs *psArgs, // Open the file using the large file api if necessary. VSILFILE *fpImage = fpLin; - if (fpImage == nullptr) + if (!fpImage) { fpImage = VSIFOpenL(real_filename, "rb"); @@ -3049,10 +3050,6 @@ JPGDatasetCommon *JPGDataset::OpenStage2(JPGDatasetOpenArgs *psArgs, return nullptr; } } - else - { - fpImage = fpLin; - } // Create a corresponding GDALDataset. poDS->nQLevel = nQLevel; diff --git a/frmts/ngsgeoid/ngsgeoiddataset.cpp b/frmts/ngsgeoid/ngsgeoiddataset.cpp index e7b2adb08128..f530eb9b81bc 100644 --- a/frmts/ngsgeoid/ngsgeoiddataset.cpp +++ b/frmts/ngsgeoid/ngsgeoiddataset.cpp @@ -375,8 +375,8 @@ const OGRSpatialReference *NGSGEOIDDataset::GetSpatialRef() const return &m_oSRS; } - CPLString osFilename(CPLGetBasename(GetDescription())); - osFilename.tolower(); + const CPLString osFilename = + CPLString(CPLGetBasename(GetDescription())).tolower(); // See https://www.ngs.noaa.gov/GEOID/GEOID12B/faq_2012B.shtml diff --git a/frmts/png/pngdataset.cpp b/frmts/png/pngdataset.cpp index a968f0c8ad64..5d49a9026392 100644 --- a/frmts/png/pngdataset.cpp +++ b/frmts/png/pngdataset.cpp @@ -1588,11 +1588,12 @@ void PNGDataset::CollectXMPMetadata() if (memcmp(pszContent, "XML:com.adobe.xmp\0\0\0\0\0", 22) == 0) { // Avoid setting the PAM dirty bit just for that. - int nOldPamFlags = nPamFlags; + const int nOldPamFlags = nPamFlags; char *apszMDList[2] = {pszContent + 22, nullptr}; SetMetadata(apszMDList, "xml:XMP"); + // cppcheck-suppress redundantAssignment nPamFlags = nOldPamFlags; VSIFree(pszContent); diff --git a/frmts/webp/webpdataset.cpp b/frmts/webp/webpdataset.cpp index ce8b6366aa99..56516a8e812a 100644 --- a/frmts/webp/webpdataset.cpp +++ b/frmts/webp/webpdataset.cpp @@ -247,6 +247,7 @@ char **WEBPDataset::GetMetadata(const char *pszDomain) char *apszMDList[2] = {pszXMP, nullptr}; SetMetadata(apszMDList, "xml:XMP"); + // cppcheck-suppress redundantAssignment nPamFlags = nOldPamFlags; VSIFree(pszXMP); diff --git a/frmts/wms/minidriver_arcgis_server.cpp b/frmts/wms/minidriver_arcgis_server.cpp index f90fa224a5c3..56c606c48cbb 100644 --- a/frmts/wms/minidriver_arcgis_server.cpp +++ b/frmts/wms/minidriver_arcgis_server.cpp @@ -68,8 +68,8 @@ CPLErr WMSMiniDriver_AGS::Initialize(CPLXMLNode *config, m_image_format = CPLGetXMLValue(config, "ImageFormat", "png"); m_time_range = CPLGetXMLValue(config, "TimeRange", ""); - m_transparent = CPLGetXMLValue(config, "Transparent", ""); - m_transparent.tolower(); + m_transparent = + CPLString(CPLGetXMLValue(config, "Transparent", "")).tolower(); m_layers = CPLGetXMLValue(config, "Layers", ""); const char *irs = CPLGetXMLValue(config, "SRS", "102100"); diff --git a/gcore/gdal_misc.cpp b/gcore/gdal_misc.cpp index 822ed2823928..047a789993ed 100644 --- a/gcore/gdal_misc.cpp +++ b/gcore/gdal_misc.cpp @@ -1869,9 +1869,9 @@ CPLString GDALFindAssociatedFile(const char *pszBaseFilename, CPLString osAltExt = pszExt; if (islower(static_cast(pszExt[0]))) - osAltExt.toupper(); + osAltExt = osAltExt.toupper(); else - osAltExt.tolower(); + osAltExt = osAltExt.tolower(); osTarget = CPLResetExtension(pszBaseFilename, osAltExt); diff --git a/gcore/gdal_pam.h b/gcore/gdal_pam.h index 2b95741eb0b1..1bfaf2355919 100644 --- a/gcore/gdal_pam.h +++ b/gcore/gdal_pam.h @@ -38,6 +38,7 @@ class GDALPamRasterBand; #define GCIF_SCALEOFFSET 0x008000 #define GCIF_UNITTYPE 0x010000 #define GCIF_COLORTABLE 0x020000 +/* Same value as GCIF_COLORTABLE */ #define GCIF_COLORINTERP 0x020000 #define GCIF_BAND_METADATA 0x040000 #define GCIF_RAT 0x080000 @@ -50,8 +51,8 @@ class GDALPamRasterBand; #define GCIF_PAM_DEFAULT \ (GCIF_GEOTRANSFORM | GCIF_PROJECTION | GCIF_METADATA | GCIF_GCPS | \ GCIF_NODATA | GCIF_CATEGORYNAMES | GCIF_MINMAX | GCIF_SCALEOFFSET | \ - GCIF_UNITTYPE | GCIF_COLORTABLE | GCIF_COLORINTERP | GCIF_BAND_METADATA | \ - GCIF_RAT | GCIF_MASK | GCIF_ONLY_IF_MISSING | GCIF_PROCESS_BANDS | \ + GCIF_UNITTYPE | GCIF_COLORTABLE | GCIF_BAND_METADATA | GCIF_RAT | \ + GCIF_MASK | GCIF_ONLY_IF_MISSING | GCIF_PROCESS_BANDS | \ GCIF_BAND_DESCRIPTION) /* GDAL PAM Flags */ diff --git a/gcore/gdaldriver.cpp b/gcore/gdaldriver.cpp index 6be4d75cc5cb..728ded9e90e1 100644 --- a/gcore/gdaldriver.cpp +++ b/gcore/gdaldriver.cpp @@ -1461,7 +1461,7 @@ bool GDALDriver::CanVectorTranslateFrom( if (!ppapszFailureReasons) { for (const char *pszReason : - cpl::Iterate(CSLConstList(papszFailureReasons))) + cpl::Iterate(static_cast(papszFailureReasons))) { CPLDebug("GDAL", "%s", pszReason); } diff --git a/gcore/gdaljp2abstractdataset.cpp b/gcore/gdaljp2abstractdataset.cpp index d20552f237f8..7e6923db73eb 100644 --- a/gcore/gdaljp2abstractdataset.cpp +++ b/gcore/gdaljp2abstractdataset.cpp @@ -208,8 +208,8 @@ void GDALJP2AbstractDataset::LoadJP2Metadata(GDALOpenInfo *poOpenInfo, GDALMultiDomainMetadata oLocalMDMD; oLocalMDMD.XMLInit(psXMLNode, FALSE); GDALDataset::SetMetadata(oLocalMDMD.GetMetadata()); - for (const char *pszDomain : - cpl::Iterate(CSLConstList(oLocalMDMD.GetDomainList()))) + for (const char *pszDomain : cpl::Iterate( + static_cast(oLocalMDMD.GetDomainList()))) { if (!EQUAL(pszDomain, "") && !EQUAL(pszDomain, "IMAGE_STRUCTURE")) diff --git a/gcore/gdalmultidim.cpp b/gcore/gdalmultidim.cpp index 955206d80a90..5e14ba03939b 100644 --- a/gcore/gdalmultidim.cpp +++ b/gcore/gdalmultidim.cpp @@ -6962,7 +6962,8 @@ bool GDALMDArrayMask::IRead(const GUInt64 *arrayStartIdx, const size_t *count, m_poParent->GetRawNoDataValue() == nullptr && GDALDataTypeIsInteger(m_poParent->GetDataType().GetNumericDataType())) { - if (bufferDataType == m_dt) // Byte case + const bool bBufferDataTypeIsByte = bufferDataType == m_dt; + if (bBufferDataTypeIsByte) // Byte case { bool bContiguous = true; for (size_t i = 0; i < nDims; i++) @@ -6999,7 +7000,6 @@ bool GDALMDArrayMask::IRead(const GUInt64 *arrayStartIdx, const size_t *count, size_t dimIdx = 0; const size_t nDimsMinus1 = nDims > 0 ? nDims - 1 : 0; - const bool bBufferDataTypeIsByte = bufferDataType == m_dt; GByte abyOne[16]; // 16 is sizeof GDT_CFloat64 CPLAssert(nBufferDTSize <= 16); const GByte flag = 1; @@ -7015,6 +7015,7 @@ bool GDALMDArrayMask::IRead(const GUInt64 *arrayStartIdx, const size_t *count, while (true) { + // cppcheck-suppress knownConditionTrueFalse if (bBufferDataTypeIsByte) { *dst_ptr = flag; @@ -9485,8 +9486,9 @@ GDALDatasetFromArray *GDALDatasetFromArray::Create( poDS->SetDerivedDatasetName(osDerivedDatasetName.c_str()); poDS->TryLoadXML(); - for (const auto &[pszKey, pszValue] : cpl::IterateNameValue( - CSLConstList(poDS->GDALPamDataset::GetMetadata()))) + for (const auto &[pszKey, pszValue] : + cpl::IterateNameValue(static_cast( + poDS->GDALPamDataset::GetMetadata()))) { poDS->m_oMDD.SetMetadataItem(pszKey, pszValue); } diff --git a/gcore/gdalpamdataset.cpp b/gcore/gdalpamdataset.cpp index eff50b511da1..19bff652e651 100644 --- a/gcore/gdalpamdataset.cpp +++ b/gcore/gdalpamdataset.cpp @@ -1790,7 +1790,7 @@ void GDALPamDataset::ClearStatistics() GDALRasterBand *poBand = GetRasterBand(i); CPLStringList aosNewMD; for (const char *pszStr : - cpl::Iterate(CSLConstList(poBand->GetMetadata()))) + cpl::Iterate(static_cast(poBand->GetMetadata()))) { if (STARTS_WITH_CI(pszStr, "STATISTICS_")) { diff --git a/gcore/gdalpamproxydb.cpp b/gcore/gdalpamproxydb.cpp index 16f772891657..14b4cc260613 100644 --- a/gcore/gdalpamproxydb.cpp +++ b/gcore/gdalpamproxydb.cpp @@ -279,6 +279,7 @@ static void InitProxyDB() { CPLMutexHolderD(&hProxyDBLock); // cppcheck-suppress identicalInnerCondition + // cppcheck-suppress knownConditionTrueFalse if (!bProxyDBInitialized) { const char *pszProxyDir = diff --git a/gnm/gnmgraph.cpp b/gnm/gnmgraph.cpp index e96371ade466..9f77e2918f44 100644 --- a/gnm/gnmgraph.cpp +++ b/gnm/gnmgraph.cpp @@ -571,7 +571,6 @@ void GNMGraph::TraceTargets(std::queue &vertexQueue, std::set &markedVertIds, GNMPATH &connectedIds) { - GNMCONSTVECTOR::const_iterator it; std::queue neighbours_queue; // See all given vertices except thouse that have been already seen. @@ -591,11 +590,8 @@ void GNMGraph::TraceTargets(std::queue &vertexQueue, LPGNMCONSTVECTOR panOutcomeEdgeIDs = GetOutEdges(nCurVertID); if (nullptr != panOutcomeEdgeIDs) { - for (it = panOutcomeEdgeIDs->begin(); - it != panOutcomeEdgeIDs->end(); ++it) + for (const GNMGFID nCurEdgeID : *panOutcomeEdgeIDs) { - GNMGFID nCurEdgeID = *it; - // ISSUE: think about to return a sequence of vertices and // edges (which is more universal), as now we are going to // return only sequence of edges. diff --git a/ogr/ogrfielddefn.cpp b/ogr/ogrfielddefn.cpp index ddf8505d70a7..699486be12db 100644 --- a/ogr/ogrfielddefn.cpp +++ b/ogr/ogrfielddefn.cpp @@ -2315,6 +2315,7 @@ OGRCodedFieldDomain::~OGRCodedFieldDomain() /* OGRRangeFieldDomain() */ /************************************************************************/ +// cppcheck-suppress uninitMemberVar OGRRangeFieldDomain::OGRRangeFieldDomain( const std::string &osName, const std::string &osDescription, OGRFieldType eFieldType, OGRFieldSubType eFieldSubType, diff --git a/ogr/ogrsf_frmts/generic/ogrunionlayer.cpp b/ogr/ogrsf_frmts/generic/ogrunionlayer.cpp index 292ad402880a..31df93f66335 100644 --- a/ogr/ogrsf_frmts/generic/ogrunionlayer.cpp +++ b/ogr/ogrsf_frmts/generic/ogrunionlayer.cpp @@ -62,19 +62,14 @@ OGRUnionLayerGeomFieldDefn::~OGRUnionLayerGeomFieldDefn() /* OGRUnionLayer() */ /************************************************************************/ +// cppcheck-suppress uninitMemberVar OGRUnionLayer::OGRUnionLayer(const char *pszName, int nSrcLayersIn, OGRLayer **papoSrcLayersIn, int bTakeLayerOwnership) : osName(pszName), nSrcLayers(nSrcLayersIn), papoSrcLayers(papoSrcLayersIn), - bHasLayerOwnership(bTakeLayerOwnership), poFeatureDefn(nullptr), - nFields(0), papoFields(nullptr), nGeomFields(0), papoGeomFields(nullptr), - eFieldStrategy(FIELD_UNION_ALL_LAYERS), bPreserveSrcFID(FALSE), - nFeatureCount(-1), iCurLayer(-1), pszAttributeFilter(nullptr), - nNextFID(0), panMap(nullptr), bAttrFilterPassThroughValue(-1), + bHasLayerOwnership(bTakeLayerOwnership), pabModifiedLayers(static_cast(CPLCalloc(sizeof(int), nSrcLayers))), - pabCheckIfAutoWrap( - static_cast(CPLCalloc(sizeof(int), nSrcLayers))), - poGlobalSRS(nullptr) + pabCheckIfAutoWrap(static_cast(CPLCalloc(sizeof(int), nSrcLayers))) { CPLAssert(nSrcLayersIn > 0); diff --git a/ogr/ogrsf_frmts/generic/ogrunionlayer.h b/ogr/ogrsf_frmts/generic/ogrunionlayer.h index 81f1f6bf708e..dc5e0244bd71 100644 --- a/ogr/ogrsf_frmts/generic/ogrunionlayer.h +++ b/ogr/ogrsf_frmts/generic/ogrunionlayer.h @@ -53,32 +53,32 @@ class CPL_DLL OGRUnionLayer final : public OGRLayer CPL_DISALLOW_COPY_ASSIGN(OGRUnionLayer) protected: - CPLString osName; - int nSrcLayers; - OGRLayer **papoSrcLayers; - int bHasLayerOwnership; - - OGRFeatureDefn *poFeatureDefn; - int nFields; - OGRFieldDefn **papoFields; - int nGeomFields; - OGRUnionLayerGeomFieldDefn **papoGeomFields; - FieldUnionStrategy eFieldStrategy; + CPLString osName{}; + int nSrcLayers = 0; + OGRLayer **papoSrcLayers = nullptr; + int bHasLayerOwnership = false; + + OGRFeatureDefn *poFeatureDefn = nullptr; + int nFields = 0; + OGRFieldDefn **papoFields = nullptr; + int nGeomFields = 0; + OGRUnionLayerGeomFieldDefn **papoGeomFields = nullptr; + FieldUnionStrategy eFieldStrategy = FIELD_UNION_ALL_LAYERS; CPLString osSourceLayerFieldName{}; - int bPreserveSrcFID; + int bPreserveSrcFID = false; - GIntBig nFeatureCount; + GIntBig nFeatureCount = -1; - int iCurLayer; - char *pszAttributeFilter; - int nNextFID; - int *panMap; + int iCurLayer = -1; + char *pszAttributeFilter = nullptr; + int nNextFID = 0; + int *panMap = nullptr; CPLStringList m_aosIgnoredFields{}; - int bAttrFilterPassThroughValue; - int *pabModifiedLayers; - int *pabCheckIfAutoWrap; - const OGRSpatialReference *poGlobalSRS; + int bAttrFilterPassThroughValue = -1; + int *pabModifiedLayers = nullptr; + int *pabCheckIfAutoWrap = nullptr; + const OGRSpatialReference *poGlobalSRS = nullptr; void AutoWarpLayerIfNecessary(int iSubLayer); OGRFeature *TranslateFromSrcLayer(OGRFeature *poSrcFeature); diff --git a/ogr/ogrsf_frmts/gmlas/ogrgmlaslayer.cpp b/ogr/ogrsf_frmts/gmlas/ogrgmlaslayer.cpp index f4b17f32b5db..63dc41e836fc 100644 --- a/ogr/ogrsf_frmts/gmlas/ogrgmlaslayer.cpp +++ b/ogr/ogrsf_frmts/gmlas/ogrgmlaslayer.cpp @@ -205,9 +205,8 @@ void OGRGMLASLayer::ProcessDataRecordOfDataArrayCreateFields( if (psIter->eType == CXT_Element && strcmp(psIter->pszValue, "field") == 0) { - CPLString osName = CPLGetXMLValue(psIter, "name", ""); - osName.tolower(); - OGRFieldDefn oFieldDefn(osName, OFTString); + const char *pszName = CPLGetXMLValue(psIter, "name", ""); + OGRFieldDefn oFieldDefn(CPLString(pszName).tolower(), OFTString); OGRFieldType eType; OGRFieldSubType eSubType; CPLXMLNode *psNode = GetSWEChildAndType(psIter, eType, eSubType); @@ -257,8 +256,8 @@ void OGRGMLASLayer::ProcessDataRecordCreateFields( if (psIter->eType == CXT_Element && strcmp(psIter->pszValue, "field") == 0) { - CPLString osName = CPLGetXMLValue(psIter, "name", ""); - osName = osName.tolower(); + const char *pszName = CPLGetXMLValue(psIter, "name", ""); + CPLString osName = CPLString(pszName).tolower(); OGRFieldDefn oFieldDefn(osName, OFTString); OGRFieldType eType; OGRFieldSubType eSubType; @@ -316,8 +315,9 @@ void OGRGMLASLayer::ProcessDataRecordCreateFields( if (psIter2->eType == CXT_Element && strcmp(psIter2->pszValue, "value") != 0) { - CPLString osName2(osName + "_" + psIter2->pszValue); - osName2.tolower(); + const CPLString osName2 = + CPLString(osName + "_" + psIter2->pszValue) + .tolower(); for (CPLXMLNode *psIter3 = psIter2->psChild; psIter3 != nullptr; psIter3 = psIter3->psNext) { @@ -327,9 +327,10 @@ void OGRGMLASLayer::ProcessDataRecordCreateFields( const char *pszColon = strchr(pszValue, ':'); if (pszColon) pszValue = pszColon + 1; - CPLString osName3(osName2 + "_" + pszValue); - osName3.tolower(); - OGRFieldDefn oFieldDefn2(osName3, OFTString); + OGRFieldDefn oFieldDefn2( + CPLString(osName2 + "_" + pszValue) + .tolower(), + OFTString); m_poFeatureDefn->AddFieldDefn(&oFieldDefn2); } else if (psIter3->eType == CXT_Text) @@ -397,8 +398,8 @@ void OGRGMLASLayer::ProcessDataRecordFillFeature(CPLXMLNode *psDataRecord, if (psIter->eType == CXT_Element && strcmp(psIter->pszValue, "field") == 0) { - CPLString osName = CPLGetXMLValue(psIter, "name", ""); - osName = osName.tolower(); + const char *pszName = CPLGetXMLValue(psIter, "name", ""); + CPLString osName = CPLString(pszName).tolower(); OGRFieldDefn oFieldDefn(osName, OFTString); OGRFieldType eType; OGRFieldSubType eSubType; @@ -416,8 +417,8 @@ void OGRGMLASLayer::ProcessDataRecordFillFeature(CPLXMLNode *psDataRecord, { if (psIter2->eType == CXT_Element) { - CPLString osName2(osName + "_" + psIter2->pszValue); - osName2.tolower(); + const CPLString osName2 = + CPLString(osName + "_" + psIter2->pszValue).tolower(); for (CPLXMLNode *psIter3 = psIter2->psChild; psIter3 != nullptr; psIter3 = psIter3->psNext) { @@ -427,10 +428,10 @@ void OGRGMLASLayer::ProcessDataRecordFillFeature(CPLXMLNode *psDataRecord, const char *pszColon = strchr(pszValue, ':'); if (pszColon) pszValue = pszColon + 1; - CPLString osName3(osName2 + "_" + pszValue); - osName3.tolower(); - SetSWEValue(poFeature, osName3, - psIter3->psChild->pszValue); + SetSWEValue( + poFeature, + CPLString(osName2 + "_" + pszValue).tolower(), + psIter3->psChild->pszValue); } else if (psIter3->eType == CXT_Text) { diff --git a/ogr/ogrsf_frmts/ntf/ntf_estlayers.cpp b/ogr/ogrsf_frmts/ntf/ntf_estlayers.cpp index aa5377462aef..379f86f93cbf 100644 --- a/ogr/ogrsf_frmts/ntf/ntf_estlayers.cpp +++ b/ogr/ogrsf_frmts/ntf/ntf_estlayers.cpp @@ -469,7 +469,7 @@ static OGRFeature *TranslateBoundarylineCollection(NTFFileReader *poReader, poFeature->SetField(1, nNumLinks); // POLY_ID - int i, anList[MAX_LINK]; + int i, anList[MAX_LINK] = {0}; for (i = 0; i < nNumLinks; i++) anList[i] = atoi(papoGroup[0]->GetField(15 + i * 8, 20 + i * 8)); @@ -520,7 +520,7 @@ static OGRFeature *TranslateBoundarylinePoly(NTFFileReader *poReader, poFeature->SetField(4, nNumLinks); // DIR - int i, anList[MAX_LINK]; + int i, anList[MAX_LINK] = {0}; for (i = 0; i < nNumLinks; i++) anList[i] = atoi(papoGroup[2]->GetField(19 + i * 7, 19 + i * 7)); @@ -718,7 +718,7 @@ static OGRFeature *TranslateBL2000Poly(NTFFileReader *poReader, poFeature->SetField(3, nNumLinks); // DIR - int i, anList[MAX_LINK]; + int i, anList[MAX_LINK] = {0}; for (i = 0; i < nNumLinks; i++) anList[i] = atoi(papoGroup[2]->GetField(19 + i * 7, 19 + i * 7)); @@ -908,7 +908,7 @@ static OGRFeature *TranslateBL2000Collection(NTFFileReader *poReader, poFeature->SetField(1, nNumLinks); // POLY_ID / COLL_ID_REFS - int anList[MAX_LINK], anCollList[MAX_LINK]; + int anList[MAX_LINK] = {0}, anCollList[MAX_LINK] = {0}; int nPolys = 0, nCollections = 0; for (int i = 0; i < nNumLinks; i++) @@ -1117,7 +1117,7 @@ static OGRFeature *TranslateStrategiNode(CPL_UNUSED NTFFileReader *poReader, poFeature->SetField(2, nNumLinks); // DIR - int i, anList[MAX_LINK]; + int i, anList[MAX_LINK] = {0}; for (i = 0; i < nNumLinks; i++) anList[i] = atoi(papoGroup[0]->GetField(19 + i * 12, 19 + i * 12)); @@ -1141,7 +1141,7 @@ static OGRFeature *TranslateStrategiNode(CPL_UNUSED NTFFileReader *poReader, // ORIENT (optional) if (EQUAL(poFeature->GetDefnRef()->GetFieldDefn(6)->GetNameRef(), "ORIENT")) { - double adfList[MAX_LINK]; + double adfList[MAX_LINK] = {0}; for (i = 0; i < nNumLinks; i++) adfList[i] = atoi(papoGroup[0]->GetField(19 + i * 12 + 7, diff --git a/ogr/ogrsf_frmts/ntf/ntf_generic.cpp b/ogr/ogrsf_frmts/ntf/ntf_generic.cpp index 0dd87639501e..53a78e49bc2b 100644 --- a/ogr/ogrsf_frmts/ntf/ntf_generic.cpp +++ b/ogr/ogrsf_frmts/ntf/ntf_generic.cpp @@ -720,7 +720,7 @@ static OGRFeature *TranslateGenericPoly(NTFFileReader *poReader, poFeature->SetField("NUM_PARTS", nNumLinks); // DIR - int i, anList[MAX_LINK]; + int i, anList[MAX_LINK] = {0}; for (i = 0; i < nNumLinks; i++) anList[i] = atoi(papoGroup[1]->GetField(19 + i * 7, 19 + i * 7)); @@ -804,7 +804,7 @@ static OGRFeature *TranslateGenericCPoly(NTFFileReader *poReader, /* in the RingStart field. */ /* -------------------------------------------------------------------- */ int nNumLink = 0; - int anPolyId[MAX_LINK * 2]; + int anPolyId[MAX_LINK * 2] = {0}; nNumLink = atoi(papoGroup[0]->GetField(9, 12)); if (nNumLink < 0 || nNumLink > MAX_LINK) diff --git a/ogr/ogrsf_frmts/oapif/ogroapifdriver.cpp b/ogr/ogrsf_frmts/oapif/ogroapifdriver.cpp index be7890f46206..47886f0f79fb 100644 --- a/ogr/ogrsf_frmts/oapif/ogroapifdriver.cpp +++ b/ogr/ogrsf_frmts/oapif/ogroapifdriver.cpp @@ -2228,6 +2228,7 @@ OGRFeature *OGROAPIFLayer::GetNextRawFeature() } } + // cppcheck-suppress nullPointerRedundantCheck poSrcFeature = m_poUnderlyingLayer->GetNextFeature(); if (poSrcFeature) { diff --git a/ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp b/ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp index 0362f7f18630..9be77144bbe7 100644 --- a/ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp +++ b/ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp @@ -2212,9 +2212,8 @@ OGRLayer *OGRPGDataSource::GetLayerByName(const char *pszNameIn) { EndCopy(); - CPLString osTableName(pszTableName); - CPLString osTableNameLower(pszTableName); - osTableNameLower.tolower(); + const CPLString osTableName(pszTableName); + const CPLString osTableNameLower = CPLString(pszTableName).tolower(); if (osTableName != osTableNameLower) CPLPushErrorHandler(CPLQuietErrorHandler); poLayer = OpenTable(osCurrentSchema, pszTableName, pszSchemaName, diff --git a/ogr/ogrsf_frmts/tiger/tigeraltname.cpp b/ogr/ogrsf_frmts/tiger/tigeraltname.cpp index 0dc1ba2771de..f270d794460c 100644 --- a/ogr/ogrsf_frmts/tiger/tigeraltname.cpp +++ b/ogr/ogrsf_frmts/tiger/tigeraltname.cpp @@ -99,7 +99,7 @@ OGRFeature *TigerAltName::GetFeature(int nRecordId) /* -------------------------------------------------------------------- */ OGRFeature *poFeature = new OGRFeature(poFeatureDefn); - int anFeatList[5]; + int anFeatList[5] = {0}; int nFeatCount = 0; SetFields(psRTInfo, poFeature, achRecord); diff --git a/port/cpl_minizip_unzip.cpp b/port/cpl_minizip_unzip.cpp index 0302584bac2b..4a5311945016 100644 --- a/port/cpl_minizip_unzip.cpp +++ b/port/cpl_minizip_unzip.cpp @@ -1217,6 +1217,7 @@ extern int ZEXPORT cpl_unzLocateFile(unzFile file, const char *szFileName, /* We failed, so restore the state of the 'current file' to where we * were. */ + // cppcheck-suppress redundantAssignment s->num_file = num_fileSaved; s->pos_in_central_dir = pos_in_central_dirSaved; s->cur_file_info = cur_file_infoSaved; @@ -1311,7 +1312,7 @@ unzlocal_CheckCurrentFileCoherencyHeader(unz_s *s, uInt *piSizeVar, ZLIB_FILEFUNC_SEEK_SET) != 0) return UNZ_ERRNO; - if (err == UNZ_OK) + // if (err == UNZ_OK) { if (unzlocal_getLong(&s->z_filefunc, s->filestream, &uMagic) != UNZ_OK) err = UNZ_ERRNO; @@ -1614,7 +1615,7 @@ extern int cpl_unzCurrentFileInfoFromLocalHeader( ZLIB_FILEFUNC_SEEK_SET) != 0) return UNZ_ERRNO; - if (err == UNZ_OK) + // if (err == UNZ_OK) { if (unzlocal_getLong(&s->z_filefunc, s->filestream, &uMagic) != UNZ_OK) err = UNZ_ERRNO; diff --git a/port/vsipreload.cpp b/port/vsipreload.cpp index 382858b505be..1e8294678a32 100644 --- a/port/vsipreload.cpp +++ b/port/vsipreload.cpp @@ -1066,13 +1066,12 @@ static int myopen(const char *path, int flags, ...) CPLFormFilename(osCurDir.c_str(), path, nullptr)); if (strchr(osCurDir.c_str(), '/') != nullptr && strcmp(path, "..") == 0) { - char *lastslash = strrchr(newname, '/'); - if (lastslash != nullptr) + for (int i = 0; i < 2; ++i) { + char *lastslash = strrchr(newname, '/'); + if (!lastslash) + break; *lastslash = 0; - lastslash = strrchr(newname, '/'); - if (lastslash != nullptr) - *lastslash = 0; } } if (VSIStatL(newname, &sStatBufL) == 0 && S_ISDIR(sStatBufL.st_mode)) @@ -1136,13 +1135,12 @@ int CPL_DLL open64(const char *path, int flags, ...) CPLFormFilename(osCurDir.c_str(), path, nullptr)); if (strchr(osCurDir.c_str(), '/') != nullptr && strcmp(path, "..") == 0) { - char *lastslash = strrchr(newname, '/'); - if (lastslash != nullptr) + for (int i = 0; i < 2; ++i) { + char *lastslash = strrchr(newname, '/'); + if (!lastslash) + break; *lastslash = 0; - lastslash = strrchr(newname, '/'); - if (lastslash != nullptr) - *lastslash = 0; } } if (VSIStatL(newname, &sStatBufL) == 0 && S_ISDIR(sStatBufL.st_mode)) diff --git a/scripts/cppcheck.sh b/scripts/cppcheck.sh index 3946fce58b5d..ef7eb728a714 100755 --- a/scripts/cppcheck.sh +++ b/scripts/cppcheck.sh @@ -112,6 +112,9 @@ for dirname in alg port gcore ogr frmts gnm apps fuzzers; do -DGNMGFIDFormat="\"%lld\"" \ -DGDAL_RELEASE_NAME="\"dummy\"" \ "-DBANDMAP_TYPE=int*" \ + -DSQLITE_UTF8=1 \ + -DSQLITE_DETERMINISTIC=0x000000800 \ + -DSQLITE_INNOCUOUS=0x000200000 \ --include="${CPL_CONFIG_H}" \ --include=port/cpl_port.h \ -I "${CPL_CONFIG_H_DIR}" \ @@ -122,6 +125,7 @@ for dirname in alg port gcore ogr frmts gnm apps fuzzers; do -i gdalasyncread.cpp \ -i gdaltorture.cpp \ -i vsifilesystemregistrar.cpp \ + -i external_deferred_plugins.cpp \ $dirname \ -j "$(nproc)" >>${LOG_FILE} 2>&1 & # Display some progress to avoid Travis-CI killing the job after 10 minutes