From 7474f9312a5870304bb33b529d09bd8e4b872d76 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sat, 11 Jan 2025 16:55:42 +0100 Subject: [PATCH] frmts/: manual replacement of last uses of CPLGetExtension() by CPLGetExtensionSafe() --- frmts/adrg/adrgdataset.cpp | 7 ++--- frmts/adrg/srpdataset.cpp | 12 +++++---- frmts/aigrid/aigdataset.cpp | 8 +++--- frmts/bsb/bsbdataset.cpp | 4 +-- frmts/ceos2/sar_ceosdataset.cpp | 3 ++- frmts/ecw/gdal_ecw.h | 5 ++-- frmts/gtiff/gtiffdataset_read.cpp | 2 +- frmts/hdf5/hdf5drivercore.cpp | 2 +- frmts/hfa/hfaband.cpp | 3 ++- frmts/hfa/hfaopen.cpp | 9 ++++--- frmts/ilwis/ilwisdataset.cpp | 12 +++++---- frmts/jp2kak/jp2kakdataset.cpp | 2 +- frmts/jp2kak/jp2kakdrivercore.cpp | 3 +-- .../kmlsuperoverlaydataset.cpp | 17 ++++++------ frmts/mrsid/mrsiddataset.cpp | 2 +- frmts/mrsid/mrsiddrivercore.cpp | 2 +- frmts/netcdf/netcdfdrivercore.cpp | 4 +-- frmts/nitf/rpftocfile.cpp | 5 ++-- frmts/pds/pds4dataset.cpp | 2 +- frmts/raw/byndataset.cpp | 9 +++---- frmts/raw/envidataset.cpp | 2 +- frmts/raw/fastdataset.cpp | 5 ++-- frmts/raw/lcpdataset.cpp | 2 +- frmts/raw/loslasdataset.cpp | 2 +- frmts/raw/mffdataset.cpp | 27 +++++++++---------- frmts/raw/pnmdataset.cpp | 2 +- frmts/raw/roipacdataset.cpp | 7 ++--- frmts/raw/rrasterdataset.cpp | 4 +-- frmts/saga/sagadataset.cpp | 11 ++++---- frmts/srtmhgt/srtmhgtdataset.cpp | 3 ++- 30 files changed, 93 insertions(+), 85 deletions(-) diff --git a/frmts/adrg/adrgdataset.cpp b/frmts/adrg/adrgdataset.cpp index 8b80f5fa2d1d..4100fb5d2900 100644 --- a/frmts/adrg/adrgdataset.cpp +++ b/frmts/adrg/adrgdataset.cpp @@ -1332,7 +1332,8 @@ char **ADRGDataset::GetGENListFromTHF(const char *pszFileName) char *c = (char *)strchr(osSubFileName.c_str(), ' '); if (c) *c = 0; - if (EQUAL(CPLGetExtension(osSubFileName.c_str()), "GEN")) + if (EQUAL(CPLGetExtensionSafe(osSubFileName.c_str()).c_str(), + "GEN")) { CPLDebug("ADRG", "Found GEN file in THF : %s", osSubFileName.c_str()); @@ -1551,7 +1552,7 @@ GDALDataset *ADRGDataset::Open(GDALOpenInfo *poOpenInfo) return nullptr; CPLString osFileName(poOpenInfo->pszFilename); - if (EQUAL(CPLGetExtension(osFileName.c_str()), "THF")) + if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "THF")) { char **papszFileNames = GetGENListFromTHF(osFileName.c_str()); if (papszFileNames == nullptr) @@ -1583,7 +1584,7 @@ GDALDataset *ADRGDataset::Open(GDALOpenInfo *poOpenInfo) } } - if (EQUAL(CPLGetExtension(osFileName.c_str()), "GEN")) + if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "GEN")) { osGENFileName = osFileName; diff --git a/frmts/adrg/srpdataset.cpp b/frmts/adrg/srpdataset.cpp index c35346a7ce67..31ebb1feca14 100644 --- a/frmts/adrg/srpdataset.cpp +++ b/frmts/adrg/srpdataset.cpp @@ -1134,7 +1134,8 @@ char **SRPDataset::GetGENListFromTHF(const char *pszFileName) { while (*ptrDir) { - if (EQUAL(CPLGetExtension(*ptrDir), "GEN")) + if (EQUAL(CPLGetExtensionSafe(*ptrDir).c_str(), + "GEN")) { bFound = 1; osGENFileName = CPLFormFilename( @@ -1160,7 +1161,8 @@ char **SRPDataset::GetGENListFromTHF(const char *pszFileName) { while (*ptrDir) { - if (EQUAL(CPLGetExtension(*ptrDir), "GEN") && + if (EQUAL(CPLGetExtensionSafe(*ptrDir).c_str(), + "GEN") && EQUALN(CPLGetBasename(*ptrDir), osName, 6)) { bFound = 1; @@ -1480,7 +1482,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo) return nullptr; CPLString osFileName(poOpenInfo->pszFilename); - if (EQUAL(CPLGetExtension(osFileName.c_str()), "THF")) + if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "THF")) { CPLDebug("SRP", "Read THF"); @@ -1521,7 +1523,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo) if (bTHFWithSingleGEN #ifdef OPEN_GEN - || EQUAL(CPLGetExtension(osFileName.c_str()), "GEN") + || EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "GEN") #endif ) { @@ -1550,7 +1552,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo) } } - if (EQUAL(CPLGetExtension(osFileName.c_str()), "IMG")) + if (EQUAL(CPLGetExtensionSafe(osFileName.c_str()).c_str(), "IMG")) { osIMGFileName = osFileName; diff --git a/frmts/aigrid/aigdataset.cpp b/frmts/aigrid/aigdataset.cpp index fc363c34d766..dfc1327838b9 100644 --- a/frmts/aigrid/aigdataset.cpp +++ b/frmts/aigrid/aigdataset.cpp @@ -659,8 +659,8 @@ GDALDataset *AIGDataset::Open(GDALOpenInfo *poOpenInfo) for (int iFile = 0; papszFiles != nullptr && papszFiles[iFile] != nullptr; iFile++) { - if (!EQUAL(CPLGetExtension(papszFiles[iFile]), "clr") && - !EQUAL(CPLGetExtension(papszFiles[iFile]), "CLR")) + const std::string osExt = CPLGetExtensionSafe(papszFiles[iFile]); + if (!EQUAL(osExt.c_str(), "clr") && !EQUAL(osExt.c_str(), "CLR")) continue; osClrFilename = @@ -874,12 +874,12 @@ static CPLErr AIGRename(const char *pszNewName, const char *pszOldName) /* -------------------------------------------------------------------- */ CPLString osOldPath, osNewPath; - if (strlen(CPLGetExtension(pszNewName)) > 0) + if (!CPLGetExtensionSafe(pszNewName).empty()) osNewPath = CPLGetPath(pszNewName); else osNewPath = pszNewName; - if (strlen(CPLGetExtension(pszOldName)) > 0) + if (!CPLGetExtensionSafe(pszOldName).empty()) osOldPath = CPLGetPath(pszOldName); else osOldPath = pszOldName; diff --git a/frmts/bsb/bsbdataset.cpp b/frmts/bsb/bsbdataset.cpp index e2bdb34154ba..67217d6a5a4b 100644 --- a/frmts/bsb/bsbdataset.cpp +++ b/frmts/bsb/bsbdataset.cpp @@ -568,11 +568,11 @@ void BSBDataset::ScanForGCPs(bool isNos, const char *pszFilename) void BSBDataset::ScanForGCPsNos(const char *pszFilename) { - const char *extension = CPLGetExtension(pszFilename); + const std::string extension = CPLGetExtensionSafe(pszFilename); // pseudointelligently try and guess whether we want a .geo or a .GEO const char *geofile = nullptr; - if (extension[1] == 'O') + if (extension.size() >= 2 && extension[1] == 'O') { geofile = CPLResetExtension(pszFilename, "GEO"); } diff --git a/frmts/ceos2/sar_ceosdataset.cpp b/frmts/ceos2/sar_ceosdataset.cpp index eb5347088eaf..261274a5dc6f 100644 --- a/frmts/ceos2/sar_ceosdataset.cpp +++ b/frmts/ceos2/sar_ceosdataset.cpp @@ -1824,7 +1824,8 @@ GDALDataset *SAR_CEOSDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ char *pszPath = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename)); char *pszBasename = CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename)); - char *pszExtension = CPLStrdup(CPLGetExtension(poOpenInfo->pszFilename)); + char *pszExtension = + CPLStrdup(CPLGetExtensionSafe(poOpenInfo->pszFilename).c_str()); int nBand; if (strlen(pszBasename) > 4) diff --git a/frmts/ecw/gdal_ecw.h b/frmts/ecw/gdal_ecw.h index 70ec8ec3464d..64ff4467f635 100644 --- a/frmts/ecw/gdal_ecw.h +++ b/frmts/ecw/gdal_ecw.h @@ -220,10 +220,11 @@ class VSIIOStream final : public CNCSJPCIOStream { osFilenameUsed = CPLGenerateTempFilename(nullptr); // try to preserve the extension. - if (strlen(CPLGetExtension(pszFilename)) > 0) + const auto osExt = CPLGetExtensionSafe(pszFilename); + if (!osExt.empty()) { osFilenameUsed += "."; - osFilenameUsed += CPLGetExtension(pszFilename); + osFilenameUsed += osExt; } CPLDebug("ECW", "Using filename '%s' for temporary directory " diff --git a/frmts/gtiff/gtiffdataset_read.cpp b/frmts/gtiff/gtiffdataset_read.cpp index 1ba913fa4e23..e327661f1bca 100644 --- a/frmts/gtiff/gtiffdataset_read.cpp +++ b/frmts/gtiff/gtiffdataset_read.cpp @@ -6586,7 +6586,7 @@ void GTiffDataset::LoadMetadata() return; m_bIMDRPCMetadataLoaded = true; - if (EQUAL(CPLGetExtension(GetDescription()), "ovr")) + if (EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "ovr")) { // Do not attempt to retrieve metadata files on .tif.ovr files. // For example the Pleiades metadata reader might wrongly associate a diff --git a/frmts/hdf5/hdf5drivercore.cpp b/frmts/hdf5/hdf5drivercore.cpp index 32acf189e612..d866323f3332 100644 --- a/frmts/hdf5/hdf5drivercore.cpp +++ b/frmts/hdf5/hdf5drivercore.cpp @@ -34,7 +34,7 @@ int HDF5DatasetIdentify(GDALOpenInfo *poOpenInfo) if (!poOpenInfo->pabyHeader) return FALSE; - const CPLString osExt(CPLGetExtension(poOpenInfo->pszFilename)); + const CPLString osExt(poOpenInfo->osExtension); const auto IsRecognizedByNetCDFDriver = [&osExt, poOpenInfo]() { diff --git a/frmts/hfa/hfaband.cpp b/frmts/hfa/hfaband.cpp index c4d17573c80e..e5a6e6707412 100644 --- a/frmts/hfa/hfaband.cpp +++ b/frmts/hfa/hfaband.cpp @@ -233,7 +233,8 @@ CPLErr HFABand::LoadOverviews() HFAEntry *poBandProxyNode = poNode; HFAInfo_t *psOvHFA = psInfo; - if (nOverviews == 0 && EQUAL(CPLGetExtension(psInfo->pszFilename), "aux")) + if (nOverviews == 0 && + EQUAL(CPLGetExtensionSafe(psInfo->pszFilename).c_str(), "aux")) { const CPLString osRRDFilename = CPLResetExtension(psInfo->pszFilename, "rrd"); diff --git a/frmts/hfa/hfaopen.cpp b/frmts/hfa/hfaopen.cpp index 3e13ddd03001..c1779e4c4555 100644 --- a/frmts/hfa/hfaopen.cpp +++ b/frmts/hfa/hfaopen.cpp @@ -1873,7 +1873,7 @@ HFAHandle HFACreateLL(const char *pszFilename) // If an .ige or .rrd file exists with the same base name, // delete them. (#1784) - CPLString osExtension = CPLGetExtension(pszFilename); + CPLString osExtension = CPLGetExtensionSafe(pszFilename); if (!EQUAL(osExtension, "rrd") && !EQUAL(osExtension, "aux")) { CPLString osPath = CPLGetPath(pszFilename); @@ -2906,7 +2906,7 @@ const char *HFAGetIGEFilename(HFAHandle hHFA) if (VSIStatL(osFullFilename, &sStatBuf) != 0) { const CPLString osExtension = - CPLGetExtension(pszRawFilename); + CPLGetExtensionSafe(pszRawFilename); const CPLString osBasename = CPLGetBasename(hHFA->pszFilename); osFullFilename = @@ -2955,10 +2955,11 @@ bool HFACreateSpillStack(HFAInfo_t *psInfo, int nXSize, int nYSize, int nLayers, if (psInfo->pszIGEFilename == nullptr) { - if (EQUAL(CPLGetExtension(psInfo->pszFilename), "rrd")) + const auto osExt = CPLGetExtensionSafe(psInfo->pszFilename); + if (EQUAL(osExt.c_str(), "rrd")) psInfo->pszIGEFilename = CPLStrdup(CPLResetExtension(psInfo->pszFilename, "rde")); - else if (EQUAL(CPLGetExtension(psInfo->pszFilename), "aux")) + else if (EQUAL(osExt.c_str(), "aux")) psInfo->pszIGEFilename = CPLStrdup(CPLResetExtension(psInfo->pszFilename, "axe")); else diff --git a/frmts/ilwis/ilwisdataset.cpp b/frmts/ilwis/ilwisdataset.cpp index d404a9d914ff..d2e2e5047d92 100644 --- a/frmts/ilwis/ilwisdataset.cpp +++ b/frmts/ilwis/ilwisdataset.cpp @@ -684,9 +684,11 @@ GDALDataset *ILWISDataset::Open(GDALOpenInfo *poOpenInfo) if (poOpenInfo->nHeaderBytes < 1) return nullptr; - std::string sExt = CPLGetExtension(poOpenInfo->pszFilename); - if (!EQUAL(sExt.c_str(), "mpr") && !EQUAL(sExt.c_str(), "mpl")) - return nullptr; + { + const std::string &sExt = poOpenInfo->osExtension; + if (!EQUAL(sExt.c_str(), "mpr") && !EQUAL(sExt.c_str(), "mpl")) + return nullptr; + } if (!CheckASCII(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes)) return nullptr; @@ -736,8 +738,8 @@ GDALDataset *ILWISDataset::Open(GDALOpenInfo *poOpenInfo) // of ILWIS raster maps, std::string sMapStoreName = ReadElement("MapStore", "Data", sBandName); - sExt = CPLGetExtension(sMapStoreName.c_str()); - if (!STARTS_WITH_CI(sExt.c_str(), "mp#")) + if (!STARTS_WITH_CI( + CPLGetExtensionSafe(sMapStoreName.c_str()).c_str(), "mp#")) { CPLError(CE_Failure, CPLE_AppDefined, "Unsupported ILWIS data file. \n" diff --git a/frmts/jp2kak/jp2kakdataset.cpp b/frmts/jp2kak/jp2kakdataset.cpp index 2e874f4cd4de..0301432c7a7c 100644 --- a/frmts/jp2kak/jp2kakdataset.cpp +++ b/frmts/jp2kak/jp2kakdataset.cpp @@ -692,7 +692,7 @@ GDALDataset *JP2KAKDataset::Open(GDALOpenInfo *poOpenInfo) KakaduInitialize(); // Handle setting up datasource for JPIP. - const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszExtension = poOpenInfo->osExtension.c_str(); std::vector abySubfileHeader(16); // leave in this scope if (poOpenInfo->nHeaderBytes < 16) { diff --git a/frmts/jp2kak/jp2kakdrivercore.cpp b/frmts/jp2kak/jp2kakdrivercore.cpp index 5edbaef1a03b..d64327e53d4c 100644 --- a/frmts/jp2kak/jp2kakdrivercore.cpp +++ b/frmts/jp2kak/jp2kakdrivercore.cpp @@ -133,8 +133,7 @@ int JP2KAKDatasetIdentify(GDALOpenInfo *poOpenInfo) else if (memcmp(poOpenInfo->pabyHeader, jpc_header, sizeof(jpc_header)) == 0) { - const char *const pszExtension = - CPLGetExtension(poOpenInfo->pszFilename); + const char *const pszExtension = poOpenInfo->osExtension.c_str(); if (EQUAL(pszExtension, "jpc") || EQUAL(pszExtension, "j2k") || EQUAL(pszExtension, "jp2") || EQUAL(pszExtension, "jpx") || EQUAL(pszExtension, "j2c") || EQUAL(pszExtension, "jhc")) diff --git a/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp b/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp index c3203ce42173..909946eb96b1 100644 --- a/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp +++ b/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp @@ -569,7 +569,8 @@ KmlSuperOverlayCreateCopy(const char *pszFilename, GDALDataset *poSrcDS, } else { - const char *extension = CPLGetExtension(pszFilename); + const std::string osExtension = CPLGetExtensionSafe(pszFilename); + const char *extension = osExtension.c_str(); if (!EQUAL(extension, "kml") && !EQUAL(extension, "kmz")) { CPLError(CE_Failure, CPLE_None, @@ -1681,7 +1682,7 @@ static int KmlSuperOverlayFindRegionStart(CPLXMLNode *psNode, int KmlSuperOverlayReadDataset::Identify(GDALOpenInfo *poOpenInfo) { - const char *pszExt = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszExt = poOpenInfo->osExtension.c_str(); if (EQUAL(pszExt, "kmz")) return -1; if (poOpenInfo->nHeaderBytes == 0) @@ -1746,7 +1747,8 @@ GDALDataset *KmlSuperOverlayReadDataset::Open(GDALOpenInfo *poOpenInfo) static std::unique_ptr KmlSuperOverlayLoadIcon(const char *pszBaseFilename, const char *pszIcon) { - const char *pszExt = CPLGetExtension(pszIcon); + const std::string osExt = CPLGetExtensionSafe(pszIcon); + const char *pszExt = osExt.c_str(); if (!EQUAL(pszExt, "png") && !EQUAL(pszExt, "jpg") && !EQUAL(pszExt, "jpeg")) { @@ -1812,8 +1814,7 @@ static bool KmlSuperOverlayComputeDepth(const std::string &osFilename, CPLGetXMLNode(psIter, "Region") != nullptr && (pszHref = CPLGetXMLValue(psIter, "Link.href", nullptr)) != nullptr) { - const char *pszExt = CPLGetExtension(pszHref); - if (EQUAL(pszExt, "kml")) + if (EQUAL(CPLGetExtensionSafe(pszHref).c_str(), "kml")) { CPLString osSubFilename; if (STARTS_WITH(pszHref, "http")) @@ -2580,8 +2581,7 @@ KmlSuperOverlayReadDataset::Open(const char *pszFilename, if (nRec == 2) return nullptr; CPLString osFilename(pszFilename); - const char *pszExt = CPLGetExtension(pszFilename); - if (EQUAL(pszExt, "kmz")) + if (EQUAL(CPLGetExtensionSafe(pszFilename).c_str(), "kmz")) { if (!STARTS_WITH(pszFilename, "/vsizip/")) osFilename = CPLSPrintf("/vsizip/%s", pszFilename); @@ -2591,8 +2591,7 @@ KmlSuperOverlayReadDataset::Open(const char *pszFilename, char **papszIter = papszFiles; for (; *papszIter != nullptr; papszIter++) { - pszExt = CPLGetExtension(*papszIter); - if (EQUAL(pszExt, "kml")) + if (EQUAL(CPLGetExtensionSafe(*papszIter).c_str(), "kml")) { osFilename = CPLFormFilename(osFilename, *papszIter, nullptr); osFilename = KMLRemoveSlash(osFilename); diff --git a/frmts/mrsid/mrsiddataset.cpp b/frmts/mrsid/mrsiddataset.cpp index f91b0bd34b67..65a9d4a65e6e 100644 --- a/frmts/mrsid/mrsiddataset.cpp +++ b/frmts/mrsid/mrsiddataset.cpp @@ -1307,7 +1307,7 @@ CPLErr MrSIDDataset::OpenZoomLevel(lt_int32 iZoom) /* projection */ /* -------------------------------------------------------------------- */ if (iZoom == 0 && m_oSRS.IsEmpty() && - EQUAL(CPLGetExtension(GetDescription()), "sid")) + EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "sid")) { const char *pszMETFilename = CPLResetExtension(GetDescription(), "met"); VSILFILE *fp = VSIFOpenL(pszMETFilename, "rb"); diff --git a/frmts/mrsid/mrsiddrivercore.cpp b/frmts/mrsid/mrsiddrivercore.cpp index ae34bf7cd4e1..ff620b88a67e 100644 --- a/frmts/mrsid/mrsiddrivercore.cpp +++ b/frmts/mrsid/mrsiddrivercore.cpp @@ -48,7 +48,7 @@ int MrSIDJP2Identify(GDALOpenInfo *poOpenInfo) if (memcmp(poOpenInfo->pabyHeader, jpc_header, sizeof(jpc_header)) == 0) { - const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszExtension = poOpenInfo->osExtension.c_str(); if (!EQUAL(pszExtension, "jpc") && !EQUAL(pszExtension, "j2k") && !EQUAL(pszExtension, "jp2") && !EQUAL(pszExtension, "jpx") && diff --git a/frmts/netcdf/netcdfdrivercore.cpp b/frmts/netcdf/netcdfdrivercore.cpp index 50581dae3f4c..3c47fb1993db 100644 --- a/frmts/netcdf/netcdfdrivercore.cpp +++ b/frmts/netcdf/netcdfdrivercore.cpp @@ -105,7 +105,7 @@ NetCDFFormatEnum netCDFIdentifyFormat(GDALOpenInfo *poOpenInfo, bool bCheckExt) if (bCheckExt) { // Check by default. - const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszExtension = poOpenInfo->osExtension.c_str(); if (!(EQUAL(pszExtension, "nc") || EQUAL(pszExtension, "cdf") || EQUAL(pszExtension, "nc2") || EQUAL(pszExtension, "nc4") || EQUAL(pszExtension, "nc3") || EQUAL(pszExtension, "grd") || @@ -154,7 +154,7 @@ NetCDFFormatEnum netCDFIdentifyFormat(GDALOpenInfo *poOpenInfo, bool bCheckExt) // The HDF5 signature of netCDF 4 files can be at offsets 512, 1024, 2048, // etc. - const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszExtension = poOpenInfo->osExtension.c_str(); if (poOpenInfo->fpL != nullptr && (!bCheckExt || EQUAL(pszExtension, "nc") || EQUAL(pszExtension, "cdf") || EQUAL(pszExtension, "nc4") || diff --git a/frmts/nitf/rpftocfile.cpp b/frmts/nitf/rpftocfile.cpp index d3ca6b2f0718..eea441c12a18 100644 --- a/frmts/nitf/rpftocfile.cpp +++ b/frmts/nitf/rpftocfile.cpp @@ -579,8 +579,9 @@ RPFToc *RPFTOCReadFromBuffer(const char *pszFilename, VSILFILE *fp, // some CADRG maps have legend name smaller than 8.3 then the extension // has blanks (0x20) at the end -> check only the first 3 letters of the // extension. - const char *fileExt = CPLGetExtension(frameEntry->filename); - if (EQUALN(fileExt, "ovr", 3) || EQUALN(fileExt, "lgd", 3)) + const std::string fileExt = CPLGetExtensionSafe(frameEntry->filename); + if (EQUALN(fileExt.c_str(), "ovr", 3) || + EQUALN(fileExt.c_str(), "lgd", 3)) { entry->isOverviewOrLegend = TRUE; } diff --git a/frmts/pds/pds4dataset.cpp b/frmts/pds/pds4dataset.cpp index 09e0a1fd5198..a65a9ce15cb0 100644 --- a/frmts/pds/pds4dataset.cpp +++ b/frmts/pds/pds4dataset.cpp @@ -1405,7 +1405,7 @@ static CPLString FixupTableFilename(const CPLString &osFilename) { return osFilename; } - CPLString osExt = CPLGetExtension(osFilename); + CPLString osExt = CPLGetExtensionSafe(osFilename); if (!osExt.empty()) { CPLString osTry(osFilename); diff --git a/frmts/raw/byndataset.cpp b/frmts/raw/byndataset.cpp index e36432527bd3..406e2e7eb98c 100644 --- a/frmts/raw/byndataset.cpp +++ b/frmts/raw/byndataset.cpp @@ -175,7 +175,7 @@ int BYNDataset::Identify(GDALOpenInfo *poOpenInfo) /* Check file extension (.byn/.err) */ /* -------------------------------------------------------------------- */ #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - const char *pszFileExtension = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszFileExtension = poOpenInfo->osExtension.c_str(); if (!EQUAL(pszFileExtension, "byn") && !EQUAL(pszFileExtension, "err")) { @@ -720,19 +720,16 @@ GDALDataset *BYNDataset::Create(const char *pszFilename, int nXSize, int nYSize, /* Check file extension (.byn/.err) */ /* -------------------------------------------------------------------- */ - char *pszFileExtension = CPLStrdup(CPLGetExtension(pszFilename)); + const std::string osExt = CPLGetExtensionSafe(pszFilename); - if (!EQUAL(pszFileExtension, "byn") && !EQUAL(pszFileExtension, "err")) + if (!EQUAL(osExt.c_str(), "byn") && !EQUAL(osExt.c_str(), "err")) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to create byn file with extension other than byn/err."); - CPLFree(pszFileExtension); return nullptr; } - CPLFree(pszFileExtension); - /* -------------------------------------------------------------------- */ /* Try to create the file. */ /* -------------------------------------------------------------------- */ diff --git a/frmts/raw/envidataset.cpp b/frmts/raw/envidataset.cpp index 6267cba8f087..44bc8181764b 100644 --- a/frmts/raw/envidataset.cpp +++ b/frmts/raw/envidataset.cpp @@ -2119,7 +2119,7 @@ ENVIDataset *ENVIDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck) // In case, there is no interleave keyword, we try to derive it from the // file extension. CPLString osInterleave = poDS->m_aosHeader.FetchNameValueDef( - "interleave", CPLGetExtension(poOpenInfo->pszFilename)); + "interleave", poOpenInfo->osExtension.c_str()); if (!STARTS_WITH_CI(osInterleave, "BSQ") && !STARTS_WITH_CI(osInterleave, "BIP") && diff --git a/frmts/raw/fastdataset.cpp b/frmts/raw/fastdataset.cpp index 619d3f65e7b6..e0d1b8bbe2c3 100644 --- a/frmts/raw/fastdataset.cpp +++ b/frmts/raw/fastdataset.cpp @@ -217,7 +217,7 @@ VSILFILE *FASTDataset::FOpenChannel(const char *pszBandname, int iBand, { const char *pszChannelFilename = nullptr; char *pszPrefix = CPLStrdup(CPLGetBasename(pszFilename)); - char *pszSuffix = CPLStrdup(CPLGetExtension(pszFilename)); + char *pszSuffix = CPLStrdup(CPLGetExtensionSafe(pszFilename).c_str()); fpChannels[iBand] = nullptr; @@ -663,7 +663,8 @@ GDALDataset *FASTDataset::Open(GDALOpenInfo *poOpenInfo) { // See appendix F in // http://www.euromap.de/download/p5fast_20050301.pdf - const CPLString osSuffix = CPLGetExtension(poDS->pszFilename); + const CPLString osSuffix = + CPLGetExtensionSafe(poDS->pszFilename); const char *papszBasenames[] = {"BANDF", "bandf", "BANDA", "banda"}; for (int i = 0; i < 4; i++) diff --git a/frmts/raw/lcpdataset.cpp b/frmts/raw/lcpdataset.cpp index 34294b7bc348..bcfb63256b43 100644 --- a/frmts/raw/lcpdataset.cpp +++ b/frmts/raw/lcpdataset.cpp @@ -181,7 +181,7 @@ int LCPDataset::Identify(GDALOpenInfo *poOpenInfo) /* Check file extension */ /* -------------------------------------------------------------------- */ #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - const char *pszFileExtension = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszFileExtension = poOpenInfo->osExtension.c_str(); if (!EQUAL(pszFileExtension, "lcp")) { return FALSE; diff --git a/frmts/raw/loslasdataset.cpp b/frmts/raw/loslasdataset.cpp index 51403ea14332..eeae1d80abc8 100644 --- a/frmts/raw/loslasdataset.cpp +++ b/frmts/raw/loslasdataset.cpp @@ -153,7 +153,7 @@ int LOSLASDataset::Identify(GDALOpenInfo *poOpenInfo) return FALSE; #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - const char *pszExt = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszExt = poOpenInfo->osExtension.c_str(); if (!EQUAL(pszExt, "las") && !EQUAL(pszExt, "los") && !EQUAL(pszExt, "geo")) return FALSE; #endif diff --git a/frmts/raw/mffdataset.cpp b/frmts/raw/mffdataset.cpp index b2c38a39e180..f386691ed7fb 100644 --- a/frmts/raw/mffdataset.cpp +++ b/frmts/raw/mffdataset.cpp @@ -798,8 +798,6 @@ GDALDataset *MFFDataset::Open(GDALOpenInfo *poOpenInfo) int nSkipped = 0; for (int nRawBand = 0; true; nRawBand++) { - const char *pszExtension = nullptr; - /* Find the next raw band file. */ int i = 0; // Used after for. @@ -808,11 +806,12 @@ GDALDataset *MFFDataset::Open(GDALOpenInfo *poOpenInfo) if (!EQUAL(CPLGetBasename(papszDirFiles[i]), pszTargetBase)) continue; - pszExtension = CPLGetExtension(papszDirFiles[i]); - if (strlen(pszExtension) >= 2 && - isdigit(static_cast(pszExtension[1])) && - atoi(pszExtension + 1) == nRawBand && - strchr("bBcCiIjJrRxXzZ", pszExtension[0]) != nullptr) + const std::string osExtension = + CPLGetExtensionSafe(papszDirFiles[i]); + if (osExtension.size() >= 2 && + isdigit(static_cast(osExtension[1])) && + atoi(osExtension.c_str() + 1) == nRawBand && + strchr("bBcCiIjJrRxXzZ", osExtension[0]) != nullptr) break; } @@ -840,7 +839,7 @@ GDALDataset *MFFDataset::Open(GDALOpenInfo *poOpenInfo) CSLAddString(poDS->m_papszFileList, pszRawFilename); GDALDataType eDataType = GDT_Unknown; - pszExtension = CPLGetExtension(papszDirFiles[i]); + const std::string osExt = CPLGetExtensionSafe(papszDirFiles[i]); if (pszRefinedType != nullptr) { if (EQUAL(pszRefinedType, "C*4")) @@ -888,23 +887,23 @@ GDALDataset *MFFDataset::Open(GDALOpenInfo *poOpenInfo) continue; } } - else if (STARTS_WITH_CI(pszExtension, "b")) + else if (STARTS_WITH_CI(osExt.c_str(), "b")) { eDataType = GDT_Byte; } - else if (STARTS_WITH_CI(pszExtension, "i")) + else if (STARTS_WITH_CI(osExt.c_str(), "i")) { eDataType = GDT_UInt16; } - else if (STARTS_WITH_CI(pszExtension, "j")) + else if (STARTS_WITH_CI(osExt.c_str(), "j")) { eDataType = GDT_CInt16; } - else if (STARTS_WITH_CI(pszExtension, "r")) + else if (STARTS_WITH_CI(osExt.c_str(), "r")) { eDataType = GDT_Float32; } - else if (STARTS_WITH_CI(pszExtension, "x")) + else if (STARTS_WITH_CI(osExt.c_str(), "x")) { eDataType = GDT_CFloat32; } @@ -913,7 +912,7 @@ GDALDataset *MFFDataset::Open(GDALOpenInfo *poOpenInfo) CPLError(CE_Warning, CPLE_OpenFailed, "Unable to open band %d because extension %s is not " "handled. Skipping.", - nRawBand + 1, pszExtension); + nRawBand + 1, osExt.c_str()); nSkipped++; CPL_IGNORE_RET_VAL(VSIFCloseL(fpRaw)); continue; diff --git a/frmts/raw/pnmdataset.cpp b/frmts/raw/pnmdataset.cpp index 70241d02e9a7..54b04ef17fda 100644 --- a/frmts/raw/pnmdataset.cpp +++ b/frmts/raw/pnmdataset.cpp @@ -328,7 +328,7 @@ GDALDataset *PNMDataset::Create(const char *pszFilename, int nXSize, int nYSize, return nullptr; } - const CPLString osExt(CPLGetExtension(pszFilename)); + const CPLString osExt(CPLGetExtensionSafe(pszFilename)); if (nBandsIn == 1) { if (!EQUAL(osExt, "PGM")) diff --git a/frmts/raw/roipacdataset.cpp b/frmts/raw/roipacdataset.cpp index 1943cb38a480..f9929ce88dc8 100644 --- a/frmts/raw/roipacdataset.cpp +++ b/frmts/raw/roipacdataset.cpp @@ -259,7 +259,7 @@ GDALDataset *ROIPACDataset::Open(GDALOpenInfo *poOpenInfo) PIXEL } eInterleave = UNKNOWN; - const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszExtension = poOpenInfo->osExtension.c_str(); if (strcmp(pszExtension, "raw") == 0) { /* ------------------------------------------------------------ */ @@ -506,7 +506,7 @@ int ROIPACDataset::Identify(GDALOpenInfo *poOpenInfo) /* Check if: */ /* * 1. The data file extension is known */ /* -------------------------------------------------------------------- */ - const char *pszExtension = CPLGetExtension(poOpenInfo->pszFilename); + const char *pszExtension = poOpenInfo->osExtension.c_str(); if (strcmp(pszExtension, "raw") == 0) { /* Since gdal do not read natively CInt8, more work is needed @@ -548,7 +548,8 @@ GDALDataset *ROIPACDataset::Create(const char *pszFilename, int nXSize, /* -------------------------------------------------------------------- */ /* Verify input options. */ /* -------------------------------------------------------------------- */ - const char *pszExtension = CPLGetExtension(pszFilename); + const std::string osExtension = CPLGetExtensionSafe(pszFilename); + const char *pszExtension = osExtension.c_str(); if (strcmp(pszExtension, "int") == 0 || strcmp(pszExtension, "slc") == 0) { if (nBandsIn != 1 || eType != GDT_CFloat32) diff --git a/frmts/raw/rrasterdataset.cpp b/frmts/raw/rrasterdataset.cpp index f433cc503b1c..66d0a844ade3 100644 --- a/frmts/raw/rrasterdataset.cpp +++ b/frmts/raw/rrasterdataset.cpp @@ -1178,7 +1178,7 @@ GDALDataset *RRASTERDataset::Open(GDALOpenInfo *poOpenInfo) CPLString osDirname(CPLGetDirname(poOpenInfo->pszFilename)); CPLString osBasename(CPLGetBasename(poOpenInfo->pszFilename)); - CPLString osGRDExtension(CPLGetExtension(poOpenInfo->pszFilename)); + CPLString osGRDExtension(poOpenInfo->osExtension); CPLString osGRIExtension((osGRDExtension[0] == 'g') ? "gri" : "GRI"); char **papszSiblings = poOpenInfo->GetSiblingFiles(); if (papszSiblings) @@ -1420,7 +1420,7 @@ GDALDataset *RRASTERDataset::Create(const char *pszFilename, int nXSize, return nullptr; } - CPLString osGRDExtension(CPLGetExtension(pszFilename)); + CPLString osGRDExtension(CPLGetExtensionSafe(pszFilename)); if (!EQUAL(osGRDExtension, "grd")) { CPLError(CE_Failure, CPLE_NotSupported, diff --git a/frmts/saga/sagadataset.cpp b/frmts/saga/sagadataset.cpp index 843501e2c3e2..924afc56579a 100644 --- a/frmts/saga/sagadataset.cpp +++ b/frmts/saga/sagadataset.cpp @@ -329,7 +329,7 @@ char **SAGADataset::GetFileList() // Main data file, etc. char **papszFileList = GDALPamDataset::GetFileList(); - if (!EQUAL(CPLGetExtension(GetDescription()), "sg-grd-z")) + if (!EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "sg-grd-z")) { // Header file. CPLString osFilename = CPLFormCIFilename(osPath, osName, ".sgrd"); @@ -409,16 +409,17 @@ GDALDataset *SAGADataset::Open(GDALOpenInfo *poOpenInfo) /* We assume the user is pointing to the binary (i.e. .sdat) file or a */ /* compressed raster (.sg-grd-z) file. */ /* -------------------------------------------------------------------- */ - CPLString osExtension(CPLGetExtension(poOpenInfo->pszFilename)); + const auto &osExtension = poOpenInfo->osExtension; - if (!EQUAL(osExtension, "sdat") && !EQUAL(osExtension, "sg-grd-z")) + if (!EQUAL(osExtension.c_str(), "sdat") && + !EQUAL(osExtension.c_str(), "sg-grd-z")) { return nullptr; } CPLString osPath, osFullname, osName, osHDRFilename; - if (EQUAL(osExtension, "sg-grd-z") && + if (EQUAL(osExtension.c_str(), "sg-grd-z") && !STARTS_WITH(poOpenInfo->pszFilename, "/vsizip")) { osPath = "/vsizip/{"; @@ -432,7 +433,7 @@ GDALDataset *SAGADataset::Open(GDALOpenInfo *poOpenInfo) CPLString file; for (int iFile = 0; filesinzip[iFile] != nullptr; iFile++) { - if (EQUAL(CPLGetExtension(filesinzip[iFile]), "sdat")) + if (EQUAL(CPLGetExtensionSafe(filesinzip[iFile]).c_str(), "sdat")) { file = filesinzip[iFile]; break; diff --git a/frmts/srtmhgt/srtmhgtdataset.cpp b/frmts/srtmhgt/srtmhgtdataset.cpp index cfba2c36c39c..bd53116d6a8d 100644 --- a/frmts/srtmhgt/srtmhgtdataset.cpp +++ b/frmts/srtmhgt/srtmhgtdataset.cpp @@ -183,7 +183,8 @@ double SRTMHGTRasterBand::GetNoDataValue(int *pbSuccess) const char *SRTMHGTRasterBand::GetUnitType() { - const char *pszExt = CPLGetExtension(poDS->GetDescription()); + const std::string osExt = CPLGetExtensionSafe(poDS->GetDescription()); + const char *pszExt = osExt.c_str(); if (EQUAL(pszExt, "err") || EQUAL(pszExt, "img") || EQUAL(pszExt, "num") || EQUAL(pszExt, "swb")) {