diff --git a/frmts/aaigrid/aaigriddataset.cpp b/frmts/aaigrid/aaigriddataset.cpp index 2e2ef751f31c..1b8e5dd1d0a0 100644 --- a/frmts/aaigrid/aaigriddataset.cpp +++ b/frmts/aaigrid/aaigriddataset.cpp @@ -1218,9 +1218,10 @@ GDALDataset *AAIGDataset::CommonOpen(GDALOpenInfo *poOpenInfo, } // Try to read projection file. - char *const pszDirname = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename)); + char *const pszDirname = + CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); char *const pszBasename = - CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename)); + CPLStrdup(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str()); poDS->osPrjFilename = CPLFormFilename(pszDirname, pszBasename, "prj"); int nRet = 0; @@ -1557,10 +1558,10 @@ GDALDataset *AAIGDataset::CreateCopy(const char *pszFilename, const char *pszOriginalProjection = poSrcDS->GetProjectionRef(); if (!EQUAL(pszOriginalProjection, "")) { - char *pszDirname = CPLStrdup(CPLGetPath(pszFilename)); - char *pszBasename = CPLStrdup(CPLGetBasename(pszFilename)); - char *pszPrjFilename = - CPLStrdup(CPLFormFilename(pszDirname, pszBasename, "prj")); + char *pszDirname = CPLStrdup(CPLGetPathSafe(pszFilename).c_str()); + char *pszBasename = CPLStrdup(CPLGetBasenameSafe(pszFilename).c_str()); + char *pszPrjFilename = CPLStrdup( + CPLFormFilenameSafe(pszDirname, pszBasename, "prj").c_str()); VSILFILE *fp = VSIFOpenL(pszPrjFilename, "wt"); if (fp != nullptr) { diff --git a/frmts/adrg/adrgdataset.cpp b/frmts/adrg/adrgdataset.cpp index 4100fb5d2900..cab88e22a9fe 100644 --- a/frmts/adrg/adrgdataset.cpp +++ b/frmts/adrg/adrgdataset.cpp @@ -1337,7 +1337,7 @@ char **ADRGDataset::GetGENListFromTHF(const char *pszFileName) { CPLDebug("ADRG", "Found GEN file in THF : %s", osSubFileName.c_str()); - CPLString osGENFileName(CPLGetDirname(pszFileName)); + CPLString osGENFileName(CPLGetDirnameSafe(pszFileName)); char **tokens = CSLTokenizeString2(osSubFileName.c_str(), "/\"", 0); char **ptr = tokens; @@ -1472,7 +1472,7 @@ char **ADRGDataset::GetIMGListFromGEN(const char *pszFileName, CPLDebug("ADRG", "BAD=%s", osBAD.c_str()); /* Build full IMG file name from BAD value */ - CPLString osGENDir(CPLGetDirname(pszFileName)); + CPLString osGENDir(CPLGetDirnameSafe(pszFileName)); const CPLString osFileName = CPLFormFilename(osGENDir.c_str(), osBAD.c_str(), nullptr); @@ -1704,7 +1704,7 @@ GDALDataset *ADRGDataset::Create(const char *pszFilename, int nXSize, return nullptr; } - CPLString osBaseFileName(CPLGetBasename(pszFilename)); + CPLString osBaseFileName(CPLGetBasenameSafe(pszFilename)); if (osBaseFileName.size() != 8 || osBaseFileName[6] != DIGIT_ZERO || osBaseFileName[7] != '1') { @@ -1733,7 +1733,7 @@ GDALDataset *ADRGDataset::Create(const char *pszFilename, int nXSize, return nullptr; } - CPLString osDirname(CPLGetDirname(pszFilename)); + CPLString osDirname(CPLGetDirnameSafe(pszFilename)); CPLString osTransh01THF( CPLFormFilename(osDirname.c_str(), "TRANSH01.THF", nullptr)); VSILFILE *fdTHF = VSIFOpenL(osTransh01THF.c_str(), "wb"); diff --git a/frmts/adrg/srpdataset.cpp b/frmts/adrg/srpdataset.cpp index 31ebb1feca14..20da6c48ccc7 100644 --- a/frmts/adrg/srpdataset.cpp +++ b/frmts/adrg/srpdataset.cpp @@ -1058,7 +1058,7 @@ char **SRPDataset::GetGENListFromTHF(const char *pszFileName) if (!module.Open(pszFileName, TRUE)) return papszFileNames; - CPLString osDirName(CPLGetDirname(pszFileName)); + CPLString osDirName(CPLGetDirnameSafe(pszFileName)); while (true) { @@ -1163,7 +1163,8 @@ char **SRPDataset::GetGENListFromTHF(const char *pszFileName) { if (EQUAL(CPLGetExtensionSafe(*ptrDir).c_str(), "GEN") && - EQUALN(CPLGetBasename(*ptrDir), osName, 6)) + EQUALN(CPLGetBasenameSafe(*ptrDir).c_str(), + osName, 6)) { bFound = 1; osGENFileName = CPLFormFilename( @@ -1401,7 +1402,7 @@ char **SRPDataset::GetIMGListFromGEN(const char *pszFileName, CPLDebug("SRP", "BAD=%s", osBAD.c_str()); /* Build full IMG file name from BAD value */ - const CPLString osGENDir(CPLGetDirname(pszFileName)); + const CPLString osGENDir(CPLGetDirnameSafe(pszFileName)); const CPLString osFileName = CPLFormFilename(osGENDir.c_str(), osBAD.c_str(), nullptr); diff --git a/frmts/ceos2/sar_ceosdataset.cpp b/frmts/ceos2/sar_ceosdataset.cpp index 261274a5dc6f..72d8276d73ba 100644 --- a/frmts/ceos2/sar_ceosdataset.cpp +++ b/frmts/ceos2/sar_ceosdataset.cpp @@ -1822,8 +1822,9 @@ GDALDataset *SAR_CEOSDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Try the various filenames. */ /* -------------------------------------------------------------------- */ - char *pszPath = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename)); - char *pszBasename = CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename)); + char *pszPath = CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); + char *pszBasename = + CPLStrdup(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str()); char *pszExtension = CPLStrdup(CPLGetExtensionSafe(poOpenInfo->pszFilename).c_str()); diff --git a/frmts/coasp/coasp_dataset.cpp b/frmts/coasp/coasp_dataset.cpp index 3db8b729a0cf..e3abf841272f 100644 --- a/frmts/coasp/coasp_dataset.cpp +++ b/frmts/coasp/coasp_dataset.cpp @@ -393,8 +393,9 @@ GDALDataset *COASPDataset::Open(GDALOpenInfo *poOpenInfo) poDS->pszFileName = VSIStrdup(poOpenInfo->pszFilename); /* determine the file name prefix */ - char *pszBaseName = VSIStrdup(CPLGetBasename(poDS->pszFileName)); - char *pszDir = VSIStrdup(CPLGetPath(poDS->pszFileName)); + char *pszBaseName = + VSIStrdup(CPLGetBasenameSafe(poDS->pszFileName).c_str()); + char *pszDir = VSIStrdup(CPLGetPathSafe(poDS->pszFileName).c_str()); const char *pszExt = "rc"; int nNull = static_cast(strlen(pszBaseName)) - 1; if (nNull <= 0) diff --git a/frmts/esric/esric_dataset.cpp b/frmts/esric/esric_dataset.cpp index b3b162577c61..06eaeb1c3f3f 100644 --- a/frmts/esric/esric_dataset.cpp +++ b/frmts/esric/esric_dataset.cpp @@ -684,7 +684,8 @@ GDALDataset *ECDataset::Open(GDALOpenInfo *poOpenInfo, tileBundlesPath.erase(0, 2); } - ds->dname.Printf("%s/%s", CPLGetDirname(poOpenInfo->pszFilename), + ds->dname.Printf("%s/%s", + CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), tileBundlesPath.c_str()); CPLErr error = ds->InitializeFromJSON(oRoot); if (CE_None != error) diff --git a/frmts/gti/gdaltileindexdataset.cpp b/frmts/gti/gdaltileindexdataset.cpp index 1df2d7dfac5b..808ea93aaf76 100644 --- a/frmts/gti/gdaltileindexdataset.cpp +++ b/frmts/gti/gdaltileindexdataset.cpp @@ -591,15 +591,16 @@ static std::string GetAbsoluteFileName(const char *pszTileName, const std::string osRet = CPLIsFilenameRelative(osPath.c_str()) ? oSubDSInfo->ModifyPathComponent( - CPLProjectRelativeFilename(CPLGetPath(pszVRTName), - osPath.c_str())) + CPLProjectRelativeFilename( + CPLGetPathSafe(pszVRTName).c_str(), + osPath.c_str())) : std::string(pszTileName); GDALDestroySubdatasetInfo(oSubDSInfo); return osRet; } - const std::string osRelativeMadeAbsolute = - CPLProjectRelativeFilename(CPLGetPath(pszVRTName), pszTileName); + const std::string osRelativeMadeAbsolute = CPLProjectRelativeFilename( + CPLGetPathSafe(pszVRTName).c_str(), pszTileName); VSIStatBufL sStat; if (VSIStatL(osRelativeMadeAbsolute.c_str(), &sStat) == 0) return osRelativeMadeAbsolute; diff --git a/frmts/gtiff/cogdriver.cpp b/frmts/gtiff/cogdriver.cpp index 310204a64127..867b2afe7d09 100644 --- a/frmts/gtiff/cogdriver.cpp +++ b/frmts/gtiff/cogdriver.cpp @@ -62,7 +62,8 @@ static CPLString GetTmpFilename(const char *pszFilename, const char *pszExt) if (!bSupportsRandomWrite || CPLGetConfigOption("CPL_TMPDIR", nullptr) != nullptr) { - osTmpFilename = CPLGenerateTempFilename(CPLGetBasename(pszFilename)); + osTmpFilename = + CPLGenerateTempFilename(CPLGetBasenameSafe(pszFilename).c_str()); } else osTmpFilename = pszFilename; diff --git a/frmts/gtiff/gtiffdataset_read.cpp b/frmts/gtiff/gtiffdataset_read.cpp index e327661f1bca..42f4d879020e 100644 --- a/frmts/gtiff/gtiffdataset_read.cpp +++ b/frmts/gtiff/gtiffdataset_read.cpp @@ -5771,7 +5771,7 @@ CSLConstList GTiffDataset::GetSiblingFiles() const int nMaxFiles = atoi(CPLGetConfigOption("GDAL_READDIR_LIMIT_ON_OPEN", "1000")); CPLStringList aosSiblingFiles( - VSIReadDirEx(CPLGetDirname(m_pszFilename), nMaxFiles)); + VSIReadDirEx(CPLGetDirnameSafe(m_pszFilename).c_str(), nMaxFiles)); if (nMaxFiles > 0 && aosSiblingFiles.size() > nMaxFiles) { CPLDebug("GTiff", "GDAL_READDIR_LIMIT_ON_OPEN reached on %s", diff --git a/frmts/gtiff/gtiffdataset_write.cpp b/frmts/gtiff/gtiffdataset_write.cpp index aa45e94280fc..91f2d910080b 100644 --- a/frmts/gtiff/gtiffdataset_write.cpp +++ b/frmts/gtiff/gtiffdataset_write.cpp @@ -5515,7 +5515,7 @@ TIFF *GTiffDataset::CreateLL(const char *pszFilename, int nXSize, int nYSize, CPLTestBool(CPLGetConfigOption("CHECK_DISK_FREE_SPACE", "TRUE"))) { GIntBig nFreeDiskSpace = - VSIGetDiskFreeSpace(CPLGetDirname(pszFilename)); + VSIGetDiskFreeSpace(CPLGetDirnameSafe(pszFilename).c_str()); if (nFreeDiskSpace >= 0 && nFreeDiskSpace < dfUncompressedImageSize) { ReportError(pszFilename, CE_Failure, CPLE_FileIO, diff --git a/frmts/hdf4/hdf4imagedataset.cpp b/frmts/hdf4/hdf4imagedataset.cpp index e5a6084feed8..f51cb69949f0 100644 --- a/frmts/hdf4/hdf4imagedataset.cpp +++ b/frmts/hdf4/hdf4imagedataset.cpp @@ -288,7 +288,7 @@ CPLErr HDF4ImageRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, /* to look in to find the external files. Normally this is the */ /* directory holding the hdf file. */ /* -------------------------------------------------------------------- */ - HXsetdir(CPLGetPath(poGDS->pszFilename)); + HXsetdir(CPLGetPathSafe(poGDS->pszFilename).c_str()); /* -------------------------------------------------------------------- */ /* Handle different configurations. */ diff --git a/frmts/hdf5/s100.cpp b/frmts/hdf5/s100.cpp index 86762b3a921d..4964256c7617 100644 --- a/frmts/hdf5/s100.cpp +++ b/frmts/hdf5/s100.cpp @@ -417,8 +417,9 @@ std::string S100ReadMetadata(GDALDataset *poDS, const std::string &osFilename, const char *pszVal = poAttr->ReadAsString(); if (pszVal && pszVal[0]) { - osMetadataFile = CPLFormFilename(CPLGetPath(osFilename.c_str()), - pszVal, nullptr); + osMetadataFile = + CPLFormFilename(CPLGetPathSafe(osFilename.c_str()).c_str(), + pszVal, nullptr); VSIStatBufL sStat; if (VSIStatL(osMetadataFile.c_str(), &sStat) != 0) { @@ -427,9 +428,9 @@ std::string S100ReadMetadata(GDALDataset *poDS, const std::string &osFilename, // but the actual filename does not start with "MD_"... if (STARTS_WITH(pszVal, "MD_")) { - osMetadataFile = - CPLFormFilename(CPLGetPath(osFilename.c_str()), - pszVal + strlen("MD_"), nullptr); + osMetadataFile = CPLFormFilename( + CPLGetPathSafe(osFilename.c_str()).c_str(), + pszVal + strlen("MD_"), nullptr); if (VSIStatL(osMetadataFile.c_str(), &sStat) != 0) { osMetadataFile.clear(); diff --git a/frmts/hfa/hfaband.cpp b/frmts/hfa/hfaband.cpp index e5a6e6707412..3ec2a23fb5a7 100644 --- a/frmts/hfa/hfaband.cpp +++ b/frmts/hfa/hfaband.cpp @@ -174,10 +174,10 @@ CPLErr HFABand::LoadOverviews() if (psHFA == nullptr) { char *pszBasename = - CPLStrdup(CPLGetBasename(psInfo->pszFilename)); + CPLStrdup(CPLGetBasenameSafe(psInfo->pszFilename).c_str()); - pszJustFilename = - CPLStrdup(CPLFormFilename(nullptr, pszBasename, "rrd")); + pszJustFilename = CPLStrdup( + CPLFormFilenameSafe(nullptr, pszBasename, "rrd").c_str()); CPLDebug("HFA", "Failed to find overview file with " "expected name, try %s instead.", diff --git a/frmts/hfa/hfaopen.cpp b/frmts/hfa/hfaopen.cpp index c1779e4c4555..a9514f00a059 100644 --- a/frmts/hfa/hfaopen.cpp +++ b/frmts/hfa/hfaopen.cpp @@ -201,7 +201,7 @@ HFAHandle HFAOpen(const char *pszFilename, const char *pszAccess) static_cast(CPLCalloc(sizeof(HFAInfo_t), 1)); psInfo->pszFilename = CPLStrdup(CPLGetFilename(pszFilename)); - psInfo->pszPath = CPLStrdup(CPLGetPath(pszFilename)); + psInfo->pszPath = CPLStrdup(CPLGetPathSafe(pszFilename).c_str()); psInfo->fp = fp; if (EQUAL(pszAccess, "r") || EQUAL(pszAccess, "rb")) psInfo->eAccess = HFA_ReadOnly; @@ -340,8 +340,8 @@ HFAInfo_t *HFAGetDependent(HFAInfo_t *psBase, const char *pszFilename) // Try to open the dependent file. const char *pszMode = psBase->eAccess == HFA_Update ? "r+b" : "rb"; - char *pszDependent = - CPLStrdup(CPLFormFilename(psBase->pszPath, pszFilename, nullptr)); + char *pszDependent = CPLStrdup( + CPLFormFilenameSafe(psBase->pszPath, pszFilename, nullptr).c_str()); VSILFILE *fp = VSIFOpenL(pszDependent, pszMode); if (fp != nullptr) @@ -1808,7 +1808,7 @@ HFAHandle HFACreateLL(const char *pszFilename) psInfo->pProParameters = nullptr; psInfo->bTreeDirty = false; psInfo->pszFilename = CPLStrdup(CPLGetFilename(pszFilename)); - psInfo->pszPath = CPLStrdup(CPLGetPath(pszFilename)); + psInfo->pszPath = CPLStrdup(CPLGetPathSafe(pszFilename).c_str()); // Write out the Ehfa_HeaderTag. bool bRet = VSIFWriteL((void *)"EHFA_HEADER_TAG", 1, 16, fp) > 0; @@ -2957,14 +2957,14 @@ bool HFACreateSpillStack(HFAInfo_t *psInfo, int nXSize, int nYSize, int nLayers, { const auto osExt = CPLGetExtensionSafe(psInfo->pszFilename); if (EQUAL(osExt.c_str(), "rrd")) - psInfo->pszIGEFilename = - CPLStrdup(CPLResetExtension(psInfo->pszFilename, "rde")); + psInfo->pszIGEFilename = CPLStrdup( + CPLResetExtensionSafe(psInfo->pszFilename, "rde").c_str()); else if (EQUAL(osExt.c_str(), "aux")) - psInfo->pszIGEFilename = - CPLStrdup(CPLResetExtension(psInfo->pszFilename, "axe")); + psInfo->pszIGEFilename = CPLStrdup( + CPLResetExtensionSafe(psInfo->pszFilename, "axe").c_str()); else - psInfo->pszIGEFilename = - CPLStrdup(CPLResetExtension(psInfo->pszFilename, "ige")); + psInfo->pszIGEFilename = CPLStrdup( + CPLResetExtensionSafe(psInfo->pszFilename, "ige").c_str()); } char *pszFullFilename = CPLStrdup( diff --git a/frmts/http/httpdriver.cpp b/frmts/http/httpdriver.cpp index 3b15354e9543..aebab228e5b1 100644 --- a/frmts/http/httpdriver.cpp +++ b/frmts/http/httpdriver.cpp @@ -174,7 +174,8 @@ static GDALDataset *HTTPOpen(GDALOpenInfo *poOpenInfo) CPLString osTempFilename; #ifdef _WIN32 - const char *pszPath = CPLGetPath(CPLGenerateTempFilename(NULL)); + const char *pszPath = + CPLGetPath(CPLGenerateTempFilenameSafe(NULL).c_str()); #else const char *pszPath = "/tmp"; #endif diff --git a/frmts/idrisi/IdrisiDataset.cpp b/frmts/idrisi/IdrisiDataset.cpp index 54a6a1c55061..82088484792b 100644 --- a/frmts/idrisi/IdrisiDataset.cpp +++ b/frmts/idrisi/IdrisiDataset.cpp @@ -2393,8 +2393,9 @@ CPLErr IdrisiGeoReference2Wkt(const char *pszFilename, const char *pszRefSystem, // Search for georeference file .ref // ------------------------------------------------------------------ - const char *pszFName = CPLSPrintf("%s%c%s.ref", CPLGetDirname(pszFilename), - PATHDELIM, pszRefSystem); + const char *pszFName = + CPLSPrintf("%s%c%s.ref", CPLGetDirnameSafe(pszFilename).c_str(), + PATHDELIM, pszRefSystem); if (!FileExists(pszFName)) { @@ -3129,10 +3130,10 @@ CPLErr IdrisiDataset::Wkt2GeoReference(const OGRSpatialReference &oSRS, papszRef = CSLAddNameValue(papszRef, refSTANDL_2, CPLSPrintf("%.9g", dfStdP2)); myCSLSetNameValueSeparator(papszRef, ": "); - SaveAsCRLF(papszRef, CPLResetExtension(pszFilename, extREF)); + SaveAsCRLF(papszRef, CPLResetExtensionSafe(pszFilename, extREF).c_str()); CSLDestroy(papszRef); - *pszRefSystem = CPLStrdup(CPLGetBasename(pszFilename)); + *pszRefSystem = CPLStrdup(CPLGetBasenameSafe(pszFilename).c_str()); *pszRefUnit = CPLStrdup(pszLinearUnit); CPLFree(pszGeorefName); diff --git a/frmts/ilwis/ilwiscoordinatesystem.cpp b/frmts/ilwis/ilwiscoordinatesystem.cpp index 56f4aa95bddf..11b99bb544c0 100644 --- a/frmts/ilwis/ilwiscoordinatesystem.cpp +++ b/frmts/ilwis/ilwiscoordinatesystem.cpp @@ -996,8 +996,8 @@ CPLErr ILWISDataset::WriteProjection() OGRSpatialReference *poGeogSRS = nullptr; std::string csFileName = CPLResetExtension(osFileName, "csy"); - std::string pszBaseName = std::string(CPLGetBasename(osFileName)); - // std::string pszPath = std::string(CPLGetPath( osFileName )); + std::string pszBaseName = std::string(CPLGetBasenameSafe(osFileName)); + // std::string pszPath = std::string(CPLGetPathSafe( osFileName )); const bool bHaveSRS = !m_oSRS.IsEmpty(); const IlwisDatums *piwDatum = iwDatums; diff --git a/frmts/ilwis/ilwisdataset.cpp b/frmts/ilwis/ilwisdataset.cpp index d2e2e5047d92..3f74ef4b4f75 100644 --- a/frmts/ilwis/ilwisdataset.cpp +++ b/frmts/ilwis/ilwisdataset.cpp @@ -496,8 +496,9 @@ void ILWISDataset::CollectTransformCoef(std::string &pszRefName) if (!georef.empty() && !EQUAL(georef.c_str(), "none")) { // Form the geo-referencing name - std::string osBaseName = std::string(CPLGetBasename(georef.c_str())); - std::string osPath = std::string(CPLGetPath(osFileName)); + std::string osBaseName = + std::string(CPLGetBasenameSafe(georef.c_str())); + std::string osPath = std::string(CPLGetPathSafe(osFileName)); pszRefName = std::string( CPLFormFilename(osPath.c_str(), osBaseName.c_str(), "grf")); @@ -583,8 +584,8 @@ void ILWISDataset::WriteGeoReference() // Re-write the GeoRef property to raster ODF // Form band file name - std::string sBaseName = std::string(CPLGetBasename(osFileName)); - std::string sPath = std::string(CPLGetPath(osFileName)); + std::string sBaseName = std::string(CPLGetBasenameSafe(osFileName)); + std::string sPath = std::string(CPLGetPathSafe(osFileName)); if (nBands == 1) { WriteElement("Map", "GeoRef", osFileName, sBaseName + ".grf"); @@ -705,8 +706,9 @@ GDALDataset *ILWISDataset::Open(GDALOpenInfo *poOpenInfo) const std::string maptype = ReadElement("BaseMap", "Type", poOpenInfo->pszFilename); // const std::string sBaseName = - // std::string(CPLGetBasename(poOpenInfo->pszFilename) ); - const std::string sPath = std::string(CPLGetPath(poOpenInfo->pszFilename)); + // std::string(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str() ); + const std::string sPath = + std::string(CPLGetPathSafe(poOpenInfo->pszFilename)); // Verify whether it is a map list or a map if (EQUAL(ilwistype.c_str(), "MapList")) @@ -724,9 +726,9 @@ GDALDataset *ILWISDataset::Open(GDALOpenInfo *poOpenInfo) std::string sBandName = ReadElement( "MapList", std::string(cBandName), poOpenInfo->pszFilename); std::string pszBandBaseName = - std::string(CPLGetBasename(sBandName.c_str())); + std::string(CPLGetBasenameSafe(sBandName.c_str())); std::string pszBandPath = - std::string(CPLGetPath(sBandName.c_str())); + std::string(CPLGetPathSafe(sBandName.c_str())); if (pszBandPath.empty()) { sBandName = std::string(CPLFormFilename( @@ -833,8 +835,9 @@ GDALDataset *ILWISDataset::Open(GDALOpenInfo *poOpenInfo) !(STARTS_WITH_CI(csy.c_str(), "LatlonWGS84.csy"))) { std::string osBaseName = - std::string(CPLGetBasename(csy.c_str())); - std::string osPath = std::string(CPLGetPath(poDS->osFileName)); + std::string(CPLGetBasenameSafe(csy.c_str())); + std::string osPath = + std::string(CPLGetPathSafe(poDS->osFileName)); csy = std::string( CPLFormFilename(osPath.c_str(), osBaseName.c_str(), "csy")); pszProj = ReadElement("CoordSystem", "Type", csy); @@ -924,8 +927,8 @@ GDALDataset *ILWISDataset::Create(const char *pszFilename, int nXSize, EQUAL(sStoreType.c_str(), "float")) stepsize = 0; - const std::string osBaseName = std::string(CPLGetBasename(pszFilename)); - const std::string osPath = std::string(CPLGetPath(pszFilename)); + const std::string osBaseName = std::string(CPLGetBasenameSafe(pszFilename)); + const std::string osPath = std::string(CPLGetPathSafe(pszFilename)); /* -------------------------------------------------------------------- */ /* Write out object definition file for each band */ @@ -1094,8 +1097,8 @@ GDALDataset *ILWISDataset::CreateCopy(const char *pszFilename, if (poDS == nullptr) return nullptr; - const std::string osBaseName = std::string(CPLGetBasename(pszFilename)); - const std::string osPath = std::string(CPLGetPath(pszFilename)); + const std::string osBaseName = std::string(CPLGetBasenameSafe(pszFilename)); + const std::string osPath = std::string(CPLGetPathSafe(pszFilename)); /* -------------------------------------------------------------------- */ /* Copy and geo-transform and projection information. */ @@ -1166,7 +1169,7 @@ GDALDataset *ILWISDataset::CreateCopy(const char *pszFilename, snprintf(szName, sizeof(szName), "%s_band_%d", osBaseName.c_str(), iBand + 1); pszODFName = - std::string(CPLFormFilename(osPath.c_str(), szName, "mpr")); + std::string(CPLFormFilenameSafe(osPath.c_str(), szName, "mpr")); // pszDataBaseName = std::string(szName); } /* -------------------------------------------------------------------- @@ -1324,10 +1327,11 @@ ILWISRasterBand::ILWISRasterBand(ILWISDataset *poDSIn, int nBandIn, { sBandName = sBandNameIn; } - std::string sInputPath = std::string(CPLGetPath(poDSIn->osFileName)); - std::string sBandPath = std::string(CPLGetPath(sBandName.c_str())); + std::string sInputPath = + std::string(CPLGetPathSafe(poDSIn->osFileName)); + std::string sBandPath = std::string(CPLGetPathSafe(sBandName.c_str())); std::string sBandBaseName = - std::string(CPLGetBasename(sBandName.c_str())); + std::string(CPLGetBasenameSafe(sBandName.c_str())); if (sBandPath.empty()) sBandName = std::string(CPLFormFilename( sInputPath.c_str(), sBandBaseName.c_str(), "mpr")); @@ -1396,7 +1400,7 @@ void ILWISRasterBand::ILWISOpen(const std::string &pszFileName) { ILWISDataset *dataset = (ILWISDataset *)poDS; std::string pszDataFile = - std::string(CPLResetExtension(pszFileName.c_str(), "mp#")); + std::string(CPLResetExtensionSafe(pszFileName.c_str(), "mp#")); fpRaw = VSIFOpenL(pszDataFile.c_str(), (dataset->eAccess == GA_Update) ? "rb+" : "rb"); @@ -1482,8 +1486,9 @@ CPLErr ILWISRasterBand::GetILWISInfo(const std::string &pszFileName) const std::string domName = ReadElement("BaseMap", "Domain", pszFileName.c_str()); - const std::string osBaseName = std::string(CPLGetBasename(domName.c_str())); - const std::string osPath = std::string(CPLGetPath(pszFileName.c_str())); + const std::string osBaseName = + std::string(CPLGetBasenameSafe(domName.c_str())); + const std::string osPath = std::string(CPLGetPathSafe(pszFileName.c_str())); // Check against all "system-domains" if (EQUAL(osBaseName.c_str(), diff --git a/frmts/jaxapalsar/jaxapalsardataset.cpp b/frmts/jaxapalsar/jaxapalsardataset.cpp index a7b7480a137e..e6be1eee7173 100644 --- a/frmts/jaxapalsar/jaxapalsardataset.cpp +++ b/frmts/jaxapalsar/jaxapalsardataset.cpp @@ -578,7 +578,8 @@ GDALDataset *PALSARJaxaDataset::Open(GDALOpenInfo *poOpenInfo) /* Try to read each of the polarizations */ const size_t nImgFileLen = - strlen(CPLGetDirname(poOpenInfo->pszFilename)) + strlen(pszSuffix) + 8; + strlen(CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str()) + + strlen(pszSuffix) + 8; char *pszImgFile = (char *)CPLMalloc(nImgFileLen); int nBandNum = 1; @@ -650,7 +651,8 @@ GDALDataset *PALSARJaxaDataset::Open(GDALOpenInfo *poOpenInfo) /* read metadata from Leader file. */ const size_t nLeaderFilenameLen = - strlen(CPLGetDirname(poOpenInfo->pszFilename)) + strlen(pszSuffix) + 5; + strlen(CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str()) + + strlen(pszSuffix) + 5; char *pszLeaderFilename = (char *)CPLMalloc(nLeaderFilenameLen); snprintf(pszLeaderFilename, nLeaderFilenameLen, "%s%sLED%s", CPLGetDirname(poOpenInfo->pszFilename), SEP_STRING, pszSuffix); diff --git a/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp b/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp index 909946eb96b1..b3a68d456ed0 100644 --- a/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp +++ b/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp @@ -582,7 +582,7 @@ KmlSuperOverlayCreateCopy(const char *pszFilename, GDALDataset *poSrcDS, isKmz = true; } - output_dir = CPLStrdup(CPLGetPath(pszFilename)); + output_dir = CPLStrdup(CPLGetPathSafe(pszFilename).c_str()); if (strcmp(output_dir, "") == 0) { CPLFree(output_dir); @@ -1760,8 +1760,8 @@ KmlSuperOverlayLoadIcon(const char *pszBaseFilename, const char *pszIcon) osSubFilename = CPLSPrintf("/vsicurl_streaming/%s", pszIcon); else { - osSubFilename = - CPLFormFilename(CPLGetPath(pszBaseFilename), pszIcon, nullptr); + osSubFilename = CPLFormFilename(CPLGetPathSafe(pszBaseFilename).c_str(), + pszIcon, nullptr); osSubFilename = KMLRemoveSlash(osSubFilename); } @@ -2526,7 +2526,7 @@ GDALDataset *KmlSingleOverlayRasterDataset::Open(const char *pszFilename, if (!KmlSuperOverlayGetBoundingBox(psGO, adfExtents)) return nullptr; const char *pszImageFilename = - CPLFormFilename(CPLGetPath(osFilename), pszHref, nullptr); + CPLFormFilename(CPLGetPathSafe(osFilename).c_str(), pszHref, nullptr); GDALDataset *poImageDS = GDALDataset::FromHandle(GDALOpenShared(pszImageFilename, GA_ReadOnly)); if (poImageDS == nullptr) @@ -2663,8 +2663,8 @@ KmlSuperOverlayReadDataset::Open(const char *pszFilename, osSubFilename = CPLSPrintf("/vsicurl_streaming/%s", pszHref); else { - osSubFilename = - CPLFormFilename(CPLGetPath(osFilename), pszHref, nullptr); + osSubFilename = CPLFormFilename(CPLGetPathSafe(osFilename).c_str(), + pszHref, nullptr); osSubFilename = KMLRemoveSlash(osSubFilename); } @@ -2675,7 +2675,8 @@ KmlSuperOverlayReadDataset::Open(const char *pszFilename, const char *pszOverlayName = CPLGetXMLValue(psDocument, "name", nullptr); if (pszOverlayName != nullptr && - strcmp(pszOverlayName, CPLGetBasename(pszFilename)) != 0) + strcmp(pszOverlayName, + CPLGetBasenameSafe(pszFilename).c_str()) != 0) { osOverlayName = pszOverlayName; } diff --git a/frmts/mbtiles/mbtilesdataset.cpp b/frmts/mbtiles/mbtilesdataset.cpp index 5365de838372..3a5db1709e42 100644 --- a/frmts/mbtiles/mbtilesdataset.cpp +++ b/frmts/mbtiles/mbtilesdataset.cpp @@ -3054,8 +3054,8 @@ bool MBTilesDataset::CreateInternal(const char *pszFilename, int nXSize, return false; } - const char *pszName = - CSLFetchNameValueDef(papszOptions, "NAME", CPLGetBasename(pszFilename)); + const char *pszName = CSLFetchNameValueDef( + papszOptions, "NAME", CPLGetBasenameSafe(pszFilename).c_str()); char *pszSQL = sqlite3_mprintf( "INSERT INTO metadata (name, value) VALUES ('name', '%q')", pszName); sqlite3_exec(hDB, pszSQL, nullptr, nullptr, nullptr); @@ -3068,7 +3068,7 @@ bool MBTilesDataset::CreateInternal(const char *pszFilename, int nXSize, sqlite3_free(pszSQL); const char *pszDescription = CSLFetchNameValueDef( - papszOptions, "DESCRIPTION", CPLGetBasename(pszFilename)); + papszOptions, "DESCRIPTION", CPLGetBasenameSafe(pszFilename).c_str()); pszSQL = sqlite3_mprintf( "INSERT INTO metadata (name, value) VALUES ('description', '%q')", pszDescription); diff --git a/frmts/mrf/marfa_dataset.cpp b/frmts/mrf/marfa_dataset.cpp index 6af454ae1702..419eeb2d8792 100644 --- a/frmts/mrf/marfa_dataset.cpp +++ b/frmts/mrf/marfa_dataset.cpp @@ -617,7 +617,7 @@ GDALDataset *MRFDataset::Open(GDALOpenInfo *poOpenInfo) else if (poOpenInfo->eAccess == GA_ReadOnly && fn.size() > 600 && (fn[262] == 0 || fn[262] == 32) && STARTS_WITH(fn.c_str() + 257, "ustar") && - strlen(CPLGetPath(fn.c_str())) == 0 && + strlen(CPLGetPathSafe(fn.c_str()).c_str()) == 0 && STARTS_WITH(fn.c_str() + 512, "")) { // An MRF inside a tar insidefn = string("/vsitar/") + pszFileName + "/" + pszHeader; diff --git a/frmts/mrf/mrfdrivercore.cpp b/frmts/mrf/mrfdrivercore.cpp index dcfdad5ade58..8bf987e9baa3 100644 --- a/frmts/mrf/mrfdrivercore.cpp +++ b/frmts/mrf/mrfdrivercore.cpp @@ -100,7 +100,8 @@ int MRFDriverIdentify(GDALOpenInfo *poOpenInfo) // accept a tar file if the first file has no folder look like an MRF if (poOpenInfo->eAccess == GA_ReadOnly && fn.size() > 600 && (fn[262] == 0 || fn[262] == 32) && STARTS_WITH(fn + 257, "ustar") && - strlen(CPLGetPath(fn)) == 0 && STARTS_WITH(fn + 512, "")) + strlen(CPLGetPathSafe(fn).c_str()) == 0 && + STARTS_WITH(fn + 512, "")) { return TRUE; } diff --git a/frmts/netcdf/netcdf_sentinel3_sral_mwr.cpp b/frmts/netcdf/netcdf_sentinel3_sral_mwr.cpp index 76fe2e80332c..0dc0514ea1f3 100644 --- a/frmts/netcdf/netcdf_sentinel3_sral_mwr.cpp +++ b/frmts/netcdf/netcdf_sentinel3_sral_mwr.cpp @@ -461,7 +461,7 @@ void netCDFDataset::ProcessSentinel3_SRAL_MWR() NCDF_ERR(status); if (status != NC_NOERR) break; - std::string name(CPLGetBasename(GetDescription())); + std::string name(CPLGetBasenameSafe(GetDescription())); name += '_'; name += szDimName; std::shared_ptr poLayer( diff --git a/frmts/netcdf/netcdfdataset.cpp b/frmts/netcdf/netcdfdataset.cpp index dfe3cd2476c8..7c6f33154612 100644 --- a/frmts/netcdf/netcdfdataset.cpp +++ b/frmts/netcdf/netcdfdataset.cpp @@ -11800,7 +11800,8 @@ static CPLErr NCDFOpenSubDataset(int nCdfId, const char *pszSubdatasetName, *pnVarId = -1; // Open group. - char *pszGroupFullName = CPLStrdup(CPLGetPath(pszSubdatasetName)); + char *pszGroupFullName = + CPLStrdup(CPLGetPathSafe(pszSubdatasetName).c_str()); // Add a leading slash if needed. if (pszGroupFullName[0] != '/') { @@ -12372,7 +12373,7 @@ CPLErr netCDFDataset::CreateGrpVectorLayers( if (pszGroupName == nullptr || pszGroupName[0] == '\0') { CPLFree(pszGroupName); - pszGroupName = CPLStrdup(CPLGetBasename(osFilename)); + pszGroupName = CPLStrdup(CPLGetBasenameSafe(osFilename).c_str()); } OGRwkbGeometryType eGType = wkbUnknown; CPLString osLayerName = CSLFetchNameValueDef( diff --git a/frmts/netcdf/netcdflayersg.cpp b/frmts/netcdf/netcdflayersg.cpp index e1fda6024423..860dbd545c98 100644 --- a/frmts/netcdf/netcdflayersg.cpp +++ b/frmts/netcdf/netcdflayersg.cpp @@ -393,5 +393,5 @@ OGRFeature *netCDFLayer::buildSGeometryFeature(size_t featureInd) std::string netCDFDataset::generateLogName() { - return std::string(CPLGenerateTempFilename(nullptr)); + return std::string(CPLGenerateTempFilenameSafe(nullptr)); } diff --git a/frmts/ngsgeoid/ngsgeoiddataset.cpp b/frmts/ngsgeoid/ngsgeoiddataset.cpp index f530eb9b81bc..6d3d424bd742 100644 --- a/frmts/ngsgeoid/ngsgeoiddataset.cpp +++ b/frmts/ngsgeoid/ngsgeoiddataset.cpp @@ -376,7 +376,7 @@ const OGRSpatialReference *NGSGEOIDDataset::GetSpatialRef() const } const CPLString osFilename = - CPLString(CPLGetBasename(GetDescription())).tolower(); + CPLString(CPLGetBasenameSafe(GetDescription())).tolower(); // See https://www.ngs.noaa.gov/GEOID/GEOID12B/faq_2012B.shtml diff --git a/frmts/nitf/rpftocfile.cpp b/frmts/nitf/rpftocfile.cpp index eea441c12a18..02ae07c64c9a 100644 --- a/frmts/nitf/rpftocfile.cpp +++ b/frmts/nitf/rpftocfile.cpp @@ -661,7 +661,7 @@ RPFToc *RPFTOCReadFromBuffer(const char *pszFilename, VSILFILE *fp, // Check if it was not intended to be "./X/" instead. VSIStatBufL sStatBuf; if (frameEntry->directory[0] == '/' && - VSIStatL(CPLFormFilename(CPLGetDirname(pszFilename), + VSIStatL(CPLFormFilename(CPLGetDirnameSafe(pszFilename).c_str(), frameEntry->directory + 1, nullptr), &sStatBuf) == 0 && VSI_ISDIR(sStatBuf.st_mode)) @@ -672,7 +672,7 @@ RPFToc *RPFTOCReadFromBuffer(const char *pszFilename, VSILFILE *fp, } { - char *baseDir = CPLStrdup(CPLGetDirname(pszFilename)); + char *baseDir = CPLStrdup(CPLGetDirnameSafe(pszFilename).c_str()); VSIStatBufL sStatBuf; char *subdir = nullptr; if (CPLIsFilenameRelative(frameEntry->directory) == FALSE) diff --git a/frmts/northwood/grddataset.cpp b/frmts/northwood/grddataset.cpp index 6d0a4d0840ea..1755828600e2 100644 --- a/frmts/northwood/grddataset.cpp +++ b/frmts/northwood/grddataset.cpp @@ -792,7 +792,7 @@ int NWT_GRDDataset::UpdateHeader() int NWT_GRDDataset::WriteTab() { // Create the filename for the .tab file. - const std::string sTabFile(CPLResetExtension(pGrd->szFileName, "tab")); + const std::string sTabFile(CPLResetExtensionSafe(pGrd->szFileName, "tab")); VSILFILE *tabfp = VSIFOpenL(sTabFile.c_str(), "wt"); if (tabfp == nullptr) diff --git a/frmts/pds/isis3dataset.cpp b/frmts/pds/isis3dataset.cpp index 77cfcb042581..3cac34265d4a 100644 --- a/frmts/pds/isis3dataset.cpp +++ b/frmts/pds/isis3dataset.cpp @@ -1628,9 +1628,9 @@ GDALDataset *ISIS3Dataset::Open(GDALOpenInfo *poOpenInfo) if (oFilename.GetType() == CPLJSONObject::Type::String) { VSIStatBufL sStat; - CPLString osFilename( - CPLFormFilename(CPLGetPath(poOpenInfo->pszFilename), - oFilename.ToString().c_str(), nullptr)); + CPLString osFilename(CPLFormFilename( + CPLGetPathSafe(poOpenInfo->pszFilename).c_str(), + oFilename.ToString().c_str(), nullptr)); if (VSIStatL(osFilename, &sStat) == 0) { poDS->m_aosAdditionalFiles.AddString(osFilename); @@ -1682,8 +1682,8 @@ GDALDataset *ISIS3Dataset::Open(GDALOpenInfo *poOpenInfo) const CPLString osQubeFile( EQUAL(pszCore, "") ? poOpenInfo->pszFilename - : CPLFormFilename(CPLGetPath(poOpenInfo->pszFilename), pszCore, - nullptr)); + : CPLFormFilename(CPLGetPathSafe(poOpenInfo->pszFilename).c_str(), + pszCore, nullptr)); if (!EQUAL(pszCore, "")) { poDS->m_osExternalFilename = osQubeFile; @@ -3039,7 +3039,7 @@ void ISIS3Dataset::BuildLabel() oHistory.Add("Bytes", static_cast(m_osHistory.size())); if (!m_osExternalFilename.empty()) { - CPLString osFilename(CPLGetBasename(GetDescription())); + CPLString osFilename(CPLGetBasenameSafe(GetDescription())); osFilename += ".History.IsisCube"; oHistory.Add("^History", osFilename); } @@ -3117,7 +3117,7 @@ void ISIS3Dataset::BuildLabel() { VSIStatBufL sStat; const CPLString osSrcFilename( - CPLFormFilename(CPLGetPath(osLabelSrcFilename), + CPLFormFilename(CPLGetPathSafe(osLabelSrcFilename).c_str(), oFilenameCap.ToString().c_str(), nullptr)); if (VSIStatL(osSrcFilename, &sStat) == 0) { @@ -3151,7 +3151,7 @@ void ISIS3Dataset::BuildLabel() if (!m_osExternalFilename.empty()) { - CPLString osDstFilename(CPLGetBasename(GetDescription())); + CPLString osDstFilename(CPLGetBasenameSafe(GetDescription())); osDstFilename += "."; osDstFilename += osContainerName; if (!osName.empty()) @@ -3275,8 +3275,8 @@ void ISIS3Dataset::BuildHistory() char szFullFilename[2048] = {0}; if (!CPLGetExecPath(szFullFilename, sizeof(szFullFilename) - 1)) strcpy(szFullFilename, "unknown_program"); - const CPLString osProgram(CPLGetBasename(szFullFilename)); - const CPLString osPath(CPLGetPath(szFullFilename)); + const CPLString osProgram(CPLGetBasenameSafe(szFullFilename)); + const CPLString osPath(CPLGetPathSafe(szFullFilename)); CPLJSONObject oObj; oHistoryObj.Add(osProgram, oObj); @@ -3521,10 +3521,10 @@ void ISIS3Dataset::WriteLabel() } else { - CPLString osFilename(CPLGetBasename(GetDescription())); + CPLString osFilename(CPLGetBasenameSafe(GetDescription())); osFilename += ".History.IsisCube"; - osFilename = CPLFormFilename(CPLGetPath(GetDescription()), - osFilename, nullptr); + osFilename = CPLFormFilename( + CPLGetPathSafe(GetDescription()).c_str(), osFilename, nullptr); VSILFILE *fp = VSIFOpenL(osFilename, "wb"); if (fp) { diff --git a/frmts/pds/pds4dataset.cpp b/frmts/pds/pds4dataset.cpp index a65a9ce15cb0..829c443c5cc8 100644 --- a/frmts/pds/pds4dataset.cpp +++ b/frmts/pds/pds4dataset.cpp @@ -1432,7 +1432,7 @@ static CPLString FixupTableFilename(const CPLString &osFilename) bool PDS4Dataset::OpenTableCharacter(const char *pszFilename, const CPLXMLNode *psTable) { - CPLString osLayerName(CPLGetBasename(pszFilename)); + CPLString osLayerName(CPLGetBasenameSafe(pszFilename)); CPLString osFullFilename = FixupTableFilename(CPLFormFilename( CPLGetPath(m_osXMLFilename.c_str()), pszFilename, nullptr)); std::unique_ptr poLayer( @@ -1454,7 +1454,7 @@ bool PDS4Dataset::OpenTableCharacter(const char *pszFilename, bool PDS4Dataset::OpenTableBinary(const char *pszFilename, const CPLXMLNode *psTable) { - CPLString osLayerName(CPLGetBasename(pszFilename)); + CPLString osLayerName(CPLGetBasenameSafe(pszFilename)); CPLString osFullFilename = FixupTableFilename(CPLFormFilename( CPLGetPath(m_osXMLFilename.c_str()), pszFilename, nullptr)); std::unique_ptr poLayer( @@ -1476,7 +1476,7 @@ bool PDS4Dataset::OpenTableBinary(const char *pszFilename, bool PDS4Dataset::OpenTableDelimited(const char *pszFilename, const CPLXMLNode *psTable) { - CPLString osLayerName(CPLGetBasename(pszFilename)); + CPLString osLayerName(CPLGetBasenameSafe(pszFilename)); CPLString osFullFilename = FixupTableFilename(CPLFormFilename( CPLGetPath(m_osXMLFilename.c_str()), pszFilename, nullptr)); std::unique_ptr poLayer( @@ -4168,8 +4168,9 @@ OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName, CPLString osFullFilename; if (bSameDirectory) { - osFullFilename = CPLFormFilename(CPLGetPath(m_osXMLFilename.c_str()), - osBasename.c_str(), pszExt); + osFullFilename = + CPLFormFilename(CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(), + osBasename.c_str(), pszExt); VSIStatBufL sStat; if (VSIStatL(osFullFilename, &sStat) == 0) { @@ -4183,7 +4184,7 @@ OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName, else { CPLString osDirectory = - CPLFormFilename(CPLGetPath(m_osXMLFilename), + CPLFormFilename(CPLGetPathSafe(m_osXMLFilename).c_str(), CPLGetBasename(m_osXMLFilename), nullptr); VSIStatBufL sStat; if (VSIStatL(osDirectory, &sStat) != 0 && @@ -4370,7 +4371,8 @@ PDS4Dataset *PDS4Dataset::CreateInternal(const char *pszFilename, const char *pszImageExtension = aosOptions.FetchNameValueDef( "IMAGE_EXTENSION", EQUAL(pszImageFormat, "RAW") ? "img" : "tif"); CPLString osImageFilename(aosOptions.FetchNameValueDef( - "IMAGE_FILENAME", CPLResetExtension(pszFilename, pszImageExtension))); + "IMAGE_FILENAME", + CPLResetExtensionSafe(pszFilename, pszImageExtension).c_str())); const bool bAppend = aosOptions.FetchBool("APPEND_SUBDATASET", false); if (bAppend) diff --git a/frmts/plmosaic/plmosaicdataset.cpp b/frmts/plmosaic/plmosaicdataset.cpp index 3026e35d986b..f0b3c33a7cb3 100644 --- a/frmts/plmosaic/plmosaicdataset.cpp +++ b/frmts/plmosaic/plmosaicdataset.cpp @@ -328,7 +328,7 @@ PLMosaicDataset::PLMosaicDataset() adfGeoTransform[5] = 1; SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE"); - osCachePathRoot = CPLGetPath(CPLGenerateTempFilename("")); + osCachePathRoot = CPLGetPath(CPLGenerateTempFilenameSafe("").c_str()); } /************************************************************************/ diff --git a/frmts/prf/phprfdataset.cpp b/frmts/prf/phprfdataset.cpp index 4f54b4297b2a..bffdd5cbf3f6 100644 --- a/frmts/prf/phprfdataset.cpp +++ b/frmts/prf/phprfdataset.cpp @@ -385,7 +385,7 @@ GDALDataset *PhPrfDataset::Open(GDALOpenInfo *poOpenInfo) int nSizeY = 0; int nBandCount = 0; GDALDataType eResultDatatype = GDT_Unknown; - CPLString osPartsBasePath(CPLGetPath(poOpenInfo->pszFilename)); + CPLString osPartsBasePath(CPLGetPathSafe(poOpenInfo->pszFilename)); CPLString osPartsPath(osPartsBasePath + "/" + CPLGetBasename(poOpenInfo->pszFilename)); CPLString osPartsExt; diff --git a/frmts/raw/ehdrdataset.cpp b/frmts/raw/ehdrdataset.cpp index 816a4c7a0fa6..2fbb1703b44a 100644 --- a/frmts/raw/ehdrdataset.cpp +++ b/frmts/raw/ehdrdataset.cpp @@ -833,13 +833,14 @@ CPLString EHdrDataset::GetImageRepFilename(const char *pszFilename) if (EQUAL(CPLGetFilename(pszFilename), "imspatio.bil") || EQUAL(CPLGetFilename(pszFilename), "haspatio.bil")) { - CPLString osImageRepFilename(CPLFormCIFilename(osPath, "image", "rep")); + CPLString osImageRepFilename( + CPLFormCIFilenameSafe(osPath, "image", "rep")); if (VSIStatExL(osImageRepFilename, &sStatBuf, VSI_STAT_EXISTS_FLAG) == 0) return osImageRepFilename; // Try in the upper directories if not found in the BIL image directory. - CPLString dirName(CPLGetDirname(osPath)); + CPLString dirName(CPLGetDirnameSafe(osPath)); if (CPLIsFilenameRelative(osPath.c_str())) { char *cwd = CPLGetCurrentDir(); @@ -863,7 +864,7 @@ CPLString EHdrDataset::GetImageRepFilename(const char *pszFilename) { break; } - dirName = CPLString(CPLGetDirname(dirName)); + dirName = CPLString(CPLGetDirnameSafe(dirName)); } } return CPLString(); @@ -947,7 +948,8 @@ GDALDataset *EHdrDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck) if (papszSiblingFiles) { const int iFile = CSLFindString( - papszSiblingFiles, CPLFormFilename(nullptr, osName, pszHeaderExt)); + papszSiblingFiles, + CPLFormFilenameSafe(nullptr, osName, pszHeaderExt).c_str()); if (iFile < 0) // Return if there is no corresponding .hdr file. return nullptr; @@ -1677,7 +1679,7 @@ GDALDataset *EHdrDataset::Create(const char *pszFilename, int nXSize, // Create the hdr filename. char *const pszHdrFilename = - CPLStrdup(CPLResetExtension(pszFilename, "hdr")); + CPLStrdup(CPLResetExtensionSafe(pszFilename, "hdr").c_str()); // Open the file. fp = VSIFOpenL(pszHdrFilename, "wt"); diff --git a/frmts/raw/fastdataset.cpp b/frmts/raw/fastdataset.cpp index e0d1b8bbe2c3..2ee666e8dba2 100644 --- a/frmts/raw/fastdataset.cpp +++ b/frmts/raw/fastdataset.cpp @@ -216,7 +216,7 @@ VSILFILE *FASTDataset::FOpenChannel(const char *pszBandname, int iBand, int iFASTBand) { const char *pszChannelFilename = nullptr; - char *pszPrefix = CPLStrdup(CPLGetBasename(pszFilename)); + char *pszPrefix = CPLStrdup(CPLGetBasenameSafe(pszFilename).c_str()); char *pszSuffix = CPLStrdup(CPLGetExtensionSafe(pszFilename).c_str()); fpChannels[iBand] = nullptr; @@ -573,7 +573,8 @@ GDALDataset *FASTDataset::Open(GDALOpenInfo *poOpenInfo) std::swap(poDS->fpHeader, poOpenInfo->fpL); poDS->pszFilename = poOpenInfo->pszFilename; - poDS->pszDirname = CPLStrdup(CPLGetDirname(poOpenInfo->pszFilename)); + poDS->pszDirname = + CPLStrdup(CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str()); /* -------------------------------------------------------------------- */ /* Read the administrative record. */ diff --git a/frmts/raw/genbindataset.cpp b/frmts/raw/genbindataset.cpp index c9fff1fc92f0..41130fe555e8 100644 --- a/frmts/raw/genbindataset.cpp +++ b/frmts/raw/genbindataset.cpp @@ -408,8 +408,9 @@ GDALDataset *GenBinDataset::Open(GDALOpenInfo *poOpenInfo) char **papszSiblingFiles = poOpenInfo->GetSiblingFiles(); if (papszSiblingFiles) { - const int iFile = CSLFindString( - papszSiblingFiles, CPLFormFilename(nullptr, osName, "hdr")); + const int iFile = + CSLFindString(papszSiblingFiles, + CPLFormFilenameSafe(nullptr, osName, "hdr").c_str()); if (iFile < 0) // return if there is no corresponding .hdr file return nullptr; diff --git a/frmts/raw/hkvdataset.cpp b/frmts/raw/hkvdataset.cpp index 7adb93fe4e47..ab12fb717521 100644 --- a/frmts/raw/hkvdataset.cpp +++ b/frmts/raw/hkvdataset.cpp @@ -1531,10 +1531,10 @@ GDALDataset *HKVDataset::Create(const char *pszFilenameIn, int nXSize, /* -------------------------------------------------------------------- */ char *pszBaseDir = nullptr; - if (strlen(CPLGetPath(pszFilenameIn)) == 0) + if (strlen(CPLGetPathSafe(pszFilenameIn).c_str()) == 0) pszBaseDir = CPLStrdup("."); else - pszBaseDir = CPLStrdup(CPLGetPath(pszFilenameIn)); + pszBaseDir = CPLStrdup(CPLGetPathSafe(pszFilenameIn).c_str()); VSIStatBuf sStat; if (CPLStat(pszBaseDir, &sStat) != 0 || !VSI_ISDIR(sStat.st_mode)) diff --git a/frmts/raw/iscedataset.cpp b/frmts/raw/iscedataset.cpp index 9f9fe873d2bd..0bd1d2201278 100644 --- a/frmts/raw/iscedataset.cpp +++ b/frmts/raw/iscedataset.cpp @@ -120,8 +120,9 @@ static CPLString getXMLFilename(GDALOpenInfo *poOpenInfo) const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); const CPLString osName = CPLGetFilename(poOpenInfo->pszFilename); - const int iFile = CSLFindString( - papszSiblingFiles, CPLFormFilename(nullptr, osName, "xml")); + const int iFile = + CSLFindString(papszSiblingFiles, + CPLFormFilenameSafe(nullptr, osName, "xml").c_str()); if (iFile >= 0) { osXMLFilename = diff --git a/frmts/raw/landataset.cpp b/frmts/raw/landataset.cpp index 44b30a18122a..fbf5abf2431f 100644 --- a/frmts/raw/landataset.cpp +++ b/frmts/raw/landataset.cpp @@ -576,8 +576,9 @@ GDALDataset *LANDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Check for a trailer file with a colormap in it. */ /* -------------------------------------------------------------------- */ - char *pszPath = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename)); - char *pszBasename = CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename)); + char *pszPath = CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); + char *pszBasename = + CPLStrdup(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str()); const char *pszTRLFilename = CPLFormCIFilename(pszPath, pszBasename, "trl"); VSILFILE *fpTRL = VSIFOpenL(pszTRLFilename, "rb"); if (fpTRL != nullptr) diff --git a/frmts/raw/lcpdataset.cpp b/frmts/raw/lcpdataset.cpp index bcfb63256b43..2fedccbed6b8 100644 --- a/frmts/raw/lcpdataset.cpp +++ b/frmts/raw/lcpdataset.cpp @@ -740,9 +740,10 @@ GDALDataset *LCPDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Try to read projection file. */ /* -------------------------------------------------------------------- */ - char *const pszDirname = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename)); + char *const pszDirname = + CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); char *const pszBasename = - CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename)); + CPLStrdup(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str()); poDS->osPrjFilename = CPLFormFilename(pszDirname, pszBasename, "prj"); VSIStatBufL sStatBuf; @@ -1603,10 +1604,12 @@ GDALDataset *LCPDataset::CreateCopy(const char *pszFilename, poSrcSRS->exportToWkt(&pszESRIProjection, apszOptions); if (pszESRIProjection) { - char *const pszDirname = CPLStrdup(CPLGetPath(pszFilename)); - char *const pszBasename = CPLStrdup(CPLGetBasename(pszFilename)); - char *pszPrjFilename = - CPLStrdup(CPLFormFilename(pszDirname, pszBasename, "prj")); + char *const pszDirname = + CPLStrdup(CPLGetPathSafe(pszFilename).c_str()); + char *const pszBasename = + CPLStrdup(CPLGetBasenameSafe(pszFilename).c_str()); + char *pszPrjFilename = CPLStrdup( + CPLFormFilenameSafe(pszDirname, pszBasename, "prj").c_str()); fp = VSIFOpenL(pszPrjFilename, "wt"); if (fp != nullptr) { diff --git a/frmts/raw/mffdataset.cpp b/frmts/raw/mffdataset.cpp index f386691ed7fb..8076799aaba2 100644 --- a/frmts/raw/mffdataset.cpp +++ b/frmts/raw/mffdataset.cpp @@ -784,10 +784,12 @@ GDALDataset *MFFDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Read the directory to find matching band files. */ /* -------------------------------------------------------------------- */ - char *const pszTargetPath = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename)); + char *const pszTargetPath = + CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); char *const pszTargetBase = - CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename)); - char **papszDirFiles = VSIReadDir(CPLGetPath(poOpenInfo->pszFilename)); + CPLStrdup(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str()); + char **papszDirFiles = + VSIReadDir(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); if (papszDirFiles == nullptr) { CPLFree(pszTargetPath); @@ -803,7 +805,8 @@ GDALDataset *MFFDataset::Open(GDALOpenInfo *poOpenInfo) int i = 0; // Used after for. for (; papszDirFiles[i] != nullptr; i++) { - if (!EQUAL(CPLGetBasename(papszDirFiles[i]), pszTargetBase)) + if (!EQUAL(CPLGetBasenameSafe(papszDirFiles[i]).c_str(), + pszTargetBase)) continue; const std::string osExtension = diff --git a/frmts/raw/pauxdataset.cpp b/frmts/raw/pauxdataset.cpp index eed2edd674e7..e38c59cfdf8f 100644 --- a/frmts/raw/pauxdataset.cpp +++ b/frmts/raw/pauxdataset.cpp @@ -613,7 +613,7 @@ GDALDataset *PAuxDataset::Open(GDALOpenInfo *poOpenInfo) } szAuxTarget[sizeof(szAuxTarget) - 1] = '\0'; - const std::string osPath(CPLGetPath(poOpenInfo->pszFilename)); + const std::string osPath(CPLGetPathSafe(poOpenInfo->pszFilename)); osTarget = CPLFormFilename(osPath.c_str(), szAuxTarget, nullptr); } @@ -1010,7 +1010,8 @@ GDALDataset *PAuxDataset::Create(const char *pszFilename, int nXSize, static CPLErr PAuxDelete(const char *pszBasename) { - VSILFILE *fp = VSIFOpenL(CPLResetExtension(pszBasename, "aux"), "r"); + VSILFILE *fp = + VSIFOpenL(CPLResetExtensionSafe(pszBasename, "aux").c_str(), "r"); if (fp == nullptr) { CPLError(CE_Failure, CPLE_AppDefined, @@ -1039,7 +1040,7 @@ static CPLErr PAuxDelete(const char *pszBasename) return CE_Failure; } - VSIUnlink(CPLResetExtension(pszBasename, "aux")); + VSIUnlink(CPLResetExtensionSafe(pszBasename, "aux").c_str()); return CE_None; } diff --git a/frmts/raw/rrasterdataset.cpp b/frmts/raw/rrasterdataset.cpp index 66d0a844ade3..8773dcad923a 100644 --- a/frmts/raw/rrasterdataset.cpp +++ b/frmts/raw/rrasterdataset.cpp @@ -1176,8 +1176,8 @@ GDALDataset *RRASTERDataset::Open(GDALOpenInfo *poOpenInfo) return nullptr; } - CPLString osDirname(CPLGetDirname(poOpenInfo->pszFilename)); - CPLString osBasename(CPLGetBasename(poOpenInfo->pszFilename)); + CPLString osDirname(CPLGetDirnameSafe(poOpenInfo->pszFilename)); + CPLString osBasename(CPLGetBasenameSafe(poOpenInfo->pszFilename)); CPLString osGRDExtension(poOpenInfo->osExtension); CPLString osGRIExtension((osGRDExtension[0] == 'g') ? "gri" : "GRI"); char **papszSiblings = poOpenInfo->GetSiblingFiles(); diff --git a/frmts/rcm/rcmdataset.cpp b/frmts/rcm/rcmdataset.cpp index 46248c14c7c1..85d426f710ae 100644 --- a/frmts/rcm/rcmdataset.cpp +++ b/frmts/rcm/rcmdataset.cpp @@ -1871,8 +1871,8 @@ GDALDataset *RCMDataset::Open(GDALOpenInfo *poOpenInfo) /* Form full filename (path of product.xml + basename). */ /* -------------------------------------------------------------------- */ - char *pszFullname = - CPLStrdup(CPLFormFilename(osPath, pszBasedFilename, nullptr)); + char *pszFullname = CPLStrdup( + CPLFormFilenameSafe(osPath, pszBasedFilename, nullptr).c_str()); /* -------------------------------------------------------------------- */ diff --git a/frmts/rs2/rs2dataset.cpp b/frmts/rs2/rs2dataset.cpp index eab4a415e668..7f58b4a7db10 100644 --- a/frmts/rs2/rs2dataset.cpp +++ b/frmts/rs2/rs2dataset.cpp @@ -30,7 +30,8 @@ typedef enum eCalibration_t static bool IsValidXMLFile(const char *pszPath, const char *pszLut) { /* Return true for valid xml file, false otherwise */ - char *pszLutFile = VSIStrdup(CPLFormFilename(pszPath, pszLut, nullptr)); + char *pszLutFile = + VSIStrdup(CPLFormFilenameSafe(pszPath, pszLut, nullptr).c_str()); CPLXMLTreeCloser psLut(CPLParseXMLFile(pszLutFile)); @@ -846,7 +847,7 @@ GDALDataset *RS2Dataset::Open(GDALOpenInfo *poOpenInfo) CPLString osGammaLUT; CPLString osSigma0LUT; - char *pszPath = CPLStrdup(CPLGetPath(osMDFilename)); + char *pszPath = CPLStrdup(CPLGetPathSafe(osMDFilename).c_str()); const int nFLen = static_cast(osMDFilename.size()); CPLXMLNode *psNode = psImageAttributes->psChild; @@ -944,8 +945,8 @@ GDALDataset *RS2Dataset::Open(GDALOpenInfo *poOpenInfo) /* Form full filename (path of product.xml + basename). */ /* -------------------------------------------------------------------- */ - char *pszFullname = - CPLStrdup(CPLFormFilename(pszPath, pszBasename, nullptr)); + char *pszFullname = CPLStrdup( + CPLFormFilenameSafe(pszPath, pszBasename, nullptr).c_str()); /* -------------------------------------------------------------------- */ @@ -1012,7 +1013,7 @@ GDALDataset *RS2Dataset::Open(GDALOpenInfo *poOpenInfo) } RS2CalibRasterBand *poBand = new RS2CalibRasterBand( poDS, CPLGetXMLValue(psNode, "pole", ""), eDataType, poBandFile, - eCalib, CPLFormFilename(pszPath, pszLUT, nullptr)); + eCalib, CPLFormFilenameSafe(pszPath, pszLUT, nullptr).c_str()); poDS->SetBand(poDS->GetRasterCount() + 1, poBand); } diff --git a/frmts/safe/safedataset.cpp b/frmts/safe/safedataset.cpp index b489a87751df..e815349427ea 100644 --- a/frmts/safe/safedataset.cpp +++ b/frmts/safe/safedataset.cpp @@ -997,7 +997,7 @@ GDALDataset *SAFEDataset::Open(GDALOpenInfo *poOpenInfo) if (psManifest == nullptr) return nullptr; - CPLString osPath(CPLGetPath(osMDFilename)); + CPLString osPath(CPLGetPathSafe(osMDFilename)); /* -------------------------------------------------------------------- */ /* Confirm the requested access is supported. */ diff --git a/frmts/saga/sagadataset.cpp b/frmts/saga/sagadataset.cpp index 924afc56579a..3ff20e9fa402 100644 --- a/frmts/saga/sagadataset.cpp +++ b/frmts/saga/sagadataset.cpp @@ -444,7 +444,8 @@ GDALDataset *SAGADataset::Open(GDALOpenInfo *poOpenInfo) osFullname = CPLFormFilename(osPath, file, nullptr); osName = CPLGetBasename(file); - osHDRFilename = CPLFormFilename(osPath, CPLGetBasename(file), "sgrd"); + osHDRFilename = + CPLFormFilename(osPath, CPLGetBasenameSafe(file).c_str(), "sgrd"); } else { @@ -452,7 +453,8 @@ GDALDataset *SAGADataset::Open(GDALOpenInfo *poOpenInfo) osPath = CPLGetPath(poOpenInfo->pszFilename); osName = CPLGetBasename(poOpenInfo->pszFilename); osHDRFilename = CPLFormCIFilename( - osPath, CPLGetBasename(poOpenInfo->pszFilename), "sgrd"); + osPath, CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str(), + "sgrd"); } VSILFILE *fp = VSIFOpenL(osHDRFilename, "r"); @@ -787,7 +789,7 @@ CPLErr SAGADataset::WriteHeader(CPLString osHDRFilename, GDALDataType eType, return CE_Failure; } - VSIFPrintfL(fp, "NAME\t= %s\n", CPLGetBasename(osHDRFilename)); + VSIFPrintfL(fp, "NAME\t= %s\n", CPLGetBasenameSafe(osHDRFilename).c_str()); VSIFPrintfL(fp, "DESCRIPTION\t=\n"); VSIFPrintfL(fp, "UNIT\t=\n"); VSIFPrintfL(fp, "DATAFILE_OFFSET\t= 0\n"); diff --git a/frmts/sentinel2/sentinel2dataset.cpp b/frmts/sentinel2/sentinel2dataset.cpp index 6dcca9759604..4eb51ecfa578 100644 --- a/frmts/sentinel2/sentinel2dataset.cpp +++ b/frmts/sentinel2/sentinel2dataset.cpp @@ -503,11 +503,11 @@ GDALDataset *SENTINEL2Dataset::Open(GDALOpenInfo *poOpenInfo) STARTS_WITH_CI(pszJustFilename, "S2B_USER_PRD_MSI")) && EQUAL(CPLGetExtensionSafe(pszJustFilename).c_str(), "zip")) { - const CPLString osBasename(CPLGetBasename(pszJustFilename)); + const CPLString osBasename(CPLGetBasenameSafe(pszJustFilename)); CPLString osFilename(poOpenInfo->pszFilename); CPLString osMTD(osBasename); // Normally given above constraints, osMTD.size() should be >= 16 - // but if pszJustFilename is too long, CPLGetBasename() will return + // but if pszJustFilename is too long, CPLGetBasenameSafe().c_str() will return // an empty string. if (osMTD.size() < 16) return nullptr; @@ -529,7 +529,7 @@ GDALDataset *SENTINEL2Dataset::Open(GDALOpenInfo *poOpenInfo) STARTS_WITH_CI(pszJustFilename, "S2B_MSIL1C_")) && EQUAL(CPLGetExtensionSafe(pszJustFilename).c_str(), "zip")) { - const CPLString osBasename(CPLGetBasename(pszJustFilename)); + const CPLString osBasename(CPLGetBasenameSafe(pszJustFilename)); CPLString osFilename(poOpenInfo->pszFilename); CPLString osSAFE(osBasename); // S2B_MSIL1C_20171004T233419_N0206_R001_T54DWM_20171005T001811.SAFE.zip @@ -548,7 +548,7 @@ GDALDataset *SENTINEL2Dataset::Open(GDALOpenInfo *poOpenInfo) STARTS_WITH_CI(pszJustFilename, "S2B_MSIL2A_")) && EQUAL(CPLGetExtensionSafe(pszJustFilename).c_str(), "zip")) { - const CPLString osBasename(CPLGetBasename(pszJustFilename)); + const CPLString osBasename(CPLGetBasenameSafe(pszJustFilename)); CPLString osFilename(poOpenInfo->pszFilename); CPLString osSAFE(osBasename); // S2B_MSIL1C_20171004T233419_N0206_R001_T54DWM_20171005T001811.SAFE.zip @@ -935,7 +935,7 @@ static bool SENTINEL2GetGranuleList( return false; } - CPLString osDirname(CPLGetDirname(pszFilename)); + CPLString osDirname(CPLGetDirnameSafe(pszFilename)); #ifdef HAVE_READLINK char szPointerFilename[2048]; int nBytes = static_cast( @@ -1727,8 +1727,8 @@ SENTINEL2GetMainMTDFilenameFromGranuleMTD(const char *pszFilename) { // Look for product MTD file CPLString osTopDir(CPLFormFilename( - CPLFormFilename(CPLGetDirname(pszFilename), "..", nullptr), "..", - nullptr)); + CPLFormFilename(CPLGetDirnameSafe(pszFilename).c_str(), "..", nullptr), + "..", nullptr)); // Workaround to avoid long filenames on Windows if (CPLIsFilenameRelative(pszFilename)) @@ -1737,7 +1737,7 @@ SENTINEL2GetMainMTDFilenameFromGranuleMTD(const char *pszFilename) const char *pszPath = CPLGetPath(pszFilename); if (strchr(pszPath, '/') || strchr(pszPath, '\\')) { - osTopDir = CPLGetPath(CPLGetPath(pszPath)); + osTopDir = CPLGetPath(CPLGetPathSafe(pszPath).c_str()); if (osTopDir == "") osTopDir = "."; } @@ -1816,9 +1816,9 @@ static void SENTINEL2GetResolutionSetAndMainMDFromGranule( if (atoi(osBandName) < 10) osBandName = "0" + osBandName; - CPLString osTile(SENTINEL2GetTilename(CPLGetPath(pszFilename), - CPLGetBasename(pszFilename), - osBandName)); + CPLString osTile( + SENTINEL2GetTilename(CPLGetPathSafe(pszFilename).c_str(), + CPLGetBasename(pszFilename), osBandName)); VSIStatBufL sStat; if (VSIStatExL(osTile, &sStat, VSI_STAT_EXISTS_FLAG) == 0) { @@ -2050,9 +2050,9 @@ GDALDataset *SENTINEL2Dataset::OpenL1BSubdataset(GDALOpenInfo *poOpenInfo) { for (size_t i = 0; i < aosBands.size(); i++) { - CPLString osTile(SENTINEL2GetTilename(CPLGetPath(osFilename), - CPLGetBasename(osFilename), - aosBands[i])); + CPLString osTile( + SENTINEL2GetTilename(CPLGetPathSafe(osFilename).c_str(), + CPLGetBasename(osFilename), aosBands[i])); if (SENTINEL2GetTileInfo(osTile, &nCols, &nRows, &nBits)) { if (nBits <= 16) @@ -2161,7 +2161,8 @@ GDALDataset *SENTINEL2Dataset::OpenL1BSubdataset(GDALOpenInfo *poOpenInfo) osBandName = aosBands[0]; CPLString osTile(SENTINEL2GetTilename( - CPLGetPath(osFilename), CPLGetBasename(osFilename), osBandName)); + CPLGetPath(osFilename), CPLGetBasenameSafe(osFilename).c_str(), + osBandName)); bool bTileFound = false; if (nValMax == 0) @@ -2326,7 +2327,7 @@ static bool SENTINEL2GetGranuleList_L1CSafeCompact( return false; } - CPLString osDirname(CPLGetDirname(pszFilename)); + CPLString osDirname(CPLGetDirnameSafe(pszFilename)); #ifdef HAVE_READLINK char szPointerFilename[2048]; int nBytes = static_cast( @@ -2371,9 +2372,10 @@ static bool SENTINEL2GetGranuleList_L1CSafeCompact( 3); // strip B12 // GRANULE/L1C_T30TXT_A007999_20170102T111441/IMG_DATA/T30TXT_20170102T111442_B12 // --> GRANULE/L1C_T30TXT_A007999_20170102T111441/MTD_TL.xml - oDesc.osMTDTLPath = osDirname + chSeparator + - CPLGetDirname(CPLGetDirname(pszImageFile)) + - chSeparator + "MTD_TL.xml"; + oDesc.osMTDTLPath = + osDirname + chSeparator + + CPLGetDirname(CPLGetDirnameSafe(pszImageFile).c_str()) + + chSeparator + "MTD_TL.xml"; osList.push_back(oDesc); } } @@ -2417,7 +2419,7 @@ static bool SENTINEL2GetGranuleList_L2ASafeCompact( } } - CPLString osDirname(CPLGetDirname(pszFilename)); + CPLString osDirname(CPLGetDirnameSafe(pszFilename)); #ifdef HAVE_READLINK char szPointerFilename[2048]; int nBytes = static_cast( @@ -2471,8 +2473,9 @@ static bool SENTINEL2GetGranuleList_L2ASafeCompact( oDesc.osBandPrefixPath.resize(oDesc.osBandPrefixPath.size() - 36); // GRANULE/L1C_T30TXT_A007999_20170102T111441/IMG_DATA/T30TXT_20170102T111442_B12_60m // --> GRANULE/L1C_T30TXT_A007999_20170102T111441/MTD_TL.xml - oDesc.osMTDTLPath = osDirname + chSeparator + - CPLGetDirname(CPLGetDirname(pszImageFile)); + oDesc.osMTDTLPath = + osDirname + chSeparator + + CPLGetDirname(CPLGetDirnameSafe(pszImageFile).c_str()); if (oDesc.osMTDTLPath.size() < 9) { CPLDebug("SENTINEL2", "MTDTL path too short"); diff --git a/frmts/stacta/stactadataset.cpp b/frmts/stacta/stactadataset.cpp index c2402ca89cfb..304820e8bcc3 100644 --- a/frmts/stacta/stactadataset.cpp +++ b/frmts/stacta/stactadataset.cpp @@ -858,8 +858,8 @@ bool STACTADataset::Open(GDALOpenInfo *poOpenInfo) { if (STARTS_WITH(osURLTemplate, "./")) osURLTemplate = osURLTemplate.substr(2); - osURLTemplate = CPLProjectRelativeFilename(CPLGetDirname(osFilename), - osURLTemplate); + osURLTemplate = CPLProjectRelativeFilename( + CPLGetDirnameSafe(osFilename).c_str(), osURLTemplate); } // Parse optional tile matrix set limits diff --git a/frmts/tiledb/tiledbdense.cpp b/frmts/tiledb/tiledbdense.cpp index 22493681e2ba..5b1172af747e 100644 --- a/frmts/tiledb/tiledbdense.cpp +++ b/frmts/tiledb/tiledbdense.cpp @@ -349,7 +349,8 @@ CPLErr TileDBRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, if (eErr == CE_None) { CPLString osName = CPLString().Printf( - "%s", CPLGetBasename(poAttrDS->GetDescription())); + "%s", CPLGetBasenameSafe(poAttrDS->GetDescription()) + .c_str()); SetBuffer(poQuery.get(), eAttrType, osName, pAttrBlock, nValues); @@ -1240,7 +1241,7 @@ CPLErr TileDBRasterDataset::TryLoadCachedXML(CSLConstList /*papszSiblingFiles*/, /* -------------------------------------------------------------------- */ - CPLString osPath(CPLGetPath(psPam->pszPamFilename)); + CPLString osPath(CPLGetPathSafe(psPam->pszPamFilename)); const CPLErr eErr = XMLInit(psTree, osPath); CPLDestroyXMLNode(psTree); @@ -1391,7 +1392,8 @@ GDALDataset *TileDBRasterDataset::OpenInternal(GDALOpenInfo *poOpenInfo, osAux.Printf("%s.tdb", pszArrayName); // aux file is in array folder - poDS->SetPhysicalFilename(CPLFormFilename(osURI, osAux, nullptr)); + poDS->SetPhysicalFilename( + CPLFormFilenameSafe(osURI, osAux, nullptr).c_str()); // Initialize any PAM information. poDS->SetDescription(osURI); diff --git a/frmts/tsx/tsxdataset.cpp b/frmts/tsx/tsxdataset.cpp index 114708a7d92c..4c356445b932 100644 --- a/frmts/tsx/tsxdataset.cpp +++ b/frmts/tsx/tsxdataset.cpp @@ -258,9 +258,12 @@ int TSXDataset::Identify(GDALOpenInfo *poOpenInfo) /* Check if the filename contains TSX1_SAR (TerraSAR-X) or TDX1_SAR * (TanDEM-X) or PAZ1_SAR (PAZ) */ - if (!(STARTS_WITH_CI(CPLGetBasename(osFilename), "TSX1_SAR") || - STARTS_WITH_CI(CPLGetBasename(osFilename), "TDX1_SAR") || - STARTS_WITH_CI(CPLGetBasename(osFilename), "PAZ1_SAR"))) + if (!(STARTS_WITH_CI(CPLGetBasenameSafe(osFilename).c_str(), + "TSX1_SAR") || + STARTS_WITH_CI(CPLGetBasenameSafe(osFilename).c_str(), + "TDX1_SAR") || + STARTS_WITH_CI(CPLGetBasenameSafe(osFilename).c_str(), + "PAZ1_SAR"))) return 0; VSIStatBufL sStat; @@ -273,9 +276,12 @@ int TSXDataset::Identify(GDALOpenInfo *poOpenInfo) /* Check if the filename contains TSX1_SAR (TerraSAR-X) or TDX1_SAR * (TanDEM-X) or PAZ1_SAR (PAZ) */ - if (!(STARTS_WITH_CI(CPLGetBasename(poOpenInfo->pszFilename), "TSX1_SAR") || - STARTS_WITH_CI(CPLGetBasename(poOpenInfo->pszFilename), "TDX1_SAR") || - STARTS_WITH_CI(CPLGetBasename(poOpenInfo->pszFilename), "PAZ1_SAR"))) + if (!(STARTS_WITH_CI(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str(), + "TSX1_SAR") || + STARTS_WITH_CI(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str(), + "TDX1_SAR") || + STARTS_WITH_CI(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str(), + "PAZ1_SAR"))) return 0; /* finally look for the ::FlushCache(T &obj, bool bAtClosing) obj.m_bNeedsFlush = false; // Serialize XML representation to disk - const std::string osVRTPath(CPLGetPath(obj.GetDescription())); + const std::string osVRTPath(CPLGetPathSafe(obj.GetDescription())); CPLXMLNode *psDSTree = obj.T::SerializeToXML(osVRTPath.c_str()); if (!CPLSerializeXMLTreeToFile(psDSTree, obj.GetDescription())) eErr = CE_Failure; @@ -856,9 +856,10 @@ GDALDataset *VRTDataset::Open(GDALOpenInfo *poOpenInfo) #endif // HAVE_READLINK && HAVE_LSTAT if (osInitialCurrentVrtFilename == currentVrtFilename) - pszVRTPath = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename)); + pszVRTPath = + CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); else - pszVRTPath = CPLStrdup(CPLGetPath(currentVrtFilename)); + pszVRTPath = CPLStrdup(CPLGetPathSafe(currentVrtFilename).c_str()); CPL_IGNORE_RET_VAL(VSIFCloseL(fp)); } @@ -1675,7 +1676,8 @@ CPLErr VRTDataset::AddBand(GDALDataType eType, char **papszOptions) VRTRawRasterBand *poBand = new VRTRawRasterBand(this, GetRasterCount() + 1, eType); - char *l_pszVRTPath = CPLStrdup(CPLGetPath(GetDescription())); + char *l_pszVRTPath = + CPLStrdup(CPLGetPathSafe(GetDescription()).c_str()); if (EQUAL(l_pszVRTPath, "")) { CPLFree(l_pszVRTPath); diff --git a/frmts/vrt/vrtdriver.cpp b/frmts/vrt/vrtdriver.cpp index 14db8591e6fe..1970da902513 100644 --- a/frmts/vrt/vrtdriver.cpp +++ b/frmts/vrt/vrtdriver.cpp @@ -187,7 +187,7 @@ static GDALDataset *VRTCreateCopy(const char *pszFilename, GDALDataset *poSrcDS, /* Convert tree to a single block of XML text. */ /* -------------------------------------------------------------------- */ - char *pszVRTPath = CPLStrdup(CPLGetPath(pszFilename)); + char *pszVRTPath = CPLStrdup(CPLGetPathSafe(pszFilename).c_str()); poSrcVRTDS->UnsetPreservedRelativeFilenames(); CPLXMLNode *psDSTree = poSrcVRTDS->SerializeToXML(pszVRTPath); diff --git a/frmts/vrt/vrtrasterband.cpp b/frmts/vrt/vrtrasterband.cpp index 2698926b777c..e7934238c6b0 100644 --- a/frmts/vrt/vrtrasterband.cpp +++ b/frmts/vrt/vrtrasterband.cpp @@ -517,8 +517,9 @@ CPLErr VRTRasterBand::XMLInit(const CPLXMLNode *psTree, const char *pszVRTPath, if (pszVRTPath != nullptr && atoi(CPLGetXMLValue(psFileNameNode, "relativetoVRT", "0"))) { - pszSrcDSName = - CPLStrdup(CPLProjectRelativeFilename(pszVRTPath, pszFilename)); + pszSrcDSName = CPLStrdup( + CPLProjectRelativeFilenameSafe(pszVRTPath, pszFilename) + .c_str()); } else pszSrcDSName = CPLStrdup(pszFilename); diff --git a/frmts/vrt/vrtrawrasterband.cpp b/frmts/vrt/vrtrawrasterband.cpp index 3bbfb668a664..e282f991f1e9 100644 --- a/frmts/vrt/vrtrawrasterband.cpp +++ b/frmts/vrt/vrtrawrasterband.cpp @@ -179,8 +179,8 @@ CPLErr VRTRawRasterBand::SetRawLink(const char *pszFilename, char *pszExpandedFilename = nullptr; if (pszVRTPath != nullptr && bRelativeToVRTIn) { - pszExpandedFilename = - CPLStrdup(CPLProjectRelativeFilename(pszVRTPath, pszFilename)); + pszExpandedFilename = CPLStrdup( + CPLProjectRelativeFilenameSafe(pszVRTPath, pszFilename).c_str()); } else { @@ -495,7 +495,7 @@ void VRTRawRasterBand::GetFileList(char ***ppapszFileList, int *pnSize, CPLString osSourceFilename; if (m_bRelativeToVRT && strlen(poDS->GetDescription()) > 0) osSourceFilename = - CPLFormFilename(CPLGetDirname(poDS->GetDescription()), + CPLFormFilename(CPLGetDirnameSafe(poDS->GetDescription()).c_str(), m_pszSourceFilename, nullptr); else osSourceFilename = m_pszSourceFilename; diff --git a/frmts/wms/gdalwmscache.cpp b/frmts/wms/gdalwmscache.cpp index f9b7bcedc3e2..eab071122a1c 100644 --- a/frmts/wms/gdalwmscache.cpp +++ b/frmts/wms/gdalwmscache.cpp @@ -76,7 +76,7 @@ class GDALWMSFileCache : public GDALWMSCacheImpl { // Warns if it fails to write, but returns success CPLString soFilePath = GetFilePath(pszKey); - MakeDirs(CPLGetDirname(soFilePath)); + MakeDirs(CPLGetDirnameSafe(soFilePath).c_str()); if (CPLCopyFile(soFilePath, osFileName) == CE_None) return CE_None; // Warn if it fails after folder creation @@ -247,9 +247,9 @@ CPLErr GDALWMSCache::Initialize(const char *pszUrl, CPLXMLNode *pConfig) #endif if (pszHome) { - m_osCachePath = - CPLFormFilename(CPLFormFilename(pszHome, ".cache", nullptr), - "gdalwmscache", nullptr); + m_osCachePath = CPLFormFilename( + CPLFormFilenameSafe(pszHome, ".cache", nullptr).c_str(), + "gdalwmscache", nullptr); } else { diff --git a/frmts/zarr/zarr_array.cpp b/frmts/zarr/zarr_array.cpp index 5f038d8945db..d71264aa19f8 100644 --- a/frmts/zarr/zarr_array.cpp +++ b/frmts/zarr/zarr_array.cpp @@ -2668,7 +2668,7 @@ bool ZarrArray::Rename(const std::string &osNewName) } const std::string osRootDirectoryName( - CPLGetDirname(CPLGetDirname(m_osFilename.c_str()))); + CPLGetDirname(CPLGetDirnameSafe(m_osFilename.c_str()).c_str())); const std::string osOldDirectoryName = CPLFormFilename(osRootDirectoryName.c_str(), m_osName.c_str(), nullptr); const std::string osNewDirectoryName = CPLFormFilename( diff --git a/frmts/zarr/zarr_v2_array.cpp b/frmts/zarr/zarr_v2_array.cpp index 42042344897e..92aedc834ec6 100644 --- a/frmts/zarr/zarr_v2_array.cpp +++ b/frmts/zarr/zarr_v2_array.cpp @@ -987,7 +987,7 @@ std::string ZarrV2Array::BuildTileFilename(const uint64_t *tileIndices) const } } - return CPLFormFilename(CPLGetDirname(m_osFilename.c_str()), + return CPLFormFilename(CPLGetDirnameSafe(m_osFilename.c_str()).c_str(), osFilename.c_str(), nullptr); } @@ -997,7 +997,7 @@ std::string ZarrV2Array::BuildTileFilename(const uint64_t *tileIndices) const std::string ZarrV2Array::GetDataDirectory() const { - return std::string(CPLGetDirname(m_osFilename.c_str())); + return std::string(CPLGetDirnameSafe(m_osFilename.c_str())); } /************************************************************************/ diff --git a/frmts/zarr/zarr_v3_array.cpp b/frmts/zarr/zarr_v3_array.cpp index c81543a8255e..e5689e5d54b7 100644 --- a/frmts/zarr/zarr_v3_array.cpp +++ b/frmts/zarr/zarr_v3_array.cpp @@ -815,12 +815,12 @@ std::string ZarrV3Array::BuildTileFilename(const uint64_t *tileIndices) const { if (m_aoDims.empty()) { - return CPLFormFilename(CPLGetDirname(m_osFilename.c_str()), + return CPLFormFilename(CPLGetDirnameSafe(m_osFilename.c_str()).c_str(), m_bV2ChunkKeyEncoding ? "0" : "c", nullptr); } else { - std::string osFilename(CPLGetDirname(m_osFilename.c_str())); + std::string osFilename(CPLGetDirnameSafe(m_osFilename.c_str())); osFilename += '/'; if (!m_bV2ChunkKeyEncoding) { @@ -842,7 +842,7 @@ std::string ZarrV3Array::BuildTileFilename(const uint64_t *tileIndices) const std::string ZarrV3Array::GetDataDirectory() const { - return std::string(CPLGetDirname(m_osFilename.c_str())); + return std::string(CPLGetDirnameSafe(m_osFilename.c_str())); } /************************************************************************/ diff --git a/frmts/zarr/zarrdriver.cpp b/frmts/zarr/zarrdriver.cpp index 08c1adbc86c1..57f7d361a039 100644 --- a/frmts/zarr/zarrdriver.cpp +++ b/frmts/zarr/zarrdriver.cpp @@ -1089,7 +1089,8 @@ GDALDataset *ZarrDataset::Create(const char *pszName, int nXSize, int nYSize, if (bAppendSubDS) { - VSIRmdir(CPLFormFilename(pszName, pszArrayName, nullptr)); + VSIRmdir( + CPLFormFilenameSafe(pszName, pszArrayName, nullptr).c_str()); } else { @@ -1104,18 +1105,22 @@ GDALDataset *ZarrDataset::Create(const char *pszName, int nXSize, int nYSize, { if (pszArrayName && strcmp(pszFile, pszArrayName) == 0) { - VSIRmdir(CPLFormFilename(pszName, pszFile, nullptr)); + VSIRmdir(CPLFormFilenameSafe(pszName, pszFile, nullptr) + .c_str()); } else if (!pszArrayName && - strcmp(pszFile, CPLGetBasename(pszName)) == 0) + strcmp(pszFile, + CPLGetBasenameSafe(pszName).c_str()) == 0) { - VSIRmdir(CPLFormFilename(pszName, pszFile, nullptr)); + VSIRmdir(CPLFormFilenameSafe(pszName, pszFile, nullptr) + .c_str()); } else if (strcmp(pszFile, ".zgroup") == 0 || strcmp(pszFile, ".zmetadata") == 0 || strcmp(pszFile, "zarr.json") == 0) { - VSIUnlink(CPLFormFilename(pszName, pszFile, nullptr)); + VSIUnlink(CPLFormFilenameSafe(pszName, pszFile, nullptr) + .c_str()); } } VSIRmdir(pszName); diff --git a/ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp b/ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp index 10511dbc2d05..c97bb0c444a8 100644 --- a/ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp +++ b/ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp @@ -233,7 +233,8 @@ bool OGRADBCDataset::Open(const GDALOpenInfo *poOpenInfo) if (iPos2 != std::string::npos) { const std::string osFilename = osSQL.substr(iPos, iPos2 - iPos); - if (EQUAL(CPLGetExtension(osFilename.c_str()), "parquet")) + if (EQUAL(CPLGetExtensionSafe(osFilename.c_str()).c_str(), + "parquet")) { m_osParquetFilename = osFilename; bIsParquet = true; @@ -405,8 +406,8 @@ bool OGRADBCDataset::Open(const GDALOpenInfo *poOpenInfo) m_osParquetFilename = pszFilename; osLayerName = CPLGetBasename(m_osParquetFilename.c_str()); if (osLayerName == "*") - osLayerName = - CPLGetBasename(CPLGetDirname(m_osParquetFilename.c_str())); + osLayerName = CPLGetBasename( + CPLGetDirnameSafe(m_osParquetFilename.c_str()).c_str()); if (!pszSQL) { osSQL = diff --git a/ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp b/ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp index 468a05d42ddf..64b0b5d602a5 100644 --- a/ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp +++ b/ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp @@ -256,7 +256,7 @@ static GDALDataset *OGRFeatherDriverOpen(GDALOpenInfo *poOpenInfo) } auto poRecordBatchReader = *result; auto poLayer = std::make_unique( - poDS.get(), CPLGetBasename(poOpenInfo->pszFilename), + poDS.get(), CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str(), poRecordBatchReader); poDS->SetLayer(std::move(poLayer)); } diff --git a/ogr/ogrsf_frmts/arrow/ogrfeatherwriterlayer.cpp b/ogr/ogrsf_frmts/arrow/ogrfeatherwriterlayer.cpp index b09d42cfe23b..2c276244fa52 100644 --- a/ogr/ogrsf_frmts/arrow/ogrfeatherwriterlayer.cpp +++ b/ogr/ogrsf_frmts/arrow/ogrfeatherwriterlayer.cpp @@ -71,7 +71,7 @@ bool OGRFeatherWriterLayer::SetOptions(const std::string &osFilename, OGRwkbGeometryType eGType) { const char *pszDefaultFormat = - (EQUAL(CPLGetExtension(osFilename.c_str()), "arrows") || + (EQUAL(CPLGetExtensionSafe(osFilename.c_str()).c_str(), "arrows") || STARTS_WITH_CI(osFilename.c_str(), "/vsistdout")) ? "STREAM" : "FILE"; diff --git a/ogr/ogrsf_frmts/avc/ogravcbindriver.cpp b/ogr/ogrsf_frmts/avc/ogravcbindriver.cpp index 358c3725f838..2535acf34ea1 100644 --- a/ogr/ogrsf_frmts/avc/ogravcbindriver.cpp +++ b/ogr/ogrsf_frmts/avc/ogravcbindriver.cpp @@ -31,7 +31,8 @@ static GDALDataset *OGRAVCBinDriverOpen(GDALOpenInfo *poOpenInfo) bool bFoundCandidateFile = false; for (int i = 0; papszSiblingFiles[i] != nullptr; i++) { - if (EQUAL(CPLGetExtension(papszSiblingFiles[i]), "ADF")) + if (EQUAL(CPLGetExtensionSafe(papszSiblingFiles[i]).c_str(), + "ADF")) { bFoundCandidateFile = true; break; diff --git a/ogr/ogrsf_frmts/cad/gdalcaddataset.cpp b/ogr/ogrsf_frmts/cad/gdalcaddataset.cpp index a1fe3036ad1d..8fb4b351830c 100644 --- a/ogr/ogrsf_frmts/cad/gdalcaddataset.cpp +++ b/ogr/ogrsf_frmts/cad/gdalcaddataset.cpp @@ -229,8 +229,9 @@ int GDALCADDataset::Open(GDALOpenInfo *poOpenInfo, CADFileIO *pFileIO, CPLString osImgPath = CPLGetPath(osImgFilename); if (osImgPath.empty()) { - osImgFilename = CPLFormFilename(CPLGetPath(osCADFilename), - osImgFilename, nullptr); + osImgFilename = + CPLFormFilename(CPLGetPathSafe(osCADFilename).c_str(), + osImgFilename, nullptr); } if (!CPLCheckForFile(const_cast(osImgFilename.c_str()), diff --git a/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp b/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp index 8c5862890615..4c26f86d57a7 100644 --- a/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp +++ b/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp @@ -84,7 +84,7 @@ OGRErr OGRCSVEditableLayerSynchronizer::EditableSyncToDisk( const CPLString osLayerName(m_poCSVLayer->GetName()); const CPLString osFilename(m_poCSVLayer->GetFilename()); const bool bCreateCSVT = m_poCSVLayer->GetCreateCSVT(); - const CPLString osCSVTFilename(CPLResetExtension(osFilename, "csvt")); + const CPLString osCSVTFilename(CPLResetExtensionSafe(osFilename, "csvt")); VSIStatBufL sStatBuf; const bool bHasCSVT = VSIStatL(osCSVTFilename, &sStatBuf) == 0; CPLString osTmpFilename(osFilename); @@ -248,7 +248,7 @@ OGRErr OGRCSVEditableLayerSynchronizer::EditableSyncToDisk( CPLError(CE_Failure, CPLE_AppDefined, "Error while creating %s", osTmpFilename.c_str()); VSIUnlink(osTmpFilename); - VSIUnlink(CPLResetExtension(osTmpFilename, "csvt")); + VSIUnlink(CPLResetExtensionSafe(osTmpFilename, "csvt").c_str()); return eErr; } @@ -631,7 +631,7 @@ bool OGRCSVDataSource::Open(const char *pszFilename, bool bUpdateIn, { char **papszFiles = VSIReadDir(osFilename); if (CSLCount(papszFiles) != 1 || - !EQUAL(CPLGetExtension(papszFiles[0]), "CSV")) + !EQUAL(CPLGetExtensionSafe(papszFiles[0]).c_str(), "CSV")) { CSLDestroy(papszFiles); return FALSE; @@ -679,7 +679,7 @@ bool OGRCSVDataSource::Open(const char *pszFilename, bool bUpdateIn, // GNIS specific. else if (strlen(papszNames[i]) > 2 && STARTS_WITH_CI(papszNames[i] + 2, "_Features_") && - EQUAL(CPLGetExtension(papszNames[i]), "txt")) + EQUAL(CPLGetExtensionSafe(papszNames[i]).c_str(), "txt")) { bool bRet = OpenTable(oSubFilename, papszOpenOptionsIn, nullptr, "PRIM"); @@ -695,7 +695,7 @@ bool OGRCSVDataSource::Open(const char *pszFilename, bool bUpdateIn, // GNIS specific. else if (strlen(papszNames[i]) > 2 && STARTS_WITH_CI(papszNames[i] + 2, "_FedCodes_") && - EQUAL(CPLGetExtension(papszNames[i]), "txt")) + EQUAL(CPLGetExtensionSafe(papszNames[i]).c_str(), "txt")) { if (!OpenTable(oSubFilename, papszOpenOptionsIn, nullptr, "PRIMARY")) @@ -1228,8 +1228,8 @@ OGRCSVDataSource::ICreateLayer(const char *pszLayerName, poSpatialRef->exportToWkt(&pszWKT); if (pszWKT) { - VSILFILE *fpPRJ = - VSIFOpenL(CPLResetExtension(osFilename, "prj"), "wb"); + VSILFILE *fpPRJ = VSIFOpenL( + CPLResetExtensionSafe(osFilename, "prj").c_str(), "wb"); if (fpPRJ) { CPL_IGNORE_RET_VAL(VSIFPrintfL(fpPRJ, "%s\n", pszWKT)); diff --git a/ogr/ogrsf_frmts/csv/ogrcsvlayer.cpp b/ogr/ogrsf_frmts/csv/ogrcsvlayer.cpp index 887f8902899e..91b49da6212e 100644 --- a/ogr/ogrsf_frmts/csv/ogrcsvlayer.cpp +++ b/ogr/ogrsf_frmts/csv/ogrcsvlayer.cpp @@ -778,7 +778,7 @@ void OGRCSVLayer::BuildFeatureDefn(const char *pszNfdcGeomField, poFeatureDefn->GetGeomFieldDefn(0)->GetSpatialRef() == nullptr) { VSILFILE *fpPRJ = - VSIFOpenL(CPLResetExtension(pszFilename, "prj"), "rb"); + VSIFOpenL(CPLResetExtensionSafe(pszFilename, "prj").c_str(), "rb"); if (fpPRJ != nullptr) { GByte *pabyRet = nullptr; @@ -1986,8 +1986,10 @@ OGRErr OGRCSVLayer::WriteHeader() VSILFILE *fpCSVT = nullptr; if (bCreateCSVT && iFile == 0) { - char *pszDirName = CPLStrdup(CPLGetDirname(pszFilename)); - char *pszBaseName = CPLStrdup(CPLGetBasename(pszFilename)); + char *pszDirName = + CPLStrdup(CPLGetDirnameSafe(pszFilename).c_str()); + char *pszBaseName = + CPLStrdup(CPLGetBasenameSafe(pszFilename).c_str()); fpCSVT = VSIFOpenL( CPLFormFilename(pszDirName, pszBaseName, ".csvt"), "wb"); CPLFree(pszDirName); diff --git a/ogr/ogrsf_frmts/edigeo/ogredigeodatasource.cpp b/ogr/ogrsf_frmts/edigeo/ogredigeodatasource.cpp index 91135d0f18b3..481e6e3d7159 100644 --- a/ogr/ogrsf_frmts/edigeo/ogredigeodatasource.cpp +++ b/ogr/ogrsf_frmts/edigeo/ogredigeodatasource.cpp @@ -156,8 +156,8 @@ VSILFILE *OGREDIGEODataSource::OpenFile(const char *pszType, const CPLString &osExt) { CPLString osTmp = osLON + pszType; - CPLString osFilename = CPLFormCIFilename(CPLGetPath(GetDescription()), - osTmp.c_str(), osExt.c_str()); + CPLString osFilename = CPLFormCIFilename( + CPLGetPathSafe(GetDescription()).c_str(), osTmp.c_str(), osExt.c_str()); VSILFILE *fp = VSIFOpenL(osFilename, "rb"); if (fp == nullptr) { diff --git a/ogr/ogrsf_frmts/filegdb/FGdbDriver.cpp b/ogr/ogrsf_frmts/filegdb/FGdbDriver.cpp index 4c073ddafbac..5a294006a455 100644 --- a/ogr/ogrsf_frmts/filegdb/FGdbDriver.cpp +++ b/ogr/ogrsf_frmts/filegdb/FGdbDriver.cpp @@ -471,11 +471,13 @@ OGRErr FGdbTransactionManager::CommitTransaction(GDALDataset *&poDSInOut, continue; VSIStatBufL sStat; if ((*papszIter)[0] == 'a' && - VSIStatL(CPLFormFilename(osEditedName, *papszIter, nullptr), + VSIStatL(CPLFormFilenameSafe(osEditedName, *papszIter, nullptr) + .c_str(), &sStat) != 0) { if (EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE1") || - VSIRename(CPLFormFilename(osName, *papszIter, nullptr), + VSIRename(CPLFormFilenameSafe(osName, *papszIter, nullptr) + .c_str(), CPLFormFilename(osName, *papszIter, "tmp")) != 0) { CPLError(CE_Failure, CPLE_AppDefined, @@ -498,7 +500,8 @@ OGRErr FGdbTransactionManager::CommitTransaction(GDALDataset *&poDSInOut, if (strcmp(*papszIter, ".") == 0 || strcmp(*papszIter, "..") == 0) continue; struct stat sStat; - if (lstat(CPLFormFilename(osEditedName, *papszIter, nullptr), + if (lstat(CPLFormFilenameSafe(osEditedName, *papszIter, nullptr) + .c_str(), &sStat) != 0) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot stat %s", @@ -509,14 +512,16 @@ OGRErr FGdbTransactionManager::CommitTransaction(GDALDataset *&poDSInOut, { // If there was such a file in original directory, first rename // it as a temporary file - if (lstat(CPLFormFilename(osName, *papszIter, nullptr), + if (lstat(CPLFormFilenameSafe(osName, *papszIter, nullptr) + .c_str(), &sStat) == 0) { if (EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE2") || - VSIRename(CPLFormFilename(osName, *papszIter, nullptr), - CPLFormFilename(osName, *papszIter, "tmp")) != - 0) + VSIRename( + CPLFormFilenameSafe(osName, *papszIter, nullptr) + .c_str(), + CPLFormFilename(osName, *papszIter, "tmp")) != 0) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s", diff --git a/ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp b/ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp index 1b50500a030f..4cee91b39e67 100644 --- a/ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp +++ b/ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp @@ -210,15 +210,15 @@ int FGdbLayer::EditIndexesForFIDHack(const char *pszRadixTablename) return FALSE; } - CPLString osDirectory(CPLGetPath(pszRadixTablename)); + CPLString osDirectory(CPLGetPathSafe(pszRadixTablename)); char **papszFiles = VSIReadDir(osDirectory); - CPLString osBasename(CPLGetBasename(pszRadixTablename)); + CPLString osBasename(CPLGetBasenameSafe(pszRadixTablename)); int bRet = TRUE; for (char **papszIter = papszFiles; papszIter && *papszIter; papszIter++) { if (strncmp(*papszIter, osBasename.c_str(), osBasename.size()) == 0 && - (EQUAL(CPLGetExtension(*papszIter), "atx") || - EQUAL(CPLGetExtension(*papszIter), "spx"))) + (EQUAL(CPLGetExtensionSafe(*papszIter).c_str(), "atx") || + EQUAL(CPLGetExtensionSafe(*papszIter).c_str(), "spx"))) { CPLString osIndex( CPLFormFilename(osDirectory, *papszIter, nullptr)); diff --git a/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobufdataset.cpp b/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobufdataset.cpp index a59807e117a1..a2cbc9408588 100644 --- a/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobufdataset.cpp +++ b/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobufdataset.cpp @@ -91,7 +91,8 @@ static CPLErr OGRFlatGoBufDriverDelete(const char *pszDataSource) papszDirEntries != nullptr && papszDirEntries[iFile] != nullptr; iFile++) { - if (EQUAL(CPLGetExtension(papszDirEntries[iFile]), "fgb")) + if (EQUAL(CPLGetExtensionSafe(papszDirEntries[iFile]).c_str(), + "fgb")) { VSIUnlink(CPLFormFilename(pszDataSource, papszDirEntries[iFile], nullptr)); @@ -245,7 +246,7 @@ GDALDataset *OGRFlatGeobufDataset::Open(GDALOpenInfo *poOpenInfo) { if (strcmp(aosFiles[i], ".") == 0 || strcmp(aosFiles[i], "..") == 0) continue; - if (EQUAL(CPLGetExtension(aosFiles[i]), "fgb")) + if (EQUAL(CPLGetExtensionSafe(aosFiles[i]).c_str(), "fgb")) nCountFGB++; else nCountNonFGB++; @@ -258,7 +259,7 @@ GDALDataset *OGRFlatGeobufDataset::Open(GDALOpenInfo *poOpenInfo) } for (int i = 0; i < aosFiles.size(); i++) { - if (EQUAL(CPLGetExtension(aosFiles[i]), "fgb")) + if (EQUAL(CPLGetExtensionSafe(aosFiles[i]).c_str(), "fgb")) { CPLString osFilename(CPLFormFilename(poOpenInfo->pszFilename, aosFiles[i], nullptr)); @@ -379,7 +380,7 @@ int OGRFlatGeobufDataset::TestCapability(const char *pszCap) static CPLString LaunderLayerName(const char *pszLayerName) { - std::string osRet(CPLLaunderForFilename(pszLayerName, nullptr)); + std::string osRet(CPLLaunderForFilenameSafe(pszLayerName, nullptr)); if (osRet != pszLayerName) { CPLError(CE_Warning, CPLE_AppDefined, diff --git a/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp b/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp index 0889526680dc..e76245357625 100644 --- a/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp +++ b/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp @@ -2455,8 +2455,8 @@ void OGRFlatGeobufLayer::ResetReading() std::string OGRFlatGeobufLayer::GetTempFilePath(const CPLString &fileName, CSLConstList papszOptions) { - const CPLString osDirname(CPLGetPath(fileName.c_str())); - const CPLString osBasename(CPLGetBasename(fileName.c_str())); + const CPLString osDirname(CPLGetPathSafe(fileName.c_str())); + const CPLString osBasename(CPLGetBasenameSafe(fileName.c_str())); const char *pszTempDir = CSLFetchNameValue(papszOptions, "TEMPORARY_DIR"); std::string osTempFile = pszTempDir ? CPLFormFilename(pszTempDir, osBasename, nullptr) diff --git a/ogr/ogrsf_frmts/generic/ogr_miattrind.cpp b/ogr/ogrsf_frmts/generic/ogr_miattrind.cpp index f16b994c3926..93dba0d9d15a 100644 --- a/ogr/ogrsf_frmts/generic/ogr_miattrind.cpp +++ b/ogr/ogrsf_frmts/generic/ogr_miattrind.cpp @@ -164,9 +164,11 @@ OGRErr OGRMILayerAttrIndex::Initialize(const char *pszIndexPathIn, if (STARTS_WITH_CI(pszIndexPathIn, "")) return LoadConfigFromXML(pszIndexPathIn); - pszMetadataFilename = CPLStrdup(CPLResetExtension(pszIndexPathIn, "idm")); + pszMetadataFilename = + CPLStrdup(CPLResetExtensionSafe(pszIndexPathIn, "idm").c_str()); - pszMIINDFilename = CPLStrdup(CPLResetExtension(pszIndexPathIn, "ind")); + pszMIINDFilename = + CPLStrdup(CPLResetExtensionSafe(pszIndexPathIn, "ind").c_str()); /* -------------------------------------------------------------------- */ /* If a metadata file already exists, load it. */ diff --git a/ogr/ogrsf_frmts/geoconcept/ogrgeoconceptdatasource.cpp b/ogr/ogrsf_frmts/geoconcept/ogrgeoconceptdatasource.cpp index 1f5a1ff54806..5cf6baa9e3cb 100644 --- a/ogr/ogrsf_frmts/geoconcept/ogrgeoconceptdatasource.cpp +++ b/ogr/ogrsf_frmts/geoconcept/ogrgeoconceptdatasource.cpp @@ -126,7 +126,7 @@ int OGRGeoconceptDataSource::LoadFile(const char *pszMode) CPLStrlwr(_pszExt); if (!_pszDirectory) - _pszDirectory = CPLStrdup(CPLGetPath(GetDescription())); + _pszDirectory = CPLStrdup(CPLGetPathSafe(GetDescription()).c_str()); if ((_hGXT = Open_GCIO(GetDescription(), _pszExt, pszMode, _pszGCT)) == nullptr) @@ -212,7 +212,7 @@ int OGRGeoconceptDataSource::Create(const char *pszName, char **papszOptions) const char *pszExtension = CSLFetchNameValue(papszOptions, "EXTENSION"); if (pszExtension == nullptr) { - _pszExt = CPLStrdup(CPLGetExtension(pszName)); + _pszExt = CPLStrdup(CPLGetExtensionSafe(pszName).c_str()); } else { @@ -234,21 +234,22 @@ int OGRGeoconceptDataSource::Create(const char *pszName, char **papszOptions) _pszDirectory = CPLStrdup(pszName); CPLFree(_pszExt); _pszExt = CPLStrdup("gxt"); - char *pszbName = CPLStrdup(CPLGetBasename(pszName)); + char *pszbName = CPLStrdup(CPLGetBasenameSafe(pszName).c_str()); if (strlen(pszbName) == 0) { /* pszName ends with '/' */ CPLFree(pszbName); char *pszNameDup = CPLStrdup(pszName); pszNameDup[strlen(pszName) - 2] = '\0'; - pszbName = CPLStrdup(CPLGetBasename(pszNameDup)); + pszbName = CPLStrdup(CPLGetBasenameSafe(pszNameDup).c_str()); CPLFree(pszNameDup); } - SetDescription(CPLFormFilename(_pszDirectory, pszbName, nullptr)); + SetDescription( + CPLFormFilenameSafe(_pszDirectory, pszbName, nullptr).c_str()); CPLFree(pszbName); } else { - _pszDirectory = CPLStrdup(CPLGetPath(pszName)); + _pszDirectory = CPLStrdup(CPLGetPathSafe(pszName).c_str()); SetDescription(pszName); } diff --git a/ogr/ogrsf_frmts/gml/gmlregistry.cpp b/ogr/ogrsf_frmts/gml/gmlregistry.cpp index 381dfd3c9cbe..b56a459b0143 100644 --- a/ogr/ogrsf_frmts/gml/gmlregistry.cpp +++ b/ogr/ogrsf_frmts/gml/gmlregistry.cpp @@ -147,8 +147,9 @@ bool GMLRegistryFeatureType::Parse(const char *pszRegistryFilename, !STARTS_WITH(pszSchemaLocation, "https://") && CPLIsFilenameRelative(pszSchemaLocation)) { - pszSchemaLocation = CPLFormFilename(CPLGetPath(pszRegistryFilename), - pszSchemaLocation, nullptr); + pszSchemaLocation = + CPLFormFilename(CPLGetPathSafe(pszRegistryFilename).c_str(), + pszSchemaLocation, nullptr); } osSchemaLocation = pszSchemaLocation; } diff --git a/ogr/ogrsf_frmts/gml/ogrgmldatasource.cpp b/ogr/ogrsf_frmts/gml/ogrgmldatasource.cpp index e343cce322d1..d139fba6c29c 100644 --- a/ogr/ogrsf_frmts/gml/ogrgmldatasource.cpp +++ b/ogr/ogrsf_frmts/gml/ogrgmldatasource.cpp @@ -755,8 +755,8 @@ bool OGRGMLDataSource::Open(GDALOpenInfo *poOpenInfo) { // When no option is given or is not recognised, // use the same file name with the extension changed to .resolved.gml - pszXlinkResolvedFilename = - CPLStrdup(CPLResetExtension(pszFilename, "resolved.gml")); + pszXlinkResolvedFilename = CPLStrdup( + CPLResetExtensionSafe(pszFilename, "resolved.gml").c_str()); // Check if the file already exists. VSIStatBufL sResStatBuf, sGMLStatBuf; @@ -1904,7 +1904,8 @@ bool OGRGMLDataSource::Create(const char *pszFilename, char **papszOptions) { if (EQUAL(CPLGetExtensionSafe(pszFilename).c_str(), "zip")) { - SetDescription(CPLFormFilename(pszFilename, "out.gml", nullptr)); + SetDescription( + CPLFormFilenameSafe(pszFilename, "out.gml", nullptr).c_str()); } fpOutput = VSIFOpenExL(GetDescription(), "wb", true); @@ -1959,13 +1960,15 @@ bool OGRGMLDataSource::Create(const char *pszFilename, char **papszOptions) } else if (pszSchemaOpt == nullptr || EQUAL(pszSchemaOpt, "EXTERNAL")) { - char *pszBasename = CPLStrdup(CPLGetBasename(GetDescription())); + char *pszBasename = + CPLStrdup(CPLGetBasenameSafe(GetDescription()).c_str()); PrintLine( fpOutput, " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); PrintLine(fpOutput, " xsi:schemaLocation=\"%s %s\"", - pszTargetNameSpace, CPLResetExtension(pszBasename, "xsd")); + pszTargetNameSpace, + CPLResetExtensionSafe(pszBasename, "xsd").c_str()); CPLFree(pszBasename); } diff --git a/ogr/ogrsf_frmts/gml/resolvexlinks.cpp b/ogr/ogrsf_frmts/gml/resolvexlinks.cpp index 9940a3b1d2ee..6b687efd5ea2 100644 --- a/ogr/ogrsf_frmts/gml/resolvexlinks.cpp +++ b/ogr/ogrsf_frmts/gml/resolvexlinks.cpp @@ -573,7 +573,8 @@ bool GMLReader::ResolveXlinks(const char *pszFile, bool *pbOutIsTempFile, if (bTryWithTempFile) { - pszTmpName = CPLStrdup(CPLGenerateTempFilename("ResolvedGML")); + pszTmpName = + CPLStrdup(CPLGenerateTempFilenameSafe("ResolvedGML").c_str()); if (!CPLSerializeXMLTreeToFile(papsSrcTree[0], pszTmpName)) { CPLError(CE_Failure, CPLE_FileIO, diff --git a/ogr/ogrsf_frmts/gmlas/ogrgmlasdatasource.cpp b/ogr/ogrsf_frmts/gmlas/ogrgmlasdatasource.cpp index 4e2261d76a56..e166ffad0603 100644 --- a/ogr/ogrsf_frmts/gmlas/ogrgmlasdatasource.cpp +++ b/ogr/ogrsf_frmts/gmlas/ogrgmlasdatasource.cpp @@ -558,9 +558,9 @@ void OGRGMLASDataSource::FillOtherMetadataLayer( (osXSDFilename.find("http://") != 0 && osXSDFilename.find("https://") != 0 && CPLIsFilenameRelative(osXSDFilename)) - ? CPLString( - CPLFormFilename(CPLGetDirname(osAbsoluteGMLFilename), - osXSDFilename, nullptr)) + ? CPLString(CPLFormFilename( + CPLGetDirnameSafe(osAbsoluteGMLFilename).c_str(), + osXSDFilename, nullptr)) : osXSDFilename); oFeature.SetField(szVALUE, osAbsoluteXSDFilename.c_str()); CPL_IGNORE_RET_VAL( @@ -652,7 +652,8 @@ OGRGMLASDataSource::BuildXSDVector(const CPLString &osXSDFilenames) CPLIsFilenameRelative(papszTokens[i]) && pszCurDir != nullptr) { aoXSDs.push_back(PairURIFilename( - "", CPLFormFilename(pszCurDir, papszTokens[i], nullptr))); + "", CPLFormFilenameSafe(pszCurDir, papszTokens[i], nullptr) + .c_str())); } else { @@ -859,9 +860,9 @@ bool OGRGMLASDataSource::Open(GDALOpenInfo *poOpenInfo) szHANDLE_MULTIPLE_IMPORTS_OPTION, m_oConf.m_bHandleMultipleImports); - bool bRet = - oAnalyzer.Analyze(m_oCache, CPLGetDirname(m_osGMLFilename), aoXSDs, - m_bSchemaFullChecking, m_bHandleMultipleImports); + bool bRet = oAnalyzer.Analyze( + m_oCache, CPLGetDirnameSafe(m_osGMLFilename).c_str(), aoXSDs, + m_bSchemaFullChecking, m_bHandleMultipleImports); if (!bRet) { return false; diff --git a/ogr/ogrsf_frmts/gmlas/ogrgmlasreader.cpp b/ogr/ogrsf_frmts/gmlas/ogrgmlasreader.cpp index 41ceb0ea9d4c..a137ba4758b0 100644 --- a/ogr/ogrsf_frmts/gmlas/ogrgmlasreader.cpp +++ b/ogr/ogrsf_frmts/gmlas/ogrgmlasreader.cpp @@ -261,7 +261,8 @@ void GMLASBaseEntityResolver::notifyClosing(const CPLString &osFilename) { CPLDebug("GMLAS", "Closing %s", osFilename.c_str()); - CPLAssert(m_aosPathStack.back() == CPLString(CPLGetDirname(osFilename))); + CPLAssert(m_aosPathStack.back() == + CPLString(CPLGetDirnameSafe(osFilename))); m_aosPathStack.pop_back(); } @@ -334,7 +335,7 @@ GMLASBaseEntityResolver::resolveEntity(const XMLCh *const /*publicId*/, DoExtraSchemaProcessing(osNewPath, fp); } - m_aosPathStack.push_back(CPLGetDirname(osNewPath)); + m_aosPathStack.push_back(CPLGetDirnameSafe(osNewPath).c_str()); GMLASInputSource *poIS = new GMLASInputSource(osNewPath, fp); poIS->SetClosingCallback(this); return poIS; @@ -449,7 +450,8 @@ bool GMLASReader::LoadXSDInParser( (osXSDFilename.find("http://") != 0 && osXSDFilename.find("https://") != 0 && CPLIsFilenameRelative(osXSDFilename)) - ? CPLString(CPLFormFilename(osBaseDirname, osXSDFilename, nullptr)) + ? CPLString( + CPLFormFilenameSafe(osBaseDirname, osXSDFilename, nullptr)) : osXSDFilename); for (int iPass = 0; iPass <= 1; ++iPass) @@ -469,7 +471,7 @@ bool GMLASReader::LoadXSDInParser( bHandleMultipleImports); // Install a temporary entity resolved based on the current XSD - CPLString osXSDDirname(CPLGetDirname(osModifXSDFilename)); + CPLString osXSDDirname(CPLGetDirnameSafe(osModifXSDFilename)); if (osXSDFilename.find("http://") == 0 || osXSDFilename.find("https://") == 0) { @@ -640,7 +642,7 @@ bool GMLASReader::Init(const char *pszFilename, m_poSAXReader->setFeature(XMLUni::fgXercesValidationErrorAsFatal, false); - CPLString osBaseDirname(CPLGetDirname(pszFilename)); + CPLString osBaseDirname(CPLGetDirnameSafe(pszFilename)); // In the case the schemas are explicitly passed, we must do special // processing diff --git a/ogr/ogrsf_frmts/gmlas/ogrgmlaswriter.cpp b/ogr/ogrsf_frmts/gmlas/ogrgmlaswriter.cpp index dd2791daf8f3..aba5c1b3749c 100644 --- a/ogr/ogrsf_frmts/gmlas/ogrgmlaswriter.cpp +++ b/ogr/ogrsf_frmts/gmlas/ogrgmlaswriter.cpp @@ -495,7 +495,7 @@ bool GMLASWriter::Write(GDALProgressFunc pfnProgress, void *pProgressData) // Write .xsd if (bWFS2FeatureCollection) - VSIUnlink(CPLResetExtension(m_osFilename, "xsd")); + VSIUnlink(CPLResetExtensionSafe(m_osFilename, "xsd").c_str()); else if (bGenerateXSD && !WriteXSD(osOutXSDFilename, aoXSDs)) return false; @@ -579,7 +579,7 @@ bool GMLASWriter::WriteXSD(const CPLString &osXSDFilenameIn, const CPLString osXSDFilename( !osXSDFilenameIn.empty() ? osXSDFilenameIn - : CPLString(CPLResetExtension(m_osFilename, "xsd"))); + : CPLString(CPLResetExtensionSafe(m_osFilename, "xsd"))); VSILFILE *fpXSD = VSIFOpenL(osXSDFilename, "wb"); if (fpXSD == nullptr) { @@ -666,7 +666,7 @@ bool GMLASWriter::WriteXMLHeader( } // Delete potentially existing .gfs file - VSIUnlink(CPLResetExtension(m_osFilename, "gfs")); + VSIUnlink(CPLResetExtensionSafe(m_osFilename, "gfs").c_str()); std::map aoWrittenPrefixes; aoWrittenPrefixes[szXSI_PREFIX] = szXSI_URI; @@ -723,8 +723,8 @@ bool GMLASWriter::WriteXMLHeader( const CPLString osXSDFilename( !osXSDFilenameIn.empty() ? osXSDFilenameIn - : CPLString( - CPLGetFilename(CPLResetExtension(m_osFilename, "xsd")))); + : CPLString(CPLGetFilename( + CPLResetExtensionSafe(m_osFilename, "xsd").c_str()))); osSchemaURI += m_osTargetNameSpace; osSchemaURI += " "; osSchemaURI += osXSDFilename; @@ -2819,7 +2819,7 @@ GDALDataset *OGRGMLASDriverCreateCopy(const char *pszFilename, GDALProgressFunc pfnProgress, void *pProgressData) { - if (strcmp(CPLGetExtension(pszFilename), "xsd") == 0) + if (strcmp(CPLGetExtensionSafe(pszFilename).c_str(), "xsd") == 0) { CPLError(CE_Failure, CPLE_AppDefined, ".xsd extension is not valid"); return nullptr; diff --git a/ogr/ogrsf_frmts/gmlutils/parsexsd.cpp b/ogr/ogrsf_frmts/gmlutils/parsexsd.cpp index bec8fae945a4..a2d2ad104348 100644 --- a/ogr/ogrsf_frmts/gmlutils/parsexsd.cpp +++ b/ogr/ogrsf_frmts/gmlutils/parsexsd.cpp @@ -983,9 +983,9 @@ static void CPLXMLSchemaResolveInclude(const char *pszMainSchemaLocation, !STARTS_WITH(pszSchemaLocation, "https://") && CPLIsFilenameRelative(pszSchemaLocation)) { - pszSchemaLocation = - CPLFormFilename(CPLGetPath(pszMainSchemaLocation), - pszSchemaLocation, nullptr); + pszSchemaLocation = CPLFormFilename( + CPLGetPathSafe(pszMainSchemaLocation).c_str(), + pszSchemaLocation, nullptr); } CPLXMLNode *psIncludedXSDTree = diff --git a/ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp b/ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp index c96cc625e9f7..abb0cad27086 100644 --- a/ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp +++ b/ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp @@ -122,7 +122,7 @@ OGRGmtDataSource::ICreateLayer(const char *pszLayerName, if (STARTS_WITH(osFilename, "/vsistdout")) pszFlags = "wb"; - else if (!EQUAL(CPLGetExtension(GetDescription()), "gmt")) + else if (!EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "gmt")) osFilename = CPLFormFilename(osPath, pszLayerName, "gmt"); /* -------------------------------------------------------------------- */ diff --git a/ogr/ogrsf_frmts/gmt/ogrgmtlayer.cpp b/ogr/ogrsf_frmts/gmt/ogrgmtlayer.cpp index cfe60261fc78..c1297d847380 100644 --- a/ogr/ogrsf_frmts/gmt/ogrgmtlayer.cpp +++ b/ogr/ogrsf_frmts/gmt/ogrgmtlayer.cpp @@ -37,7 +37,7 @@ OGRGmtLayer::OGRGmtLayer(GDALDataset *poDS, const char *pszFilename, /* -------------------------------------------------------------------- */ /* Create the feature definition */ /* -------------------------------------------------------------------- */ - poFeatureDefn = new OGRFeatureDefn(CPLGetBasename(pszFilename)); + poFeatureDefn = new OGRFeatureDefn(CPLGetBasenameSafe(pszFilename).c_str()); SetDescription(poFeatureDefn->GetName()); poFeatureDefn->Reference(); diff --git a/ogr/ogrsf_frmts/gpkg/gdalgeopackagerasterband.cpp b/ogr/ogrsf_frmts/gpkg/gdalgeopackagerasterband.cpp index eec28da3e7fa..59115e0083b3 100644 --- a/ogr/ogrsf_frmts/gpkg/gdalgeopackagerasterband.cpp +++ b/ogr/ogrsf_frmts/gpkg/gdalgeopackagerasterband.cpp @@ -2918,7 +2918,7 @@ GDALGPKGMBTilesLikePseudoDataset::DoPartialFlushOfPartialTilesIfNecessary() { m_nLastSpaceCheckTimestamp = nCurTimeStamp; GIntBig nFreeSpace = - VSIGetDiskFreeSpace(CPLGetDirname(m_osTempDBFilename)); + VSIGetDiskFreeSpace(CPLGetDirnameSafe(m_osTempDBFilename).c_str()); bool bTryFreeing = false; if (nFreeSpace >= 0 && nFreeSpace < 1024 * 1024 * 1024) { diff --git a/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp b/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp index c9d62d602e07..38bd3cd5f50a 100644 --- a/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp +++ b/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp @@ -5131,8 +5131,8 @@ int GDALGeoPackageDataset::Create(const char *pszFilename, int nXSize, { m_osFinalFilename = pszFilename; } - m_pszFilename = - CPLStrdup(CPLGenerateTempFilename(CPLGetFilename(pszFilename))); + m_pszFilename = CPLStrdup( + CPLGenerateTempFilenameSafe(CPLGetFilename(pszFilename)).c_str()); CPLDebug("GPKG", "Creating temporary file %s", m_pszFilename); } else diff --git a/ogr/ogrsf_frmts/gtfs/ogrgtfsdriver.cpp b/ogr/ogrsf_frmts/gtfs/ogrgtfsdriver.cpp index 421927961fc6..7a40e6f9630e 100644 --- a/ogr/ogrsf_frmts/gtfs/ogrgtfsdriver.cpp +++ b/ogr/ogrsf_frmts/gtfs/ogrgtfsdriver.cpp @@ -644,7 +644,7 @@ GDALDataset *OGRGTFSDataset::Open(GDALOpenInfo *poOpenInfo) { poDS->m_apoLayers.emplace_back( std::make_unique( - osBaseDir, CPLGetBasename(pszFilename), + osBaseDir, CPLGetBasenameSafe(pszFilename).c_str(), std::move(poCSVDataset))); } } diff --git a/ogr/ogrsf_frmts/idrisi/ogridrisidatasource.cpp b/ogr/ogrsf_frmts/idrisi/ogridrisidatasource.cpp index 9e8c7e0b9747..f99f8be92b8a 100644 --- a/ogr/ogrsf_frmts/idrisi/ogridrisidatasource.cpp +++ b/ogr/ogrsf_frmts/idrisi/ogridrisidatasource.cpp @@ -141,8 +141,9 @@ int OGRIdrisiDataSource::Open(const char *pszFilename) const char *pszMinY = CSLFetchNameValue(papszVDC, "min. Y"); const char *pszMaxY = CSLFetchNameValue(papszVDC, "max. Y"); - OGRIdrisiLayer *poLayer = new OGRIdrisiLayer( - pszFilename, CPLGetBasename(pszFilename), fpVCT, eType, pszWTKString); + OGRIdrisiLayer *poLayer = + new OGRIdrisiLayer(pszFilename, CPLGetBasenameSafe(pszFilename).c_str(), + fpVCT, eType, pszWTKString); papoLayers = static_cast(CPLMalloc(sizeof(OGRLayer *))); papoLayers[nLayers++] = poLayer; diff --git a/ogr/ogrsf_frmts/ili/ogrili2datasource.cpp b/ogr/ogrsf_frmts/ili/ogrili2datasource.cpp index 5e9fcb8a3996..b9207acd7e0d 100644 --- a/ogr/ogrsf_frmts/ili/ogrili2datasource.cpp +++ b/ogr/ogrsf_frmts/ili/ogrili2datasource.cpp @@ -196,8 +196,8 @@ int OGRILI2DataSource::Create(const char *pszFilename, { if (EQUAL(CPLGetExtensionSafe(pszName).c_str(), "zip")) { - char *pszNewName = - CPLStrdup(CPLFormFilename(pszName, "out.xtf", nullptr)); + char *pszNewName = CPLStrdup( + CPLFormFilenameSafe(pszName, "out.xtf", nullptr).c_str()); CPLFree(pszName); pszName = pszNewName; } diff --git a/ogr/ogrsf_frmts/jml/ogrjmldataset.cpp b/ogr/ogrsf_frmts/jml/ogrjmldataset.cpp index 966c441e4bad..9a446f73117e 100644 --- a/ogr/ogrsf_frmts/jml/ogrjmldataset.cpp +++ b/ogr/ogrsf_frmts/jml/ogrjmldataset.cpp @@ -103,8 +103,8 @@ GDALDataset *OGRJMLDataset::Open(GDALOpenInfo *poOpenInfo) poDS->fp = poOpenInfo->fpL; poOpenInfo->fpL = nullptr; - poDS->poLayer = new OGRJMLLayer(CPLGetBasename(poOpenInfo->pszFilename), - poDS, poDS->fp); + poDS->poLayer = new OGRJMLLayer( + CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str(), poDS, poDS->fp); return poDS; #endif diff --git a/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp b/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp index 09e907019c4b..318741dc565f 100644 --- a/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp +++ b/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp @@ -402,8 +402,8 @@ bool OGRLIBKMLDataSource::WriteKmz() EQUAL(CPLGetConfigOption("CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE", ""), "FORCED")) { - osTmpFilename = - CPLGenerateTempFilename(CPLGetBasename(GetDescription())); + osTmpFilename = CPLGenerateTempFilename( + CPLGetBasenameSafe(GetDescription()).c_str()); } void *hZIP = CPLCreateZip(osTmpFilename.empty() ? GetDescription() @@ -1123,7 +1123,7 @@ int OGRLIBKMLDataSource::OpenKml(const char *pszFilename, int bUpdateIn) /***** if there is placemarks in the root its a layer *****/ if (nPlacemarks) { - std::string layername_default(CPLGetBasename(pszFilename)); + std::string layername_default(CPLGetBasenameSafe(pszFilename)); if (m_poKmlDSContainer->has_name()) { @@ -1327,7 +1327,7 @@ int OGRLIBKMLDataSource::OpenKmz(const char *pszFilename, int bUpdateIn) /***** if there is placemarks in the root its a layer *****/ if (nPlacemarks) { - std::string layername_default(CPLGetBasename(pszFilename)); + std::string layername_default(CPLGetBasenameSafe(pszFilename)); if (poKmlContainer->has_name()) { @@ -1376,7 +1376,7 @@ int OGRLIBKMLDataSource::OpenDir(const char *pszFilename, int bUpdateIn) for (int iFile = 0; iFile < nFiles; iFile++) { /***** make sure its a .kml file *****/ - if (!EQUAL(CPLGetExtension(papszDirList[iFile]), "kml")) + if (!EQUAL(CPLGetExtensionSafe(papszDirList[iFile]).c_str(), "kml")) continue; /***** read the file *****/ @@ -1454,7 +1454,7 @@ int OGRLIBKMLDataSource::OpenDir(const char *pszFilename, int bUpdateIn) const std::string osLayerName = poKmlContainer->has_name() ? poKmlContainer->get_name() - : std::string(CPLGetBasename(osFilePath.c_str())); + : std::string(CPLGetBasenameSafe(osFilePath.c_str())); /***** create the layer *****/ AddLayer(osLayerName.c_str(), wkbUnknown, nullptr, this, @@ -1493,7 +1493,7 @@ static bool CheckIsKMZ(const char *pszFilename) bool bFoundKML = false; while (papszIter && *papszIter) { - if (EQUAL(CPLGetExtension(*papszIter), "kml")) + if (EQUAL(CPLGetExtensionSafe(*papszIter).c_str(), "kml")) { bFoundKML = true; break; diff --git a/ogr/ogrsf_frmts/libkml/ogrlibkmlfeature.cpp b/ogr/ogrsf_frmts/libkml/ogrlibkmlfeature.cpp index d0785a8cdc82..4e4347ebb104 100644 --- a/ogr/ogrsf_frmts/libkml/ogrlibkmlfeature.cpp +++ b/ogr/ogrsf_frmts/libkml/ogrlibkmlfeature.cpp @@ -713,12 +713,13 @@ FeaturePtr feat2kml(OGRLIBKMLDataSource *poOgrDS, OGRLIBKMLLayer *poOgrLayer, { if (STARTS_WITH(pszURL, "http")) alias->set_targethref(CPLSPrintf( - "%s/%s", CPLGetPath(pszURL), + "%s/%s", + CPLGetPathSafe(pszURL).c_str(), osImage.c_str())); else - alias->set_targethref( - CPLFormFilename(CPLGetPath(pszURL), - osImage, nullptr)); + alias->set_targethref(CPLFormFilename( + CPLGetPathSafe(pszURL).c_str(), + osImage, nullptr)); } else alias->set_targethref(osImage); diff --git a/ogr/ogrsf_frmts/libkml/ogrlibkmlstyle.cpp b/ogr/ogrsf_frmts/libkml/ogrlibkmlstyle.cpp index d7cc67c28236..472b7fc10b77 100644 --- a/ogr/ogrsf_frmts/libkml/ogrlibkmlstyle.cpp +++ b/ogr/ogrsf_frmts/libkml/ogrlibkmlstyle.cpp @@ -792,9 +792,10 @@ static StyleSelectorPtr StyleFromStyleURL(const StyleMapPtr &stylemap, /***** try it as a url then a file *****/ VSILFILE *fp = nullptr; - if ((fp = - VSIFOpenL(CPLFormFilename("/vsicurl/", pszUrlTmp, nullptr), - "r")) != nullptr || + if ((fp = VSIFOpenL( + CPLFormFilenameSafe("/vsicurl/", pszUrlTmp, nullptr) + .c_str(), + "r")) != nullptr || (fp = VSIFOpenL(pszUrlTmp, "r")) != nullptr) { char szbuf[1025] = {}; diff --git a/ogr/ogrsf_frmts/lvbag/ogrlvbagdriver.cpp b/ogr/ogrsf_frmts/lvbag/ogrlvbagdriver.cpp index c857b32f2943..1f2899552556 100644 --- a/ogr/ogrsf_frmts/lvbag/ogrlvbagdriver.cpp +++ b/ogr/ogrsf_frmts/lvbag/ogrlvbagdriver.cpp @@ -77,7 +77,7 @@ GDALDataset *OGRLVBAGDriverOpen(GDALOpenInfo *poOpenInfo) char **papszNames = VSIReadDir(pszFilename); for (int i = 0; papszNames != nullptr && papszNames[i] != nullptr; ++i) { - if (!EQUAL(CPLGetExtension(papszNames[i]), "xml")) + if (!EQUAL(CPLGetExtensionSafe(papszNames[i]).c_str(), "xml")) continue; const CPLString oSubFilename = diff --git a/ogr/ogrsf_frmts/lvbag/ogrlvbaglayer.cpp b/ogr/ogrsf_frmts/lvbag/ogrlvbaglayer.cpp index b5550ee3f3f9..fb88e82bbbc4 100644 --- a/ogr/ogrsf_frmts/lvbag/ogrlvbaglayer.cpp +++ b/ogr/ogrsf_frmts/lvbag/ogrlvbaglayer.cpp @@ -39,7 +39,7 @@ OGRLVBAGLayer::OGRLVBAGLayer(const char *pszFilename, OGRLayerPool *poPoolIn, nAttributeElementDepth{0}, eAddressRefState{AddressRefState::ADDRESS_PRIMARY}, bCollectData{false} { - SetDescription(CPLGetBasename(pszFilename)); + SetDescription(CPLGetBasenameSafe(pszFilename).c_str()); poFeatureDefn->Reference(); } diff --git a/ogr/ogrsf_frmts/mapml/ogrmapmldataset.cpp b/ogr/ogrsf_frmts/mapml/ogrmapmldataset.cpp index 55a20dcde61a..eebd79906200 100644 --- a/ogr/ogrsf_frmts/mapml/ogrmapmldataset.cpp +++ b/ogr/ogrsf_frmts/mapml/ogrmapmldataset.cpp @@ -218,7 +218,8 @@ GDALDataset *OGRMapMLReaderDataset::Open(GDALOpenInfo *poOpenInfo) CPLXMLNode *psBody = CPLGetXMLNode(psRoot, "=mapml.body"); if (psBody == nullptr) return nullptr; - const CPLString osDefaultLayerName(CPLGetBasename(poOpenInfo->pszFilename)); + const CPLString osDefaultLayerName( + CPLGetBasenameSafe(poOpenInfo->pszFilename)); std::set oSetLayerNames; for (auto psNode = psBody->psChild; psNode; psNode = psNode->psNext) { diff --git a/ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp b/ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp index 38ebde55979a..00f0c076dc34 100644 --- a/ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp +++ b/ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp @@ -35,7 +35,8 @@ OGRMiraMonLayer::OGRMiraMonLayer(GDALDataset *poDS, const char *pszFilename, /* -------------------------------------------------------------------- */ /* Create the feature definition */ /* -------------------------------------------------------------------- */ - m_poFeatureDefn = new OGRFeatureDefn(CPLGetBasename(pszFilename)); + m_poFeatureDefn = + new OGRFeatureDefn(CPLGetBasenameSafe(pszFilename).c_str()); SetDescription(m_poFeatureDefn->GetName()); m_poFeatureDefn->Reference(); @@ -2858,78 +2859,85 @@ void OGRMiraMonLayer::AddToFileList(CPLStringList &oFileList) oFileList.AddStringDirectly( VSIGetCanonicalFilename(phMiraMonLayer->pszSrcLayerName)); char *pszMMExt = - CPLStrdup(CPLGetExtension(phMiraMonLayer->pszSrcLayerName)); + CPLStrdup(CPLGetExtensionSafe(phMiraMonLayer->pszSrcLayerName).c_str()); if (phMiraMonLayer->bIsPoint) { // As it's explicit on documentation a point has also two more files: // FILE_NAME_WITHOUT_EXTENSION.pnt --> FILE_NAME_WITHOUT_EXTENSION + T.rel - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? "T.rel" : "T.REL", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.pnt --> FILE_NAME_WITHOUT_EXTENSION + T.dbf - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? "T.dbf" : "T.DBF", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); } else if (phMiraMonLayer->bIsArc && !phMiraMonLayer->bIsPolygon) { // As it's explicit on documentation a point has also five more files: // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + A.rel - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'a') ? "A.rel" : "A.REL", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + A.dbf - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'a') ? "A.dbf" : "A.DBF", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + .nod - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'a') ? ".nod" : ".NOD", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + N.rel - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'a') ? "N.rel" : "N.REL", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + N.dbf - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'a') ? "N.dbf" : "N.DBF", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); } else if (phMiraMonLayer->bIsPolygon) { @@ -2938,18 +2946,20 @@ void OGRMiraMonLayer::AddToFileList(CPLStringList &oFileList) char szArcFileName[MM_CPL_PATH_BUF_SIZE]; // FILE_NAME_WITHOUT_EXTENSION.pol --> FILE_NAME_WITHOUT_EXTENSION + P.rel - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? "P.rel" : "P.REL", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); // The name of the arc is in THIS metadata file char *pszArcLayerName = MMReturnValueFromSectionINIFile( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr), + CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr), SECTION_OVVW_ASPECTES_TECNICS, KEY_ArcSource); if (!pszArcLayerName) { @@ -2961,28 +2971,29 @@ void OGRMiraMonLayer::AddToFileList(CPLStringList &oFileList) MM_RemoveInitial_and_FinalQuotationMarks(szArcFileName); // If extension is not specified ".arc" will be used - if (MMIsEmptyString(CPLGetExtension(pszArcLayerName))) + if (MMIsEmptyString(CPLGetExtensionSafe(pszArcLayerName).c_str())) CPLStrlcat(szArcFileName, (pszMMExt[0] == 'p') ? ".arc" : ".ARC", MM_CPL_PATH_BUF_SIZE); CPLFree(pszArcLayerName); - szCompleteArcFileName = - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szArcFileName, nullptr); + szCompleteArcFileName = CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szArcFileName, nullptr); // The arc that has the coordinates of the polygon oFileList.AddStringDirectly( VSIGetCanonicalFilename(szCompleteArcFileName)); // FILE_NAME_WITHOUT_EXTENSION.pol --> FILE_NAME_WITHOUT_EXTENSION + P.dbf - CPLStrlcpy(szAuxFile, CPLGetBasename(phMiraMonLayer->pszSrcLayerName), + CPLStrlcpy(szAuxFile, + CPLGetBasenameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? "P.dbf" : "P.DBF", MM_CPL_PATH_BUF_SIZE); - oFileList.AddStringDirectly(VSIGetCanonicalFilename( - CPLFormFilename(CPLGetDirname(phMiraMonLayer->pszSrcLayerName), - szAuxFile, nullptr))); + oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( + CPLGetDirnameSafe(phMiraMonLayer->pszSrcLayerName).c_str(), + szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + A.rel const char *pszBaseArcName = CPLGetBasename(szCompleteArcFileName); diff --git a/ogr/ogrsf_frmts/mitab/mitab_ogr_datasource.cpp b/ogr/ogrsf_frmts/mitab/mitab_ogr_datasource.cpp index 89e8fb83eb67..52fe61c8b5c9 100644 --- a/ogr/ogrsf_frmts/mitab/mitab_ogr_datasource.cpp +++ b/ogr/ogrsf_frmts/mitab/mitab_ogr_datasource.cpp @@ -86,7 +86,7 @@ int OGRTABDataSource::Create(const char *pszName, char **papszOptions) // Create a new empty directory. VSIStatBufL sStat; - if (strlen(CPLGetExtension(pszName)) == 0) + if (strlen(CPLGetExtensionSafe(pszName).c_str()) == 0) { if (VSIStatL(pszName, &sStat) == 0) { @@ -145,7 +145,7 @@ int OGRTABDataSource::Create(const char *pszName, char **papszOptions) m_papoLayers = static_cast(CPLMalloc(sizeof(void *))); m_papoLayers[0] = poFile; - m_pszDirectory = CPLStrdup(CPLGetPath(pszName)); + m_pszDirectory = CPLStrdup(CPLGetPathSafe(pszName).c_str()); m_bSingleFile = TRUE; } @@ -178,7 +178,8 @@ int OGRTABDataSource::Open(GDALOpenInfo *poOpenInfo, int bTestOpen) m_papoLayers = static_cast(CPLMalloc(sizeof(void *))); m_papoLayers[0] = poFile; - m_pszDirectory = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename)); + m_pszDirectory = + CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); m_bSingleFile = TRUE; m_bSingleLayerAlreadyCreated = TRUE; @@ -325,8 +326,9 @@ OGRTABDataSource::ICreateLayer(const char *pszLayerName, { if (m_bCreateMIF) { - pszFullFilename = - CPLStrdup(CPLFormFilename(m_pszDirectory, pszLayerName, "mif")); + pszFullFilename = CPLStrdup( + CPLFormFilenameSafe(m_pszDirectory, pszLayerName, "mif") + .c_str()); poFile = new MIFFile(this); @@ -339,8 +341,9 @@ OGRTABDataSource::ICreateLayer(const char *pszLayerName, } else { - pszFullFilename = - CPLStrdup(CPLFormFilename(m_pszDirectory, pszLayerName, "tab")); + pszFullFilename = CPLStrdup( + CPLFormFilenameSafe(m_pszDirectory, pszLayerName, "tab") + .c_str()); TABFile *poTABFile = new TABFile(this); @@ -481,8 +484,8 @@ char **OGRTABDataSource::GetFileList() static const char *const apszTABExtensions[] = {"tab", "map", "ind", "dat", "id", nullptr}; const char *const *papszExtensions = nullptr; - if (EQUAL(CPLGetExtension(GetDescription()), "mif") || - EQUAL(CPLGetExtension(GetDescription()), "mid")) + if (EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "mif") || + EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "mid")) { papszExtensions = apszMIFExtensions; } diff --git a/ogr/ogrsf_frmts/mitab/mitab_tabfile.cpp b/ogr/ogrsf_frmts/mitab/mitab_tabfile.cpp index a402d2874223..ec06d3c187c4 100644 --- a/ogr/ogrsf_frmts/mitab/mitab_tabfile.cpp +++ b/ogr/ogrsf_frmts/mitab/mitab_tabfile.cpp @@ -552,7 +552,7 @@ int TABFile::Open(const char *pszFname, TABAccess eAccess, if (!bHasIndex) { const char *pszIndFilename = CPLFormCIFilename( - CPLGetPath(pszFname), CPLGetBasename(pszFname), + CPLGetPath(pszFname), CPLGetBasenameSafe(pszFname).c_str(), (bUpperCase) ? "IND" : "ind"); VSIStatBufL sStat; if (VSIStatL(pszIndFilename, &sStat) == 0) diff --git a/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp b/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp index a1505b36ba0f..245274328fbe 100644 --- a/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp +++ b/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp @@ -1973,9 +1973,9 @@ static int OGRMVTDriverIdentify(GDALOpenInfo *poOpenInfo) { // tileserver-gl metadata file: // If opening /path/to/foo/0, try looking for /path/to/foo.json - CPLString osParentDir(CPLGetPath(poOpenInfo->pszFilename)); + CPLString osParentDir(CPLGetPathSafe(poOpenInfo->pszFilename)); osMetadataFile = - CPLFormFilename(CPLGetPath(osParentDir), + CPLFormFilename(CPLGetPathSafe(osParentDir).c_str(), CPLGetFilename(osParentDir), "json"); if (VSIStatL(osMetadataFile, &sStat) == 0) { @@ -1999,7 +1999,8 @@ static int OGRMVTDriverIdentify(GDALOpenInfo *poOpenInfo) poOpenInfo->papszOpenOptions, "TILE_EXTENSION", "pbf")); for (int i = 0; i < aosSubDirContent.Count(); i++) { - if (CPLGetValueType(CPLGetBasename(aosSubDirContent[i])) == + if (CPLGetValueType( + CPLGetBasenameSafe(aosSubDirContent[i]).c_str()) == CPL_VALUE_INTEGER) { CPLString osExtension( @@ -2582,9 +2583,10 @@ GDALDataset *OGRMVTDataset::OpenDirectory(GDALOpenInfo *poOpenInfo) // tileserver-gl metadata file: // If opening /path/to/foo/0, try looking for // /path/to/foo.json - CPLString osParentDir(CPLGetPath(poOpenInfo->pszFilename)); + CPLString osParentDir( + CPLGetPathSafe(poOpenInfo->pszFilename)); osMetadataFile = - CPLFormFilename(CPLGetPath(osParentDir), + CPLFormFilename(CPLGetPathSafe(osParentDir).c_str(), CPLGetFilename(osParentDir), "json"); continue; } @@ -2606,7 +2608,7 @@ GDALDataset *OGRMVTDataset::OpenDirectory(GDALOpenInfo *poOpenInfo) { // tileserver-gl metadata file: // If opening /path/to/foo/0, try looking for /path/to/foo.json - CPLString osParentDir(CPLGetPath(poOpenInfo->pszFilename)); + CPLString osParentDir(CPLGetPathSafe(poOpenInfo->pszFilename)); osMetadataFile = CPLFormFilename( CPLGetPath(osParentDir), CPLGetFilename(osParentDir), "json"); bMetadataFileExists = (VSIStatL(osMetadataFile, &sStat) == 0); @@ -2661,7 +2663,8 @@ GDALDataset *OGRMVTDataset::OpenDirectory(GDALOpenInfo *poOpenInfo) { if (bTryToListDir) { - if (CPLGetValueType(CPLGetBasename(aosSubDirContent[j])) != + if (CPLGetValueType( + CPLGetBasenameSafe(aosSubDirContent[j]).c_str()) != CPL_VALUE_INTEGER) { continue; @@ -2967,8 +2970,9 @@ GDALDataset *OGRMVTDataset::Open(GDALOpenInfo *poOpenInfo) } CPLString osY = CPLGetBasename(osFilename); - CPLString osX = CPLGetBasename(CPLGetPath(osFilename)); - CPLString osZ = CPLGetBasename(CPLGetPath(CPLGetPath(osFilename))); + CPLString osX = CPLGetBasename(CPLGetPathSafe(osFilename).c_str()); + CPLString osZ = + CPLGetBasename(CPLGetPathSafe(CPLGetPath(osFilename)).c_str()); size_t nPos = osY.find('.'); if (nPos != std::string::npos) osY.resize(nPos); @@ -2983,9 +2987,9 @@ GDALDataset *OGRMVTDataset::Open(GDALOpenInfo *poOpenInfo) CPLGetValueType(osY) == CPL_VALUE_INTEGER && CPLGetValueType(osZ) == CPL_VALUE_INTEGER) { - osMetadataFile = - CPLFormFilename(CPLGetPath(CPLGetPath(CPLGetPath(osFilename))), - "metadata.json", nullptr); + osMetadataFile = CPLFormFilename( + CPLGetPath(CPLGetPathSafe(CPLGetPath(osFilename)).c_str()), + "metadata.json", nullptr); if (osMetadataFile.find("/vsigzip/") == 0) { osMetadataFile = osMetadataFile.substr(strlen("/vsigzip/")); @@ -3093,7 +3097,8 @@ GDALDataset *OGRMVTDataset::Open(GDALOpenInfo *poOpenInfo) int nX = 0; int nY = 0; int nZ = 0; - CPLString osBasename(CPLGetBasename(CPLGetBasename(osFilename))); + CPLString osBasename( + CPLGetBasenameSafe(CPLGetBasenameSafe(osFilename).c_str())); if (sscanf(osBasename, "%d-%d-%d", &nZ, &nX, &nY) == 3 || sscanf(osBasename, "%d_%d_%d", &nZ, &nX, &nY) == 3) { @@ -6134,8 +6139,8 @@ GDALDataset *OGRMVTWriterDataset::Create(const char *pszFilename, int nXSize, } } - poDS->m_osName = - CSLFetchNameValueDef(papszOptions, "NAME", CPLGetBasename(pszFilename)); + poDS->m_osName = CSLFetchNameValueDef( + papszOptions, "NAME", CPLGetBasenameSafe(pszFilename).c_str()); poDS->m_osDescription = CSLFetchNameValueDef(papszOptions, "DESCRIPTION", poDS->m_osDescription.c_str()); poDS->m_osType = diff --git a/ogr/ogrsf_frmts/odbc/ogrodbcdrivercore.cpp b/ogr/ogrsf_frmts/odbc/ogrodbcdrivercore.cpp index 22ec4aa32ae1..9b74f7a2469a 100644 --- a/ogr/ogrsf_frmts/odbc/ogrodbcdrivercore.cpp +++ b/ogr/ogrsf_frmts/odbc/ogrodbcdrivercore.cpp @@ -40,7 +40,8 @@ int OGRODBCDriverIdentify(GDALOpenInfo *poOpenInfo) if (STARTS_WITH_CI(poOpenInfo->pszFilename, "ODBC:")) return TRUE; - const char *psExtension(CPLGetExtension(poOpenInfo->pszFilename)); + const char *psExtension( + CPLGetExtensionSafe(poOpenInfo->pszFilename).c_str()); if (EQUAL(psExtension, "mdb")) return -1; // Could potentially be a PGeo MDB database diff --git a/ogr/ogrsf_frmts/openfilegdb/filegdbindex_write.cpp b/ogr/ogrsf_frmts/openfilegdb/filegdbindex_write.cpp index 242d431fcb73..df6ea6029461 100644 --- a/ogr/ogrsf_frmts/openfilegdb/filegdbindex_write.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/filegdbindex_write.cpp @@ -54,7 +54,8 @@ void FileGDBTable::RemoveIndices() osUCIndexFieldName.toupper(); if (osUCIndexFieldName == osUCGeomFieldName) { - VSIUnlink(CPLResetExtension(m_osFilename.c_str(), "spx")); + VSIUnlink( + CPLResetExtensionSafe(m_osFilename.c_str(), "spx").c_str()); } else { @@ -264,8 +265,9 @@ void FileGDBTable::CreateGdbIndexesFile() WriteUInt16(abyBuffer, 0); // unknown semantics } - VSILFILE *fp = - VSIFOpenL(CPLResetExtension(m_osFilename.c_str(), "gdbindexes"), "wb"); + VSILFILE *fp = VSIFOpenL( + CPLResetExtensionSafe(m_osFilename.c_str(), "gdbindexes").c_str(), + "wb"); if (fp == nullptr) return; CPL_IGNORE_RET_VAL(VSIFWriteL(abyBuffer.data(), abyBuffer.size(), 1, fp)); diff --git a/ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp b/ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp index 3ed8d567843b..025264628b7c 100644 --- a/ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp @@ -875,7 +875,7 @@ bool FileGDBTable::Open(const char *pszFilename, bool bUpdate, !CPLTestBool(CPLGetConfigOption( "OPENFILEGDB_IGNORE_GDBTABLX", "false")))) { - osTableXName = CPLFormFilename(CPLGetPath(pszFilename), + osTableXName = CPLFormFilename(CPLGetPathSafe(pszFilename).c_str(), CPLGetBasename(pszFilename), "gdbtablx"); m_fpTableX = VSIFOpenL(osTableXName, m_bUpdate ? "r+b" : "rb"); if (m_fpTableX == nullptr) @@ -2479,7 +2479,7 @@ int FileGDBTable::GetIndexCount() m_bHasReadGDBIndexes = TRUE; const char *pszIndexesName = - CPLFormFilename(CPLGetPath(m_osFilename.c_str()), + CPLFormFilename(CPLGetPathSafe(m_osFilename.c_str()).c_str(), CPLGetBasename(m_osFilename.c_str()), "gdbindexes"); VSILFILE *fpIndexes = VSIFOpenL(pszIndexesName, "rb"); VSIStatBufL sStat; @@ -2670,7 +2670,7 @@ bool FileGDBTable::HasSpatialIndex() if (m_nHasSpatialIndex < 0) { const char *pszSpxName = - CPLFormFilename(CPLGetPath(m_osFilename.c_str()), + CPLFormFilename(CPLGetPathSafe(m_osFilename.c_str()).c_str(), CPLGetBasename(m_osFilename.c_str()), "spx"); VSIStatBufL sStat; m_nHasSpatialIndex = diff --git a/ogr/ogrsf_frmts/openfilegdb/filegdbtable_freelist.cpp b/ogr/ogrsf_frmts/openfilegdb/filegdbtable_freelist.cpp index d9ed1343807c..b57682d59871 100644 --- a/ogr/ogrsf_frmts/openfilegdb/filegdbtable_freelist.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/filegdbtable_freelist.cpp @@ -744,7 +744,7 @@ void FileGDBTable::DeleteFreeList() { m_bFreelistCanBeDeleted = false; m_nHasFreeList = -1; - VSIUnlink(CPLResetExtension(m_osFilename.c_str(), "freelist")); + VSIUnlink(CPLResetExtensionSafe(m_osFilename.c_str(), "freelist").c_str()); } } /* namespace OpenFileGDB */ diff --git a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource.cpp b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource.cpp index abeef39621f1..8b73688c1afc 100644 --- a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource.cpp @@ -193,12 +193,12 @@ bool OGROpenFileGDBDataSource::Open(const GDALOpenInfo *poOpenInfo, m_osDirName = CPLGetPath(m_osDirName); } - if (EQUAL(CPLGetExtension(m_osDirName), "zip") && + if (EQUAL(CPLGetExtensionSafe(m_osDirName).c_str(), "zip") && !STARTS_WITH(m_osDirName, "/vsizip/")) { m_osDirName = "/vsizip/" + m_osDirName; } - else if (EQUAL(CPLGetExtension(m_osDirName), "tar") && + else if (EQUAL(CPLGetExtensionSafe(m_osDirName).c_str(), "tar") && !STARTS_WITH(m_osDirName, "/vsitar/")) { m_osDirName = "/vsitar/" + m_osDirName; @@ -213,7 +213,7 @@ bool OGROpenFileGDBDataSource::Open(const GDALOpenInfo *poOpenInfo, for (int i = 0; papszDir && papszDir[i] != nullptr; i++) { VSIStatBufL sStat; - if (EQUAL(CPLGetExtension(papszDir[i]), "gdb") && + if (EQUAL(CPLGetExtensionSafe(papszDir[i]).c_str(), "gdb") && VSIStatL(CPLSPrintf("%s/%s", m_osDirName.c_str(), papszDir[i]), &sStat) == 0 && VSI_ISDIR(sStat.st_mode)) diff --git a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp index 84ce62de3798..1da21a6cb4cb 100644 --- a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp @@ -1306,7 +1306,8 @@ bool OGROpenFileGDBDataSource::Create(const char *pszName) { // Write "gdb" file - const std::string osFilename(CPLFormFilename(pszName, "gdb", nullptr)); + const std::string osFilename( + CPLFormFilenameSafe(pszName, "gdb", nullptr)); VSILFILE *fp = VSIFOpenL(osFilename.c_str(), "wb"); if (!fp) return false; @@ -1524,7 +1525,9 @@ OGRErr OGROpenFileGDBDataSource::DeleteLayer(int iLayer) { if (STARTS_WITH(*papszIter, osFilenameBase.c_str())) { - VSIUnlink(CPLFormFilename(osDirname.c_str(), *papszIter, nullptr)); + VSIUnlink( + CPLFormFilenameSafe(osDirname.c_str(), *papszIter, nullptr) + .c_str()); } } CSLDestroy(papszFiles); diff --git a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdriver.cpp b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdriver.cpp index 05c1c6c3b522..8874dc5813bd 100644 --- a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdriver.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdriver.cpp @@ -57,9 +57,10 @@ static GDALDataset *OGROpenFileGDBDriverOpen(GDALOpenInfo *poOpenInfo) { if (strcmp(papszFiles[i], CPLGetFilename(osOrigFilename)) != 0) { - CPLCopyFile(CPLFormFilename(CPLGetPath(osOrigFilename), - papszFiles[i], NULL), - CPLFormFilename(pszSrcDir, papszFiles[i], NULL)); + CPLCopyFile( + CPLFormFilename(CPLGetPathSafe(osOrigFilename).c_str(), + papszFiles[i], NULL), + CPLFormFilename(pszSrcDir, papszFiles[i], NULL)); } } CSLDestroy(papszFiles); diff --git a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdrivercore.cpp b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdrivercore.cpp index bc294654d90a..56957e0dfd9a 100644 --- a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdrivercore.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdrivercore.cpp @@ -106,19 +106,21 @@ GDALIdentifyEnum OGROpenFileGDBDriverIdentify(GDALOpenInfo *poOpenInfo, } #ifdef FOR_FUSIL /* To be able to test fuzzer on any auxiliary files used (indexes, etc.) */ - else if (strlen(CPLGetBasename(pszFilename)) == 9 && + else if (strlen(CPLGetBasenameSafe(pszFilename).c_str()) == 9 && CPLGetBasename(pszFilename)[0] == 'a') { - pszFilename = CPLFormFilename(CPLGetPath(pszFilename), + pszFilename = CPLFormFilename(CPLGetPathSafe(pszFilename).c_str(), CPLGetBasename(pszFilename), "gdbtable"); return GDAL_IDENTIFY_TRUE; } - else if (strlen(CPLGetBasename(CPLGetBasename(pszFilename))) == 9 && - CPLGetBasename(CPLGetBasename(pszFilename))[0] == 'a') + else if (strlen(CPLGetBasenameSafe(CPLGetBasename(pszFilename)).c_str()) == + 9 && + CPLGetBasename(CPLGetBasenameSafe(pszFilename).c_str())[0] == 'a') { pszFilename = CPLFormFilename( CPLGetPath(pszFilename), - CPLGetBasename(CPLGetBasename(pszFilename)), "gdbtable"); + CPLGetBasename(CPLGetBasenameSafe(pszFilename).c_str()), + "gdbtable"); return GDAL_IDENTIFY_TRUE; } #endif diff --git a/ogr/ogrsf_frmts/parquet/ogrparquetdriver.cpp b/ogr/ogrsf_frmts/parquet/ogrparquetdriver.cpp index 0f493e16a7ad..59538059797e 100644 --- a/ogr/ogrsf_frmts/parquet/ogrparquetdriver.cpp +++ b/ogr/ogrsf_frmts/parquet/ogrparquetdriver.cpp @@ -47,8 +47,8 @@ static GDALDataset *OpenFromDatasetFactory( const bool bIsVSI = STARTS_WITH(osBasePath.c_str(), "/vsi"); auto poDS = std::make_unique(poMemoryPool); auto poLayer = std::make_unique( - poDS.get(), CPLGetBasename(osBasePath.c_str()), bIsVSI, dataset, - papszOpenOptions); + poDS.get(), CPLGetBasenameSafe(osBasePath.c_str()).c_str(), bIsVSI, + dataset, papszOpenOptions); poDS->SetLayer(std::move(poLayer)); poDS->SetFileSystem(fs); return poDS.release(); @@ -459,7 +459,7 @@ static GDALDataset *OGRParquetDriverOpen(GDALOpenInfo *poOpenInfo) auto poDS = std::make_unique(poMemoryPool); auto poLayer = std::make_unique( - poDS.get(), CPLGetBasename(osFilename.c_str()), + poDS.get(), CPLGetBasenameSafe(osFilename.c_str()).c_str(), std::move(arrow_reader), poOpenInfo->papszOpenOptions); // For debug purposes: return a layer with the extent of each row group diff --git a/ogr/ogrsf_frmts/pmtiles/ogrpmtileswriterdataset.cpp b/ogr/ogrsf_frmts/pmtiles/ogrpmtileswriterdataset.cpp index 9267ccb39fef..26b0b7d80920 100644 --- a/ogr/ogrsf_frmts/pmtiles/ogrpmtileswriterdataset.cpp +++ b/ogr/ogrsf_frmts/pmtiles/ogrpmtileswriterdataset.cpp @@ -84,7 +84,8 @@ bool OGRPMTilesWriterDataset::Create(const char *pszFilename, osTmpFile += ".tmp.mbtiles"; if (!aosOptions.FetchNameValue("NAME")) - aosOptions.SetNameValue("NAME", CPLGetBasename(pszFilename)); + aosOptions.SetNameValue("NAME", + CPLGetBasenameSafe(pszFilename).c_str()); m_poMBTilesWriterDataset.reset(OGRMVTWriterDatasetCreate( osTmpFile.c_str(), 0, 0, 0, GDT_Unknown, aosOptions.List())); diff --git a/ogr/ogrsf_frmts/s57/s57filecollector.cpp b/ogr/ogrsf_frmts/s57/s57filecollector.cpp index ab56a7b1b5c9..1773d0ac2f82 100644 --- a/ogr/ogrsf_frmts/s57/s57filecollector.cpp +++ b/ogr/ogrsf_frmts/s57/s57filecollector.cpp @@ -100,20 +100,23 @@ char **S57FileCollector(const char *pszDataset) /* correct name for the ENC_ROOT directory if available and */ /* build a base path for our purposes. */ /* -------------------------------------------------------------------- */ - char *pszCatDir = CPLStrdup(CPLGetPath(pszDataset)); + char *pszCatDir = CPLStrdup(CPLGetPathSafe(pszDataset).c_str()); char *pszRootDir = nullptr; - if (CPLStat(CPLFormFilename(pszCatDir, "ENC_ROOT", nullptr), &sStatBuf) == - 0 && + if (CPLStat(CPLFormFilenameSafe(pszCatDir, "ENC_ROOT", nullptr).c_str(), + &sStatBuf) == 0 && VSI_ISDIR(sStatBuf.st_mode)) { - pszRootDir = CPLStrdup(CPLFormFilename(pszCatDir, "ENC_ROOT", nullptr)); + pszRootDir = CPLStrdup( + CPLFormFilenameSafe(pszCatDir, "ENC_ROOT", nullptr).c_str()); } - else if (CPLStat(CPLFormFilename(pszCatDir, "enc_root", nullptr), - &sStatBuf) == 0 && + else if (CPLStat( + CPLFormFilenameSafe(pszCatDir, "enc_root", nullptr).c_str(), + &sStatBuf) == 0 && VSI_ISDIR(sStatBuf.st_mode)) { - pszRootDir = CPLStrdup(CPLFormFilename(pszCatDir, "enc_root", nullptr)); + pszRootDir = CPLStrdup( + CPLFormFilenameSafe(pszCatDir, "enc_root", nullptr).c_str()); } if (pszRootDir) diff --git a/ogr/ogrsf_frmts/s57/s57reader.cpp b/ogr/ogrsf_frmts/s57/s57reader.cpp index 1584b1cc5cda..d80a151ebf6a 100644 --- a/ogr/ogrsf_frmts/s57/s57reader.cpp +++ b/ogr/ogrsf_frmts/s57/s57reader.cpp @@ -3412,8 +3412,8 @@ bool S57Reader::FindAndApplyUpdates(const char *pszPath) DDFModule oUpdateModule; // trying current dir first - char *pszUpdateFilename = - CPLStrdup(CPLResetExtension(pszPath, extension.c_str())); + char *pszUpdateFilename = CPLStrdup( + CPLResetExtensionSafe(pszPath, extension.c_str()).c_str()); VSILFILE *file = VSIFOpenL(pszUpdateFilename, "r"); if (file) @@ -3430,14 +3430,16 @@ bool S57Reader::FindAndApplyUpdates(const char *pszPath) } else // File is store on Primar generated CD. { - char *pszBaseFileDir = CPLStrdup(CPLGetDirname(pszPath)); - char *pszFileDir = CPLStrdup(CPLGetDirname(pszBaseFileDir)); + char *pszBaseFileDir = + CPLStrdup(CPLGetDirnameSafe(pszPath).c_str()); + char *pszFileDir = + CPLStrdup(CPLGetDirnameSafe(pszBaseFileDir).c_str()); CPLString remotefile(pszFileDir); remotefile.append("/"); remotefile.append(dirname); remotefile.append("/"); - remotefile.append(CPLGetBasename(pszPath)); + remotefile.append(CPLGetBasenameSafe(pszPath).c_str()); remotefile.append("."); remotefile.append(extension); bSuccess = diff --git a/ogr/ogrsf_frmts/selafin/ogrselafinlayer.cpp b/ogr/ogrsf_frmts/selafin/ogrselafinlayer.cpp index 9405099772fc..41e221f89444 100644 --- a/ogr/ogrsf_frmts/selafin/ogrselafinlayer.cpp +++ b/ogr/ogrsf_frmts/selafin/ogrselafinlayer.cpp @@ -51,7 +51,8 @@ OGRSelafinLayer::OGRSelafinLayer(GDALDataset *poDS, const char *pszLayerNameP, SelafinTypeDef eTypeP) : m_poDS(poDS), eType(eTypeP), bUpdate(CPL_TO_BOOL(bUpdateP)), nStepNumber(nStepNumberP), poHeader(poHeaderP), - poFeatureDefn(new OGRFeatureDefn(CPLGetBasename(pszLayerNameP))), + poFeatureDefn( + new OGRFeatureDefn(CPLGetBasenameSafe(pszLayerNameP).c_str())), poSpatialRef(nullptr), nCurrentId(-1) { #ifdef DEBUG_VERBOSE diff --git a/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp b/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp index 4c71b63edee1..1a2ca1d1f91b 100644 --- a/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp +++ b/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp @@ -284,8 +284,8 @@ bool OGRShapeDataSource::Open(GDALOpenInfo *poOpenInfo, bool bTestOpen, !EQUAL(pszCandidate + strlen(pszCandidate) - 4, ".shp")) continue; - char *pszFilename = - CPLStrdup(CPLFormFilename(pszNewName, pszCandidate, nullptr)); + char *pszFilename = CPLStrdup( + CPLFormFilenameSafe(pszNewName, pszCandidate, nullptr).c_str()); osLayerNameSet.insert(osLayerName); #ifdef IMMEDIATE_OPENING @@ -344,8 +344,8 @@ bool OGRShapeDataSource::Open(GDALOpenInfo *poOpenInfo, bool bTestOpen, if (bFoundTAB) continue; - char *pszFilename = - CPLStrdup(CPLFormFilename(pszNewName, pszCandidate, nullptr)); + char *pszFilename = CPLStrdup( + CPLFormFilenameSafe(pszNewName, pszCandidate, nullptr).c_str()); osLayerNameSet.insert(osLayerName); @@ -539,7 +539,7 @@ void OGRShapeDataSource::AddLayer(OGRShapeLayer *poLayer) static CPLString LaunderLayerName(const char *pszLayerName) { - std::string osRet(CPLLaunderForFilename(pszLayerName, nullptr)); + std::string osRet(CPLLaunderForFilenameSafe(pszLayerName, nullptr)); if (osRet != pszLayerName) { CPLError(CE_Warning, CPLE_AppDefined, @@ -786,11 +786,12 @@ OGRShapeDataSource::ICreateLayer(const char *pszLayerName, if (bSingleFileDataSource && nLayers == 0) { - char *pszPath = CPLStrdup(CPLGetPath(GetDescription())); - char *pszFBasename = CPLStrdup(CPLGetBasename(GetDescription())); + char *pszPath = CPLStrdup(CPLGetPathSafe(GetDescription()).c_str()); + char *pszFBasename = + CPLStrdup(CPLGetBasenameSafe(GetDescription()).c_str()); - pszFilenameWithoutExt = - CPLStrdup(CPLFormFilename(pszPath, pszFBasename, nullptr)); + pszFilenameWithoutExt = CPLStrdup( + CPLFormFilenameSafe(pszPath, pszFBasename, nullptr).c_str()); CPLFree(pszFBasename); CPLFree(pszPath); @@ -803,7 +804,7 @@ OGRShapeDataSource::ICreateLayer(const char *pszLayerName, // directory as 'foo.shp' // So technically, we will not be any longer a single file // datasource ... Ahem ahem. - char *pszPath = CPLStrdup(CPLGetPath(GetDescription())); + char *pszPath = CPLStrdup(CPLGetPathSafe(GetDescription()).c_str()); pszFilenameWithoutExt = CPLStrdup(CPLFormFilename( pszPath, LaunderLayerName(pszLayerName).c_str(), nullptr)); CPLFree(pszPath); @@ -827,8 +828,8 @@ OGRShapeDataSource::ICreateLayer(const char *pszLayerName, if (nShapeType != SHPT_NULL) { - char *pszFilename = - CPLStrdup(CPLFormFilename(nullptr, pszFilenameWithoutExt, "shp")); + char *pszFilename = CPLStrdup( + CPLFormFilenameSafe(nullptr, pszFilenameWithoutExt, "shp").c_str()); hSHP = SHPCreateLL(pszFilename, nShapeType, const_cast(VSI_SHP_GetHook(l_b2GBLimit))); @@ -853,8 +854,8 @@ OGRShapeDataSource::ICreateLayer(const char *pszLayerName, /* -------------------------------------------------------------------- */ /* Create a DBF file. */ /* -------------------------------------------------------------------- */ - char *pszFilename = - CPLStrdup(CPLFormFilename(nullptr, pszFilenameWithoutExt, "dbf")); + char *pszFilename = CPLStrdup( + CPLFormFilenameSafe(nullptr, pszFilenameWithoutExt, "dbf").c_str()); DBFHandle hDBF = DBFCreateLL(pszFilename, (pszLDID != nullptr) ? pszLDID : "LDID/87", @@ -901,8 +902,8 @@ OGRShapeDataSource::ICreateLayer(const char *pszLayerName, // OGRShapeLayer constructor expects a filename with an extension (that // could be random actually), otherwise this is going to cause problems with // layer names that have a dot (not speaking about the one before the shp) - pszFilename = - CPLStrdup(CPLFormFilename(nullptr, pszFilenameWithoutExt, "shp")); + pszFilename = CPLStrdup( + CPLFormFilenameSafe(nullptr, pszFilenameWithoutExt, "shp").c_str()); OGRShapeLayer *poLayer = new OGRShapeLayer(this, pszFilename, hSHP, hDBF, poSRSClone, @@ -1624,11 +1625,13 @@ bool OGRShapeDataSource::RecompressIfNeeded( [&oMapLayerOrder](const CPLString &a, const CPLString &b) { int iA = INT_MAX; - auto oIterA = oMapLayerOrder.find(CPLGetBasename(a)); + auto oIterA = + oMapLayerOrder.find(CPLGetBasenameSafe(a).c_str()); if (oIterA != oMapLayerOrder.end()) iA = oIterA->second; int iB = INT_MAX; - auto oIterB = oMapLayerOrder.find(CPLGetBasename(b)); + auto oIterB = + oMapLayerOrder.find(CPLGetBasenameSafe(b).c_str()); if (oIterB != oMapLayerOrder.end()) iB = oIterB->second; if (iA < iB) diff --git a/ogr/ogrsf_frmts/shape/ogrshapedriver.cpp b/ogr/ogrsf_frmts/shape/ogrshapedriver.cpp index ef8a1b08ea61..d0e318f5dd83 100644 --- a/ogr/ogrsf_frmts/shape/ogrshapedriver.cpp +++ b/ogr/ogrsf_frmts/shape/ogrshapedriver.cpp @@ -48,7 +48,7 @@ static int OGRShapeDriverIdentify(GDALOpenInfo *poOpenInfo) { return FALSE; } - const std::string osExt(CPLGetExtension(poOpenInfo->pszFilename)); + const std::string osExt(CPLGetExtensionSafe(poOpenInfo->pszFilename)); if (EQUAL(osExt.c_str(), "SHP") || EQUAL(osExt.c_str(), "SHX")) { return poOpenInfo->nHeaderBytes >= 4 && @@ -118,7 +118,7 @@ static GDALDataset *OGRShapeDriverOpen(GDALOpenInfo *poOpenInfo) } #endif - CPLString osExt(CPLGetExtension(poOpenInfo->pszFilename)); + CPLString osExt(CPLGetExtensionSafe(poOpenInfo->pszFilename)); if (!STARTS_WITH(poOpenInfo->pszFilename, "/vsizip/") && (EQUAL(osExt, "shz") || (EQUAL(osExt, "zip") && @@ -163,7 +163,7 @@ static GDALDataset *OGRShapeDriverCreate(const char *pszName, int /* nBands */, char ** /* papszOptions */) { bool bSingleNewFile = false; - CPLString osExt(CPLGetExtension(pszName)); + CPLString osExt(CPLGetExtensionSafe(pszName)); /* -------------------------------------------------------------------- */ /* Is the target a valid existing directory? */ @@ -253,7 +253,7 @@ static CPLErr OGRShapeDriverDelete(const char *pszDataSource) return CE_Failure; } - CPLString osExt(CPLGetExtension(pszDataSource)); + CPLString osExt(CPLGetExtensionSafe(pszDataSource)); if (VSI_ISREG(sStatBuf.st_mode) && (EQUAL(osExt, "shz") || (EQUAL(osExt, "zip") && diff --git a/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp b/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp index 6681dd599618..60c1e1f8f1eb 100644 --- a/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp +++ b/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp @@ -2355,7 +2355,8 @@ OGRErr OGRShapeLayer::AlterGeomFieldDefn( { if (poFieldDefn->GetPrjFilename().empty()) { - poFieldDefn->SetPrjFilename(CPLResetExtension(pszFullName, "prj")); + poFieldDefn->SetPrjFilename( + CPLResetExtensionSafe(pszFullName, "prj").c_str()); } const auto poNewSRSRef = poNewGeomFieldDefn->GetSpatialRef(); @@ -2716,7 +2717,8 @@ OGRErr OGRShapeLayer::CreateSpatialIndex(int nMaxDepth) /* -------------------------------------------------------------------- */ /* Dump tree to .qix file. */ /* -------------------------------------------------------------------- */ - char *pszQIXFilename = CPLStrdup(CPLResetExtension(pszFullName, "qix")); + char *pszQIXFilename = + CPLStrdup(CPLResetExtensionSafe(pszFullName, "qix").c_str()); CPLDebug("SHAPE", "Creating index file %s", pszQIXFilename); @@ -2835,8 +2837,8 @@ OGRErr OGRShapeLayer::Repack() /* -------------------------------------------------------------------- */ /* Find existing filenames with exact case (see #3293). */ /* -------------------------------------------------------------------- */ - const CPLString osDirname(CPLGetPath(pszFullName)); - const CPLString osBasename(CPLGetBasename(pszFullName)); + const CPLString osDirname(CPLGetPathSafe(pszFullName)); + const CPLString osBasename(CPLGetBasenameSafe(pszFullName)); CPLString osDBFName; CPLString osSHPName; @@ -3763,8 +3765,9 @@ OGRErr OGRShapeLayer::Rename(const char *pszNewName) const std::string osDirname = CPLGetPath(pszFullName); for (int i = 0; i < oFileList.size(); ++i) { - const std::string osRenamedFile = CPLFormFilename( - osDirname.c_str(), pszNewName, CPLGetExtension(oFileList[i])); + const std::string osRenamedFile = + CPLFormFilename(osDirname.c_str(), pszNewName, + CPLGetExtensionSafe(oFileList[i]).c_str()); VSIStatBufL sStat; if (VSIStatL(osRenamedFile.c_str(), &sStat) == 0) { @@ -3778,8 +3781,9 @@ OGRErr OGRShapeLayer::Rename(const char *pszNewName) for (int i = 0; i < oFileList.size(); ++i) { - const std::string osRenamedFile = CPLFormFilename( - osDirname.c_str(), pszNewName, CPLGetExtension(oFileList[i])); + const std::string osRenamedFile = + CPLFormFilename(osDirname.c_str(), pszNewName, + CPLGetExtensionSafe(oFileList[i]).c_str()); if (VSIRename(oFileList[i], osRenamedFile.c_str()) != 0) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s", @@ -3798,8 +3802,9 @@ OGRErr OGRShapeLayer::Rename(const char *pszNewName) CPLGetExtension(poGeomFieldDefn->GetPrjFilename().c_str()))); } - char *pszNewFullName = CPLStrdup(CPLFormFilename( - osDirname.c_str(), pszNewName, CPLGetExtension(pszFullName))); + char *pszNewFullName = + CPLStrdup(CPLFormFilename(osDirname.c_str(), pszNewName, + CPLGetExtensionSafe(pszFullName).c_str())); CPLFree(pszFullName); pszFullName = pszNewFullName; diff --git a/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp b/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp index b133594200d2..e82938b24e25 100644 --- a/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp +++ b/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp @@ -1913,8 +1913,8 @@ bool OGRSQLiteDataSource::Create(const char *pszNameIn, char **papszOptions) if (bUseTempFile) { m_osFinalFilename = pszNameIn; - m_pszFilename = - CPLStrdup(CPLGenerateTempFilename(CPLGetFilename(pszNameIn))); + m_pszFilename = CPLStrdup( + CPLGenerateTempFilenameSafe(CPLGetFilename(pszNameIn)).c_str()); CPLDebug("SQLITE", "Creating temporary file %s", m_pszFilename); } else diff --git a/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp b/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp index 1ee09e64de5d..6e4086e79686 100644 --- a/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp +++ b/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp @@ -45,7 +45,7 @@ static int OGRSQLiteDriverIdentify(GDALOpenInfo *poOpenInfo) return TRUE; } - CPLString osExt(CPLGetExtension(poOpenInfo->pszFilename)); + CPLString osExt(CPLGetExtensionSafe(poOpenInfo->pszFilename)); if (EQUAL(osExt, "gpkg") && GDALGetDriverByName("GPKG") != nullptr) { return FALSE; diff --git a/ogr/ogrsf_frmts/sqlite/rasterlite2.cpp b/ogr/ogrsf_frmts/sqlite/rasterlite2.cpp index 019c5a15af24..ce87ce98780c 100644 --- a/ogr/ogrsf_frmts/sqlite/rasterlite2.cpp +++ b/ogr/ogrsf_frmts/sqlite/rasterlite2.cpp @@ -2109,8 +2109,8 @@ GDALDataset *OGRSQLiteDriverCreateCopy(const char *pszName, double dfYMax = adfGeoTransform[3]; double dfYMin = dfYMax + adfGeoTransform[5] * poSrcDS->GetRasterYSize(); - CPLString osSectionName( - CSLFetchNameValueDef(papszOptions, "SECTION", CPLGetBasename(pszName))); + CPLString osSectionName(CSLFetchNameValueDef( + papszOptions, "SECTION", CPLGetBasenameSafe(pszName).c_str())); const bool bPyramidize = CPLFetchBool(papszOptions, "PYRAMIDIZE", false); RasterLite2CallbackData cbk_data; cbk_data.poSrcDS = poSrcDS; diff --git a/ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp b/ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp index f68546c8c948..2404dbd5ab52 100644 --- a/ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp +++ b/ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp @@ -339,7 +339,7 @@ int OGRTigerDataSource::Open(const char *pszFilename, int bTestOpen, return FALSE; } - pszPath = CPLStrdup(CPLGetPath(pszFilename)); + pszPath = CPLStrdup(CPLGetPathSafe(pszFilename).c_str()); strncpy(szModule, CPLGetFilename(pszFilename), sizeof(szModule) - 1); /* Make sure the buffer is 0 terminated */ diff --git a/ogr/ogrsf_frmts/vdv/ogrvdvdatasource.cpp b/ogr/ogrsf_frmts/vdv/ogrvdvdatasource.cpp index ba9653a3a027..d4d9aab17a7d 100644 --- a/ogr/ogrsf_frmts/vdv/ogrvdvdatasource.cpp +++ b/ogr/ogrsf_frmts/vdv/ogrvdvdatasource.cpp @@ -190,8 +190,8 @@ void OGRIDFDataSource::Parse() } else { - osTmpFilename = - CPLGenerateTempFilename(CPLGetBasename(m_osFilename)); + osTmpFilename = CPLGenerateTempFilename( + CPLGetBasenameSafe(m_osFilename).c_str()); osTmpFilename += ".gpkg"; } VSIUnlink(osTmpFilename); @@ -1185,7 +1185,7 @@ GDALDataset *OGRVDVDataSource::Open(GDALOpenInfo *poOpenInfo) if (EQUAL(*papszIter, ".") || EQUAL(*papszIter, "..")) continue; nFiles++; - const std::string osExtension(CPLGetExtension(*papszIter)); + const std::string osExtension(CPLGetExtensionSafe(*papszIter)); int nCount = ++oMapOtherExtensions[osExtension]; if (osMajorityExtension == "" || nCount > oMapOtherExtensions[osMajorityExtension]) @@ -1225,7 +1225,8 @@ GDALDataset *OGRVDVDataSource::Open(GDALOpenInfo *poOpenInfo) for (char **papszIter = papszFiles; papszIter && *papszIter; ++papszIter) { - if (!EQUAL(CPLGetExtension(*papszIter), osMajorityExtension)) + if (!EQUAL(CPLGetExtensionSafe(*papszIter).c_str(), + osMajorityExtension)) continue; VSILFILE *fp = VSIFOpenL( CPLFormFilename(poOpenInfo->pszFilename, *papszIter, nullptr), @@ -1235,8 +1236,9 @@ GDALDataset *OGRVDVDataSource::Open(GDALOpenInfo *poOpenInfo) poDS->m_papoLayers = static_cast( CPLRealloc(poDS->m_papoLayers, sizeof(OGRLayer *) * (poDS->m_nLayerCount + 1))); - poDS->m_papoLayers[poDS->m_nLayerCount] = new OGRVDVLayer( - poDS, CPLGetBasename(*papszIter), fp, true, false, 0); + poDS->m_papoLayers[poDS->m_nLayerCount] = + new OGRVDVLayer(poDS, CPLGetBasenameSafe(*papszIter).c_str(), + fp, true, false, 0); poDS->m_nLayerCount++; } CSLDestroy(papszFiles); diff --git a/ogr/ogrsf_frmts/wasp/ogrwaspdatasource.cpp b/ogr/ogrsf_frmts/wasp/ogrwaspdatasource.cpp index d9bce9c9111f..06a11a4bf7ba 100644 --- a/ogr/ogrsf_frmts/wasp/ogrwaspdatasource.cpp +++ b/ogr/ogrsf_frmts/wasp/ogrwaspdatasource.cpp @@ -102,7 +102,8 @@ OGRErr OGRWAsPDataSource::Load(bool bSilent) CPLReadLineL(hFile); CPLReadLineL(hFile); - oLayer.reset(new OGRWAsPLayer(this, CPLGetBasename(sFilename.c_str()), + oLayer.reset(new OGRWAsPLayer(this, + CPLGetBasenameSafe(sFilename.c_str()).c_str(), hFile, poSpatialRef)); if (poSpatialRef) poSpatialRef->Release(); @@ -299,8 +300,8 @@ OGRWAsPDataSource::ICreateLayer(const char *pszName, poSRSClone->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); } oLayer.reset(new OGRWAsPLayer( - this, CPLGetBasename(pszName), hFile, poSRSClone, sFirstField, - sSecondField, sGeomField, bMerge, pdfTolerance.release(), + this, CPLGetBasenameSafe(pszName).c_str(), hFile, poSRSClone, + sFirstField, sSecondField, sGeomField, bMerge, pdfTolerance.release(), pdfAdjacentPointTolerance.release(), pdfPointToCircleRadius.release())); if (poSRSClone) poSRSClone->Release();