From 0b81ed3344d0059038093f09b2648b8aa3cd1abe Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sun, 12 Jan 2025 17:50:55 +0100 Subject: [PATCH] Automated use of RFC105 safe functions Done with following script: ```bash funcs=(CPLGetPath CPLGetDirname CPLGetBasename CPLGetExtension CPLFormFilename CPLFormCIFilename CPLResetExtension CPLProjectRelativeFilename CPLCleanTrailingSlash CPLGenerateTempFilename CPLExpandTilde CPLLaunderForFilename) for func in "${funcs[@]}"; do find frmts ogr -name "*.cpp" -exec sed -i -E "s/(CPLString\s+[a-zA-Z_][a-zA-Z0-9_]*\s*\ = )${func}\((.*)\)/\1${func}Safe(\2)/g" {} \; find frmts ogr -name "*.cpp" -exec sed -i -E "s/(std::string\s+[a-zA-Z_][a-zA-Z0-9_]*\s*\ = )${func}\((.*)\)/\1${func}Safe(\2)/g" {} \; done ``` --- frmts/adrg/adrgdataset.cpp | 2 +- frmts/adrg/srpdataset.cpp | 11 ++--- frmts/aigrid/aigdataset.cpp | 2 +- frmts/derived/deriveddataset.cpp | 4 +- frmts/dimap/dimapdataset.cpp | 18 +++++---- frmts/ers/ersdataset.cpp | 4 +- frmts/gif/biggifdataset.cpp | 2 +- frmts/hfa/hfadataset.cpp | 8 ++-- frmts/hfa/hfaopen.cpp | 8 ++-- frmts/ilwis/ilwiscoordinatesystem.cpp | 4 +- frmts/ilwis/ilwisdataset.cpp | 4 +- .../kmlsuperoverlaydataset.cpp | 2 +- frmts/map/mapdataset.cpp | 2 +- frmts/nitf/nitfdataset.cpp | 2 +- frmts/pcidsk/pcidskdataset2.cpp | 2 +- frmts/pdf/pdfdataset.cpp | 2 +- frmts/pds/isis2dataset.cpp | 8 ++-- frmts/pds/isis3dataset.cpp | 4 +- frmts/pds/pds4vector.cpp | 4 +- frmts/pds/pdsdataset.cpp | 10 ++--- frmts/raw/ehdrdataset.cpp | 40 ++++++++++--------- frmts/raw/eirdataset.cpp | 6 +-- frmts/raw/envidataset.cpp | 2 +- frmts/raw/genbindataset.cpp | 10 ++--- frmts/raw/iscedataset.cpp | 2 +- frmts/raw/ndfdataset.cpp | 2 +- frmts/raw/pauxdataset.cpp | 2 +- frmts/raw/roipacdataset.cpp | 2 +- frmts/rcm/rcmdataset.cpp | 2 +- frmts/saga/sagadataset.cpp | 18 +++++---- frmts/sigdem/sigdemdataset.cpp | 2 +- frmts/til/tildataset.cpp | 4 +- frmts/wcs/wcsutils.cpp | 12 +++--- frmts/zarr/zarr_v2_array.cpp | 2 +- frmts/zarr/zarr_v3_array.cpp | 2 +- frmts/zarr/zarrdrivercore.cpp | 3 +- ogr/ogr_geocoding.cpp | 4 +- ogr/ograpispy.cpp | 3 +- ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp | 2 +- ogr/ogrsf_frmts/cad/gdalcaddataset.cpp | 2 +- ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp | 6 +-- ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp | 2 +- ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp | 2 +- ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp | 2 +- ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp | 4 +- ogr/ogrsf_frmts/ngw/ogrngwdriver.cpp | 2 +- .../openfilegdb/filegdbtable_write.cpp | 3 +- .../ogropenfilegdbdatasource_write.cpp | 9 +++-- .../openfilegdb/ogropenfilegdblayer_write.cpp | 16 ++++---- ogr/ogrsf_frmts/pds/ogrpdsdatasource.cpp | 6 +-- .../selafin/ogrselafindatasource.cpp | 2 +- ogr/ogrsf_frmts/shape/ogrshapelayer.cpp | 4 +- ogr/ogrsf_frmts/vrt/ogrvrtdatasource.cpp | 2 +- 53 files changed, 151 insertions(+), 134 deletions(-) diff --git a/frmts/adrg/adrgdataset.cpp b/frmts/adrg/adrgdataset.cpp index cab88e22a9fe..c2c5cd5e75f1 100644 --- a/frmts/adrg/adrgdataset.cpp +++ b/frmts/adrg/adrgdataset.cpp @@ -1745,7 +1745,7 @@ GDALDataset *ADRGDataset::Create(const char *pszFilename, int nXSize, return nullptr; } - CPLString osImgFilename = CPLResetExtension(pszFilename, "IMG"); + CPLString osImgFilename = CPLResetExtensionSafe(pszFilename, "IMG"); VSILFILE *fdIMG = VSIFOpenL(osImgFilename.c_str(), "w+b"); if (fdIMG == nullptr) { diff --git a/frmts/adrg/srpdataset.cpp b/frmts/adrg/srpdataset.cpp index 20da6c48ccc7..69ac7a397985 100644 --- a/frmts/adrg/srpdataset.cpp +++ b/frmts/adrg/srpdataset.cpp @@ -579,8 +579,9 @@ bool SRPDataset::GetFromRecord(const char *pszFileName, DDFRecord *record) /* Open the .IMG file. Try to recover gracefully if the case */ /* of the filename is wrong. */ /* -------------------------------------------------------------------- */ - const CPLString osDirname = CPLGetDirname(pszFileName); - const CPLString osImgName = CPLFormCIFilename(osDirname, osBAD, nullptr); + const CPLString osDirname = CPLGetDirnameSafe(pszFileName); + const CPLString osImgName = + CPLFormCIFilenameSafe(osDirname, osBAD, nullptr); fdIMG = VSIFOpenL(osImgName, "rb"); if (fdIMG == nullptr) @@ -670,7 +671,7 @@ bool SRPDataset::GetFromRecord(const char *pszFileName, DDFRecord *record) /* -------------------------------------------------------------------- */ /* Try to collect a color map from the .QAL file. */ /* -------------------------------------------------------------------- */ - const CPLString osBasename = CPLGetBasename(pszFileName); + const CPLString osBasename = CPLGetBasenameSafe(pszFileName); osQALFileName = CPLFormCIFilename(osDirname, osBasename, "QAL"); DDFModule oQALModule; @@ -1584,7 +1585,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo) // -------------------------------------------------------------------- VSIStatBufL sStatBuf; - CPLString basename = CPLGetBasename(osFileName); + CPLString basename = CPLGetBasenameSafe(osFileName); if (basename.size() != 8) { CPLDebug("SRP", "Invalid basename file"); @@ -1593,7 +1594,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo) nRecordIndex = static_cast(CPLScanLong(basename + 6, 2)); - CPLString path = CPLGetDirname(osFileName); + CPLString path = CPLGetDirnameSafe(osFileName); CPLString basename01 = ResetTo01(basename); osFileName = CPLFormFilename(path, basename01, ".IMG"); diff --git a/frmts/aigrid/aigdataset.cpp b/frmts/aigrid/aigdataset.cpp index dfc1327838b9..b78a5c27e4e8 100644 --- a/frmts/aigrid/aigdataset.cpp +++ b/frmts/aigrid/aigdataset.cpp @@ -653,7 +653,7 @@ GDALDataset *AIGDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ char **papszFiles = VSIReadDir(psInfo->pszCoverName); CPLString osClrFilename; - CPLString osCleanPath = CPLCleanTrailingSlash(psInfo->pszCoverName); + CPLString osCleanPath = CPLCleanTrailingSlashSafe(psInfo->pszCoverName); // first check for any .clr in coverage dir. for (int iFile = 0; papszFiles != nullptr && papszFiles[iFile] != nullptr; diff --git a/frmts/derived/deriveddataset.cpp b/frmts/derived/deriveddataset.cpp index bccbf31501db..920140f2a848 100644 --- a/frmts/derived/deriveddataset.cpp +++ b/frmts/derived/deriveddataset.cpp @@ -172,10 +172,10 @@ GDALDataset *DerivedDataset::Open(GDALOpenInfo *poOpenInfo) VSIStatBufL sStat; if (VSIStatL(odFilename, &sStat) == 0) { - CPLString path = CPLGetPath(odFilename); + CPLString path = CPLGetPathSafe(odFilename); CPLString ovrFileName = "DERIVED_DATASET_" + odDerivedName + "_" + CPLGetFilename(odFilename); - CPLString ovrFilePath = CPLFormFilename(path, ovrFileName, nullptr); + CPLString ovrFilePath = CPLFormFilenameSafe(path, ovrFileName, nullptr); poDS->oOvManager.Initialize(poDS, ovrFilePath); } diff --git a/frmts/dimap/dimapdataset.cpp b/frmts/dimap/dimapdataset.cpp index 04bd3adb3c57..32a2b023fb84 100644 --- a/frmts/dimap/dimapdataset.cpp +++ b/frmts/dimap/dimapdataset.cpp @@ -647,7 +647,8 @@ GDALDataset *DIMAPDataset::Open(GDALOpenInfo *poOpenInfo) } else { - CPLString osPath = CPLGetPath(osMDFilename.c_str()); + CPLString osPath = + CPLGetPathSafe(osMDFilename.c_str()); osDIMAPFilename = CPLFormFilename(osPath, pszHref, nullptr); } @@ -659,7 +660,8 @@ GDALDataset *DIMAPDataset::Open(GDALOpenInfo *poOpenInfo) if (strlen(pszDataFileHref) > 0) { - CPLString osPath = CPLGetPath(osMDFilename.c_str()); + CPLString osPath = + CPLGetPathSafe(osMDFilename.c_str()); osImageDSFilename = CPLFormFilename( osPath, pszDataFileHref, nullptr); } @@ -709,7 +711,8 @@ GDALDataset *DIMAPDataset::Open(GDALOpenInfo *poOpenInfo) if (strlen(pszHref) > 0) // STRIP product found. { - CPLString osPath = CPLGetPath(osDIMAPFilename.c_str()); + CPLString osPath = + CPLGetPathSafe(osDIMAPFilename.c_str()); osSTRIPFilename = CPLFormCIFilename(osPath, pszHref, nullptr); if (VSIStatL(osSTRIPFilename, &sStat) == 0) @@ -740,7 +743,8 @@ GDALDataset *DIMAPDataset::Open(GDALOpenInfo *poOpenInfo) if (strlen(pszHref) > 0) // RPC product found. { - CPLString osPath = CPLGetPath(osDIMAPFilename.c_str()); + CPLString osPath = + CPLGetPathSafe(osDIMAPFilename.c_str()); osRPCFilename = CPLFormCIFilename(osPath, pszHref, nullptr); @@ -803,8 +807,8 @@ int DIMAPDataset::ReadImageInformation() const char *pszHref = CPLGetXMLValue(psDoc, "Data_Access.Data_File.DATA_FILE_PATH.href", ""); - CPLString osPath = CPLGetPath(osMDFilename); - CPLString osImageFilename = CPLFormFilename(osPath, pszHref, nullptr); + CPLString osPath = CPLGetPathSafe(osMDFilename); + CPLString osImageFilename = CPLFormFilenameSafe(osPath, pszHref, nullptr); /* -------------------------------------------------------------------- */ /* Try and open the file. */ @@ -1154,7 +1158,7 @@ int DIMAPDataset::ReadImageInformation2() int nImageDSRow = 1, nImageDSCol = 1; if (psDataFiles) { - const CPLString osPath = CPLGetPath(osDIMAPFilename); + const CPLString osPath = CPLGetPathSafe(osDIMAPFilename); for (int nPart = 0; psDataFiles != nullptr; psDataFiles = psDataFiles->psNext, nPart++) { diff --git a/frmts/ers/ersdataset.cpp b/frmts/ers/ersdataset.cpp index e45e28430218..522623009b4c 100644 --- a/frmts/ers/ersdataset.cpp +++ b/frmts/ers/ersdataset.cpp @@ -988,7 +988,7 @@ GDALDataset *ERSDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Figure out the name of the target file. */ /* -------------------------------------------------------------------- */ - CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); + CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); CPLString osDataFile = poHeader->Find("DataFile", ""); if (osDataFile.length() == 0) // just strip off extension. @@ -997,7 +997,7 @@ GDALDataset *ERSDataset::Open(GDALOpenInfo *poOpenInfo) osDataFile = osDataFile.substr(0, osDataFile.find_last_of('.')); } - CPLString osDataFilePath = CPLFormFilename(osPath, osDataFile, nullptr); + CPLString osDataFilePath = CPLFormFilenameSafe(osPath, osDataFile, nullptr); /* -------------------------------------------------------------------- */ /* DataSetType = Translated files are links to things like ecw */ diff --git a/frmts/gif/biggifdataset.cpp b/frmts/gif/biggifdataset.cpp index 557c1d2deb41..20f2e936cf56 100644 --- a/frmts/gif/biggifdataset.cpp +++ b/frmts/gif/biggifdataset.cpp @@ -220,7 +220,7 @@ CPLErr BIGGIFDataset::ReOpen() /* while closing and then destroying this temporary dataset */ const char *apszOptions[] = {"COMPRESS=LZW", "SPARSE_OK=YES", nullptr}; - CPLString osTempFilename = CPLGenerateTempFilename("biggif"); + CPLString osTempFilename = CPLGenerateTempFilenameSafe("biggif"); osTempFilename += ".tif"; diff --git a/frmts/hfa/hfadataset.cpp b/frmts/hfa/hfadataset.cpp index e0f12c1b4485..286dbc2fef67 100644 --- a/frmts/hfa/hfadataset.cpp +++ b/frmts/hfa/hfadataset.cpp @@ -4894,8 +4894,8 @@ CPLErr HFADataset::Rename(const char *pszNewName, const char *pszOldName) return eErr; // Now try to go into the .img file and update RRDNames[] lists. - CPLString osOldBasename = CPLGetBasename(pszOldName); - CPLString osNewBasename = CPLGetBasename(pszNewName); + CPLString osOldBasename = CPLGetBasenameSafe(pszOldName); + CPLString osNewBasename = CPLGetBasenameSafe(pszNewName); if (osOldBasename != osNewBasename) { @@ -4936,8 +4936,8 @@ CPLErr HFADataset::CopyFiles(const char *pszNewName, const char *pszOldName) return eErr; // Now try to go into the .img file and update RRDNames[] lists. - CPLString osOldBasename = CPLGetBasename(pszOldName); - CPLString osNewBasename = CPLGetBasename(pszNewName); + CPLString osOldBasename = CPLGetBasenameSafe(pszOldName); + CPLString osNewBasename = CPLGetBasenameSafe(pszNewName); if (osOldBasename != osNewBasename) { diff --git a/frmts/hfa/hfaopen.cpp b/frmts/hfa/hfaopen.cpp index a9514f00a059..98b47c42db4b 100644 --- a/frmts/hfa/hfaopen.cpp +++ b/frmts/hfa/hfaopen.cpp @@ -278,7 +278,7 @@ HFAInfo_t *HFACreateDependent(HFAInfo_t *psBase) return psBase->psDependent; // Create desired RRD filename. - const CPLString oBasename = CPLGetBasename(psBase->pszFilename); + const CPLString oBasename = CPLGetBasenameSafe(psBase->pszFilename); const CPLString oRRDFilename = CPLFormFilename(psBase->pszPath, oBasename, "rrd"); @@ -1876,10 +1876,10 @@ HFAHandle HFACreateLL(const char *pszFilename) CPLString osExtension = CPLGetExtensionSafe(pszFilename); if (!EQUAL(osExtension, "rrd") && !EQUAL(osExtension, "aux")) { - CPLString osPath = CPLGetPath(pszFilename); - CPLString osBasename = CPLGetBasename(pszFilename); + CPLString osPath = CPLGetPathSafe(pszFilename); + CPLString osBasename = CPLGetBasenameSafe(pszFilename); VSIStatBufL sStatBuf; - CPLString osSupFile = CPLFormCIFilename(osPath, osBasename, "rrd"); + CPLString osSupFile = CPLFormCIFilenameSafe(osPath, osBasename, "rrd"); if (VSIStatL(osSupFile, &sStatBuf) == 0) VSIUnlink(osSupFile); diff --git a/frmts/ilwis/ilwiscoordinatesystem.cpp b/frmts/ilwis/ilwiscoordinatesystem.cpp index 11b99bb544c0..5c2f799ad574 100644 --- a/frmts/ilwis/ilwiscoordinatesystem.cpp +++ b/frmts/ilwis/ilwiscoordinatesystem.cpp @@ -995,7 +995,7 @@ CPLErr ILWISDataset::WriteProjection() { OGRSpatialReference *poGeogSRS = nullptr; - std::string csFileName = CPLResetExtension(osFileName, "csy"); + std::string csFileName = CPLResetExtensionSafe(osFileName, "csy"); std::string pszBaseName = std::string(CPLGetBasenameSafe(osFileName)); // std::string pszPath = std::string(CPLGetPathSafe( osFileName )); const bool bHaveSRS = !m_oSRS.IsEmpty(); @@ -1013,7 +1013,7 @@ CPLErr ILWISDataset::WriteProjection() poGeogSRS = m_oSRS.CloneGeogCS(); } - std::string grFileName = CPLResetExtension(osFileName, "grf"); + std::string grFileName = CPLResetExtensionSafe(osFileName, "grf"); std::string csy; if (poGeogSRS) { diff --git a/frmts/ilwis/ilwisdataset.cpp b/frmts/ilwis/ilwisdataset.cpp index 3f74ef4b4f75..ba66b810d6a3 100644 --- a/frmts/ilwis/ilwisdataset.cpp +++ b/frmts/ilwis/ilwisdataset.cpp @@ -570,7 +570,7 @@ void ILWISDataset::WriteGeoReference() double dURLat = (adfGeoTransform[3]); double dURLong = (adfGeoTransform[0] + nXSize * adfGeoTransform[1]); - std::string grFileName = CPLResetExtension(osFileName, "grf"); + std::string grFileName = CPLResetExtensionSafe(osFileName, "grf"); WriteElement("Ilwis", "Type", grFileName, "GeoRef"); WriteElement("GeoRef", "lines", grFileName, nYSize); WriteElement("GeoRef", "columns", grFileName, nXSize); @@ -1204,7 +1204,7 @@ GDALDataset *ILWISDataset::CreateCopy(const char *pszFilename, /* -------------------------------------------------------------------- */ // For file name for raw data, and create binary files. - // std::string pszDataFileName = CPLResetExtension(pszODFName.c_str(), + // std::string pszDataFileName = CPLResetExtensionSafe(pszODFName.c_str(), // "mp#" ); fpData = desBand->fpRaw; diff --git a/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp b/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp index b3a68d456ed0..5d8463b1c080 100644 --- a/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp +++ b/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp @@ -2383,7 +2383,7 @@ GDALDataset *KmlSingleDocRasterDataset::Open(const char *pszFilename, return nullptr; std::vector aosDescs; - CPLString osDirname = CPLGetPath(osFilename); + CPLString osDirname = CPLGetPathSafe(osFilename); KmlSingleDocCollectTiles(psRootFolder, aosDescs, osDirname); if (aosDescs.empty()) return nullptr; diff --git a/frmts/map/mapdataset.cpp b/frmts/map/mapdataset.cpp index d74be6aa1a6f..4ab5a4632492 100644 --- a/frmts/map/mapdataset.cpp +++ b/frmts/map/mapdataset.cpp @@ -229,7 +229,7 @@ GDALDataset *MAPDataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ poDS->osImgFilename = papszLines[2]; - const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); + const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); if (CPLIsFilenameRelative(poDS->osImgFilename)) { poDS->osImgFilename = diff --git a/frmts/nitf/nitfdataset.cpp b/frmts/nitf/nitfdataset.cpp index 15adc9e4cc9c..e0b266009af3 100644 --- a/frmts/nitf/nitfdataset.cpp +++ b/frmts/nitf/nitfdataset.cpp @@ -4040,7 +4040,7 @@ char **NITFDataset::AddFile(char **papszFileList, const char *EXTENSION, const char *extension) { VSIStatBufL sStatBuf; - CPLString osTarget = CPLResetExtension(osNITFFilename, EXTENSION); + CPLString osTarget = CPLResetExtensionSafe(osNITFFilename, EXTENSION); if (oOvManager.GetSiblingFiles() != nullptr) { if (CSLFindStringCaseSensitive(oOvManager.GetSiblingFiles(), diff --git a/frmts/pcidsk/pcidskdataset2.cpp b/frmts/pcidsk/pcidskdataset2.cpp index c278bb909314..7b09ee85a2cd 100644 --- a/frmts/pcidsk/pcidskdataset2.cpp +++ b/frmts/pcidsk/pcidskdataset2.cpp @@ -853,7 +853,7 @@ char **PCIDSK2Dataset::GetFileList() { char **papszFileList = GDALPamDataset::GetFileList(); - CPLString osBaseDir = CPLGetPath(GetDescription()); + CPLString osBaseDir = CPLGetPathSafe(GetDescription()); try { diff --git a/frmts/pdf/pdfdataset.cpp b/frmts/pdf/pdfdataset.cpp index cb57e2a96719..9986deb178d7 100644 --- a/frmts/pdf/pdfdataset.cpp +++ b/frmts/pdf/pdfdataset.cpp @@ -2081,7 +2081,7 @@ CPLErr PDFDataset::ReadPixels(int nReqXOff, int nReqYOff, int nReqXSize, osCmd += "\""; } - CPLString osTmpFilenamePrefix = CPLGenerateTempFilename("pdf"); + CPLString osTmpFilenamePrefix = CPLGenerateTempFilenameSafe("pdf"); osTmpFilename = CPLSPrintf("%s-%d.ppm", osTmpFilenamePrefix.c_str(), iPage); osCmd += CPLSPrintf(" \"%s\"", osTmpFilenamePrefix.c_str()); diff --git a/frmts/pds/isis2dataset.cpp b/frmts/pds/isis2dataset.cpp index a428df549c21..d1c2a7719d32 100644 --- a/frmts/pds/isis2dataset.cpp +++ b/frmts/pds/isis2dataset.cpp @@ -232,7 +232,7 @@ GDALDataset *ISIS2Dataset::Open(GDALOpenInfo *poOpenInfo) if (pszQube[0] == '"') { - const CPLString osTPath = CPLGetPath(poOpenInfo->pszFilename); + const CPLString osTPath = CPLGetPathSafe(poOpenInfo->pszFilename); CPLString osFilename = pszQube; poDS->CleanString(osFilename); osTargetFile = CPLFormCIFilename(osTPath, osFilename, nullptr); @@ -240,7 +240,7 @@ GDALDataset *ISIS2Dataset::Open(GDALOpenInfo *poOpenInfo) } else if (pszQube[0] == '(') { - const CPLString osTPath = CPLGetPath(poOpenInfo->pszFilename); + const CPLString osTPath = CPLGetPathSafe(poOpenInfo->pszFilename); CPLString osFilename = poDS->GetKeywordSub("^QUBE", 1, ""); poDS->CleanString(osFilename); osTargetFile = CPLFormCIFilename(osTPath, osFilename, nullptr); @@ -714,8 +714,8 @@ GDALDataset *ISIS2Dataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Check for a .prj file. For isis2 I would like to keep this in */ /* -------------------------------------------------------------------- */ - const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); - const CPLString osName = CPLGetBasename(poOpenInfo->pszFilename); + const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); + const CPLString osName = CPLGetBasenameSafe(poOpenInfo->pszFilename); const char *pszPrjFile = CPLFormCIFilename(osPath, osName, "prj"); VSILFILE *fp = VSIFOpenL(pszPrjFile, "r"); diff --git a/frmts/pds/isis3dataset.cpp b/frmts/pds/isis3dataset.cpp index 3cac34265d4a..58c9e199d655 100644 --- a/frmts/pds/isis3dataset.cpp +++ b/frmts/pds/isis3dataset.cpp @@ -2446,8 +2446,8 @@ GDALDataset *ISIS3Dataset::Open(GDALOpenInfo *poOpenInfo) /* -------------------------------------------------------------------- */ /* Check for a .prj file. For ISIS3 I would like to keep this in */ /* -------------------------------------------------------------------- */ - const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); - const CPLString osName = CPLGetBasename(poOpenInfo->pszFilename); + const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); + const CPLString osName = CPLGetBasenameSafe(poOpenInfo->pszFilename); const char *pszPrjFile = CPLFormCIFilename(osPath, osName, "prj"); VSILFILE *fp = VSIFOpenL(pszPrjFile, "r"); diff --git a/frmts/pds/pds4vector.cpp b/frmts/pds/pds4vector.cpp index 4228b391921e..df10c76a570f 100644 --- a/frmts/pds/pds4vector.cpp +++ b/frmts/pds/pds4vector.cpp @@ -1788,7 +1788,7 @@ PDS4DelimitedTable::~PDS4DelimitedTable() void PDS4DelimitedTable::GenerateVRT() { - CPLString osVRTFilename = CPLResetExtension(m_osFilename, "vrt"); + CPLString osVRTFilename = CPLResetExtensionSafe(m_osFilename, "vrt"); if (m_bCreation) { // In creation mode, generate the VRT, unless explicitly disabled by @@ -2466,7 +2466,7 @@ void PDS4DelimitedTable::RefreshFileAreaObservational(CPLXMLNode *psFAO) char **PDS4DelimitedTable::GetFileList() const { auto papszFileList = PDS4TableBaseLayer::GetFileList(); - CPLString osVRTFilename = CPLResetExtension(m_osFilename, "vrt"); + CPLString osVRTFilename = CPLResetExtensionSafe(m_osFilename, "vrt"); VSIStatBufL sStat; if (VSIStatL(osVRTFilename, &sStat) == 0) { diff --git a/frmts/pds/pdsdataset.cpp b/frmts/pds/pdsdataset.cpp index a0d8f65bf3dd..ce95bb4a33f9 100644 --- a/frmts/pds/pdsdataset.cpp +++ b/frmts/pds/pdsdataset.cpp @@ -679,8 +679,8 @@ void PDSDataset::ParseSRS() /* Check for a .prj and world file to override the georeferencing. */ /* ==================================================================== */ { - const CPLString osPath = CPLGetPath(pszFilename); - const CPLString osName = CPLGetBasename(pszFilename); + const CPLString osPath = CPLGetPathSafe(pszFilename); + const CPLString osName = CPLGetBasenameSafe(pszFilename); const char *pszPrjFile = CPLFormCIFilename(osPath, osName, "prj"); VSILFILE *fp = VSIFOpenL(pszPrjFile, "r"); @@ -846,7 +846,7 @@ int PDSDataset::ParseImage(const CPLString &osPrefix, } else { - CPLString osTPath = CPLGetPath(GetDescription()); + CPLString osTPath = CPLGetPathSafe(GetDescription()); m_osImageFilename = CPLFormCIFilename(osTPath, osFilename, nullptr); osExternalCube = m_osImageFilename; } @@ -1330,7 +1330,7 @@ int PDSDataset::ParseCompressedImage() const CPLString osFileName = CleanString(GetKeyword("COMPRESSED_FILE.FILE_NAME", "")); - const CPLString osPath = CPLGetPath(GetDescription()); + const CPLString osPath = CPLGetPathSafe(GetDescription()); const CPLString osFullFileName = CPLFormFilename(osPath, osFileName, nullptr); @@ -1425,7 +1425,7 @@ GDALDataset *PDSDataset::Open(GDALOpenInfo *poOpenInfo) if (EQUAL(osEncodingType, "ZIP") && !osCompressedFilename.empty() && !osUncompressedFilename.empty()) { - const CPLString osPath = CPLGetPath(poDS->GetDescription()); + const CPLString osPath = CPLGetPathSafe(poDS->GetDescription()); osCompressedFilename = CPLFormFilename(osPath, osCompressedFilename, nullptr); osUncompressedFilename = diff --git a/frmts/raw/ehdrdataset.cpp b/frmts/raw/ehdrdataset.cpp index 2fbb1703b44a..5f599856b2cf 100644 --- a/frmts/raw/ehdrdataset.cpp +++ b/frmts/raw/ehdrdataset.cpp @@ -476,7 +476,7 @@ void EHdrDataset::ResetKeyValue(const char *pszKey, const char *pszValue) void EHdrDataset::RewriteCLR(GDALRasterBand *poBand) const { - CPLString osCLRFilename = CPLResetExtension(GetDescription(), "clr"); + CPLString osCLRFilename = CPLResetExtensionSafe(GetDescription(), "clr"); GDALColorTable *poTable = poBand->GetColorTable(); GDALRasterAttributeTable *poRAT = poBand->GetDefaultRAT(); if (poTable || poRAT) @@ -568,7 +568,8 @@ CPLErr EHdrDataset::SetSpatialRef(const OGRSpatialReference *poSRS) if (pszESRI_SRS) { // Write to .prj file. - CPLString osPrjFilename = CPLResetExtension(GetDescription(), "prj"); + CPLString osPrjFilename = + CPLResetExtensionSafe(GetDescription(), "prj"); VSILFILE *fp = VSIFOpenL(osPrjFilename.c_str(), "wt"); if (fp != nullptr) { @@ -657,8 +658,8 @@ CPLErr EHdrDataset::SetGeoTransform(double *padfGeoTransform) CPLErr EHdrDataset::RewriteHDR() { - const CPLString osPath = CPLGetPath(GetDescription()); - const CPLString osName = CPLGetBasename(GetDescription()); + const CPLString osPath = CPLGetPathSafe(GetDescription()); + const CPLString osName = CPLGetBasenameSafe(GetDescription()); const CPLString osHDRFilename = CPLFormCIFilename(osPath, osName, osHeaderExt); @@ -697,9 +698,10 @@ CPLErr EHdrDataset::RewriteHDR() CPLErr EHdrDataset::RewriteSTX() const { - const CPLString osPath = CPLGetPath(GetDescription()); - const CPLString osName = CPLGetBasename(GetDescription()); - const CPLString osSTXFilename = CPLFormCIFilename(osPath, osName, "stx"); + const CPLString osPath = CPLGetPathSafe(GetDescription()); + const CPLString osName = CPLGetBasenameSafe(GetDescription()); + const CPLString osSTXFilename = + CPLFormCIFilenameSafe(osPath, osName, "stx"); VSILFILE *fp = VSIFOpenL(osSTXFilename, "wt"); if (fp == nullptr) @@ -739,9 +741,10 @@ CPLErr EHdrDataset::RewriteSTX() const CPLErr EHdrDataset::ReadSTX() const { - const CPLString osPath = CPLGetPath(GetDescription()); - const CPLString osName = CPLGetBasename(GetDescription()); - const CPLString osSTXFilename = CPLFormCIFilename(osPath, osName, "stx"); + const CPLString osPath = CPLGetPathSafe(GetDescription()); + const CPLString osName = CPLGetBasenameSafe(GetDescription()); + const CPLString osSTXFilename = + CPLFormCIFilenameSafe(osPath, osName, "stx"); VSILFILE *fp = VSIFOpenL(osSTXFilename, "rt"); if (fp == nullptr) @@ -822,9 +825,10 @@ CPLErr EHdrDataset::ReadSTX() const CPLString EHdrDataset::GetImageRepFilename(const char *pszFilename) { - const CPLString osPath = CPLGetPath(pszFilename); - const CPLString osName = CPLGetBasename(pszFilename); - const CPLString osREPFilename = CPLFormCIFilename(osPath, osName, "rep"); + const CPLString osPath = CPLGetPathSafe(pszFilename); + const CPLString osName = CPLGetBasenameSafe(pszFilename); + const CPLString osREPFilename = + CPLFormCIFilenameSafe(osPath, osName, "rep"); VSIStatBufL sStatBuf; if (VSIStatExL(osREPFilename.c_str(), &sStatBuf, VSI_STAT_EXISTS_FLAG) == 0) @@ -877,14 +881,14 @@ CPLString EHdrDataset::GetImageRepFilename(const char *pszFilename) char **EHdrDataset::GetFileList() { - const CPLString osPath = CPLGetPath(GetDescription()); - const CPLString osName = CPLGetBasename(GetDescription()); + const CPLString osPath = CPLGetPathSafe(GetDescription()); + const CPLString osName = CPLGetBasenameSafe(GetDescription()); // Main data file, etc. char **papszFileList = GDALPamDataset::GetFileList(); // Header file. - CPLString osFilename = CPLFormCIFilename(osPath, osName, osHeaderExt); + CPLString osFilename = CPLFormCIFilenameSafe(osPath, osName, osHeaderExt); papszFileList = CSLAddString(papszFileList, osFilename); // Statistics file @@ -928,8 +932,8 @@ GDALDataset *EHdrDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck) return nullptr; // Tear apart the filename to form a .HDR filename. - const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); - const CPLString osName = CPLGetBasename(poOpenInfo->pszFilename); + const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); + const CPLString osName = CPLGetBasenameSafe(poOpenInfo->pszFilename); const char *pszHeaderExt = "hdr"; if (poOpenInfo->IsExtensionEqualToCI("SRC") && osName.size() == 7 && diff --git a/frmts/raw/eirdataset.cpp b/frmts/raw/eirdataset.cpp index 4184644049a9..466827286cbb 100644 --- a/frmts/raw/eirdataset.cpp +++ b/frmts/raw/eirdataset.cpp @@ -280,9 +280,9 @@ GDALDataset *EIRDataset::Open(GDALOpenInfo *poOpenInfo) CPLStringList aosHDR; // default raster file: same name with no extension - const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); - const CPLString osName = CPLGetBasename(poOpenInfo->pszFilename); - CPLString osRasterFilename = CPLFormCIFilename(osPath, osName, ""); + const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); + const CPLString osName = CPLGetBasenameSafe(poOpenInfo->pszFilename); + CPLString osRasterFilename = CPLFormCIFilenameSafe(osPath, osName, ""); // parse the header file const char *pszLine = nullptr; diff --git a/frmts/raw/envidataset.cpp b/frmts/raw/envidataset.cpp index 44bc8181764b..d150400c1b5a 100644 --- a/frmts/raw/envidataset.cpp +++ b/frmts/raw/envidataset.cpp @@ -2034,7 +2034,7 @@ ENVIDataset *ENVIDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck) else { // Now we need to tear apart the filename to form a .HDR filename. - CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); + CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); CPLString osName = CPLGetFilename(poOpenInfo->pszFilename); // First try hdr as an extra extension diff --git a/frmts/raw/genbindataset.cpp b/frmts/raw/genbindataset.cpp index 41130fe555e8..12a1a4fd608c 100644 --- a/frmts/raw/genbindataset.cpp +++ b/frmts/raw/genbindataset.cpp @@ -266,14 +266,14 @@ CPLErr GenBinDataset::GetGeoTransform(double *padfTransform) char **GenBinDataset::GetFileList() { - const CPLString osPath = CPLGetPath(GetDescription()); - const CPLString osName = CPLGetBasename(GetDescription()); + const CPLString osPath = CPLGetPathSafe(GetDescription()); + const CPLString osName = CPLGetBasenameSafe(GetDescription()); // Main data file, etc. char **papszFileList = GDALPamDataset::GetFileList(); // Header file. - const CPLString osFilename = CPLFormCIFilename(osPath, osName, "hdr"); + const CPLString osFilename = CPLFormCIFilenameSafe(osPath, osName, "hdr"); papszFileList = CSLAddString(papszFileList, osFilename); return papszFileList; @@ -401,8 +401,8 @@ GDALDataset *GenBinDataset::Open(GDALOpenInfo *poOpenInfo) /* Now we need to tear apart the filename to form a .HDR */ /* filename. */ /* -------------------------------------------------------------------- */ - const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); - const CPLString osName = CPLGetBasename(poOpenInfo->pszFilename); + const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); + const CPLString osName = CPLGetBasenameSafe(poOpenInfo->pszFilename); CPLString osHDRFilename; char **papszSiblingFiles = poOpenInfo->GetSiblingFiles(); diff --git a/frmts/raw/iscedataset.cpp b/frmts/raw/iscedataset.cpp index 0bd1d2201278..5b7c23710788 100644 --- a/frmts/raw/iscedataset.cpp +++ b/frmts/raw/iscedataset.cpp @@ -117,7 +117,7 @@ static CPLString getXMLFilename(GDALOpenInfo *poOpenInfo) /* We need to tear apart the filename to form a .xml */ /* filename. */ /* ------------------------------------------------------------ */ - const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); + const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); const CPLString osName = CPLGetFilename(poOpenInfo->pszFilename); const int iFile = diff --git a/frmts/raw/ndfdataset.cpp b/frmts/raw/ndfdataset.cpp index ec5559fdbf63..93cfd364998d 100644 --- a/frmts/raw/ndfdataset.cpp +++ b/frmts/raw/ndfdataset.cpp @@ -291,7 +291,7 @@ GDALDataset *NDFDataset::Open(GDALOpenInfo *poOpenInfo) } else { - CPLString osBasePath = CPLGetPath(poOpenInfo->pszFilename); + CPLString osBasePath = CPLGetPathSafe(poOpenInfo->pszFilename); osFilename = CPLFormFilename(osBasePath, osFilename, nullptr); } diff --git a/frmts/raw/pauxdataset.cpp b/frmts/raw/pauxdataset.cpp index e38c59cfdf8f..74c3c0ad82ed 100644 --- a/frmts/raw/pauxdataset.cpp +++ b/frmts/raw/pauxdataset.cpp @@ -621,7 +621,7 @@ GDALDataset *PAuxDataset::Open(GDALOpenInfo *poOpenInfo) /* Now we need to tear apart the filename to form a .aux */ /* filename. */ /* -------------------------------------------------------------------- */ - CPLString osAuxFilename = CPLResetExtension(osTarget, "aux"); + CPLString osAuxFilename = CPLResetExtensionSafe(osTarget, "aux"); /* -------------------------------------------------------------------- */ /* Do we have a .aux file? */ diff --git a/frmts/raw/roipacdataset.cpp b/frmts/raw/roipacdataset.cpp index f9929ce88dc8..5fe374edae03 100644 --- a/frmts/raw/roipacdataset.cpp +++ b/frmts/raw/roipacdataset.cpp @@ -85,7 +85,7 @@ static CPLString getRscFilename(GDALOpenInfo *poOpenInfo) /* We need to tear apart the filename to form a .rsc */ /* filename. */ /* ------------------------------------------------------------ */ - const CPLString osPath = CPLGetPath(poOpenInfo->pszFilename); + const CPLString osPath = CPLGetPathSafe(poOpenInfo->pszFilename); const CPLString osName = CPLGetFilename(poOpenInfo->pszFilename); int iFile = CSLFindString(papszSiblingFiles, diff --git a/frmts/rcm/rcmdataset.cpp b/frmts/rcm/rcmdataset.cpp index 85d426f710ae..c87070e0655d 100644 --- a/frmts/rcm/rcmdataset.cpp +++ b/frmts/rcm/rcmdataset.cpp @@ -1416,7 +1416,7 @@ GDALDataset *RCMDataset::Open(GDALOpenInfo *poOpenInfo) // XML std::string osNoiseLevelsValues; - const CPLString osPath = CPLGetPath(osMDFilename); + const CPLString osPath = CPLGetPathSafe(osMDFilename); /* Get a list of all polarizations */ CPLXMLNode *psSourceAttrs = diff --git a/frmts/saga/sagadataset.cpp b/frmts/saga/sagadataset.cpp index 3ff20e9fa402..af73c24e94e8 100644 --- a/frmts/saga/sagadataset.cpp +++ b/frmts/saga/sagadataset.cpp @@ -305,9 +305,10 @@ SAGADataset::~SAGADataset() if (headerDirty) { SAGARasterBand *poGRB = static_cast(GetRasterBand(1)); - const CPLString osPath = CPLGetPath(GetDescription()); - const CPLString osName = CPLGetBasename(GetDescription()); - const CPLString osFilename = CPLFormCIFilename(osPath, osName, ".sgrd"); + const CPLString osPath = CPLGetPathSafe(GetDescription()); + const CPLString osName = CPLGetBasenameSafe(GetDescription()); + const CPLString osFilename = + CPLFormCIFilenameSafe(osPath, osName, ".sgrd"); WriteHeader(osFilename, poGRB->GetRasterDataType(), poGRB->nRasterXSize, poGRB->nRasterYSize, poGRB->m_Xmin, poGRB->m_Ymin, poGRB->m_Cellsize, poGRB->m_NoData, 1.0, false); @@ -323,8 +324,8 @@ SAGADataset::~SAGADataset() char **SAGADataset::GetFileList() { - const CPLString osPath = CPLGetPath(GetDescription()); - const CPLString osName = CPLGetBasename(GetDescription()); + const CPLString osPath = CPLGetPathSafe(GetDescription()); + const CPLString osName = CPLGetBasenameSafe(GetDescription()); // Main data file, etc. char **papszFileList = GDALPamDataset::GetFileList(); @@ -332,7 +333,7 @@ char **SAGADataset::GetFileList() if (!EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "sg-grd-z")) { // Header file. - CPLString osFilename = CPLFormCIFilename(osPath, osName, ".sgrd"); + CPLString osFilename = CPLFormCIFilenameSafe(osPath, osName, ".sgrd"); papszFileList = CSLAddString(papszFileList, osFilename); // projections file. @@ -383,7 +384,8 @@ CPLErr SAGADataset::SetSpatialRef(const OGRSpatialReference *poSRS) /* -------------------------------------------------------------------- */ /* Write to .prj file. */ /* -------------------------------------------------------------------- */ - const CPLString osPrjFilename = CPLResetExtension(GetDescription(), "prj"); + const CPLString osPrjFilename = + CPLResetExtensionSafe(GetDescription(), "prj"); VSILFILE *l_fp = VSIFOpenL(osPrjFilename.c_str(), "wt"); if (l_fp != nullptr) { @@ -933,7 +935,7 @@ GDALDataset *SAGADataset::Create(const char *pszFilename, int nXSize, void *abyNoData = &dfNoDataForAlignment; GDALCopyWords(&dfNoDataVal, GDT_Float64, 0, abyNoData, eType, 0, 1); - const CPLString osHdrFilename = CPLResetExtension(pszFilename, "sgrd"); + const CPLString osHdrFilename = CPLResetExtensionSafe(pszFilename, "sgrd"); CPLErr eErr = WriteHeader(osHdrFilename, eType, nXSize, nYSize, 0.0, 0.0, 1.0, dfNoDataVal, 1.0, false); diff --git a/frmts/sigdem/sigdemdataset.cpp b/frmts/sigdem/sigdemdataset.cpp index 17d45cdff3d3..01f41c199d27 100644 --- a/frmts/sigdem/sigdemdataset.cpp +++ b/frmts/sigdem/sigdemdataset.cpp @@ -262,7 +262,7 @@ GDALDataset *SIGDEMDataset::CreateCopy(const char *pszFilename, { if (!EQUAL(pszProjection, "")) { - CPLString osPrjFilename = CPLResetExtension(pszFilename, "prj"); + CPLString osPrjFilename = CPLResetExtensionSafe(pszFilename, "prj"); VSILFILE *fpProj = VSIFOpenL(osPrjFilename, "wt"); if (fpProj != nullptr) { diff --git a/frmts/til/tildataset.cpp b/frmts/til/tildataset.cpp index 627e35fa62e1..90fbe8d8a90e 100644 --- a/frmts/til/tildataset.cpp +++ b/frmts/til/tildataset.cpp @@ -204,7 +204,7 @@ GDALDataset *TILDataset::Open(GDALOpenInfo *poOpenInfo) return nullptr; } - CPLString osDirname = CPLGetDirname(poOpenInfo->pszFilename); + CPLString osDirname = CPLGetDirnameSafe(poOpenInfo->pszFilename); // get metadata reader @@ -291,7 +291,7 @@ GDALDataset *TILDataset::Open(GDALOpenInfo *poOpenInfo) if (pszFilename[strlen(pszFilename) - 1] == '"') const_cast(pszFilename)[strlen(pszFilename) - 1] = '\0'; - CPLString osFilename = CPLFormFilename(osDirname, pszFilename, nullptr); + CPLString osFilename = CPLFormFilenameSafe(osDirname, pszFilename, nullptr); GDALDataset *poTemplateDS = GDALDataset::FromHandle(GDALOpen(osFilename, GA_ReadOnly)); if (poTemplateDS == nullptr || poTemplateDS->GetRasterCount() == 0) diff --git a/frmts/wcs/wcsutils.cpp b/frmts/wcs/wcsutils.cpp index 912e7c3ef78c..116ba4f395d4 100644 --- a/frmts/wcs/wcsutils.cpp +++ b/frmts/wcs/wcsutils.cpp @@ -297,7 +297,7 @@ bool MakeDir(const std::string &dirname) VSIStatBufL stat; if (VSIStatL(dirname.c_str(), &stat) != 0) { - std::string parent = CPLGetDirname(dirname.c_str()); + std::string parent = CPLGetDirnameSafe(dirname.c_str()); if (!parent.empty() && parent != ".") { if (!MakeDir(parent)) @@ -432,7 +432,7 @@ bool SetupCache(std::string &cache, bool clear) CSLDestroy(folder); } // make sure the index exists and is writable - std::string db = CPLFormFilename(cache.c_str(), "db", nullptr); + std::string db = CPLFormFilenameSafe(cache.c_str(), "db", nullptr); VSILFILE *f = VSIFOpenL(db.c_str(), "r"); if (f) { @@ -465,7 +465,7 @@ static bool CompareStrings(const std::string &a, const std::string &b) std::vector ReadCache(const std::string &cache) { std::vector contents; - std::string db = CPLFormFilename(cache.c_str(), "db", nullptr); + std::string db = CPLFormFilenameSafe(cache.c_str(), "db", nullptr); char **data = CSLLoad(db.c_str()); if (data) { @@ -502,7 +502,7 @@ bool DeleteEntryFromCache(const std::string &cache, const std::string &key, { // Depending on which one of key and value is not "" delete the relevant // entry. - std::string db = CPLFormFilename(cache.c_str(), "db", nullptr); + std::string db = CPLFormFilenameSafe(cache.c_str(), "db", nullptr); char **data = CSLLoad(db.c_str()); // returns NULL in error and for empty files char **data2 = CSLAddNameValue(nullptr, "foo", "bar"); @@ -575,7 +575,7 @@ CPLErr SearchCache(const std::string &cache, const std::string &url, std::string &filename, const std::string &ext, bool &found) { found = false; - std::string db = CPLFormFilename(cache.c_str(), "db", nullptr); + std::string db = CPLFormFilenameSafe(cache.c_str(), "db", nullptr); VSILFILE *f = VSIFOpenL(db.c_str(), "r"); if (!f) { @@ -624,7 +624,7 @@ CPLErr AddEntryToCache(const std::string &cache, const std::string &url, // todo: lock the cache // assuming the url is not in the cache const std::string store = filename; - const std::string db = CPLFormFilename(cache.c_str(), "db", nullptr); + const std::string db = CPLFormFilenameSafe(cache.c_str(), "db", nullptr); VSILFILE *f = VSIFOpenL(db.c_str(), "a"); if (!f) { diff --git a/frmts/zarr/zarr_v2_array.cpp b/frmts/zarr/zarr_v2_array.cpp index 92aedc834ec6..399890c27aa7 100644 --- a/frmts/zarr/zarr_v2_array.cpp +++ b/frmts/zarr/zarr_v2_array.cpp @@ -872,7 +872,7 @@ bool ZarrV2Array::FlushDirtyTile() const if (m_osDimSeparator == "/") { - std::string osDir = CPLGetDirname(osFilename.c_str()); + std::string osDir = CPLGetDirnameSafe(osFilename.c_str()); VSIStatBufL sStat; if (VSIStatL(osDir.c_str(), &sStat) != 0) { diff --git a/frmts/zarr/zarr_v3_array.cpp b/frmts/zarr/zarr_v3_array.cpp index e5689e5d54b7..3c2f1525f0f0 100644 --- a/frmts/zarr/zarr_v3_array.cpp +++ b/frmts/zarr/zarr_v3_array.cpp @@ -768,7 +768,7 @@ bool ZarrV3Array::FlushDirtyTile() const if (m_osDimSeparator == "/") { - std::string osDir = CPLGetDirname(osFilename.c_str()); + std::string osDir = CPLGetDirnameSafe(osFilename.c_str()); VSIStatBufL sStat; if (VSIStatL(osDir.c_str(), &sStat) != 0) { diff --git a/frmts/zarr/zarrdrivercore.cpp b/frmts/zarr/zarrdrivercore.cpp index 26edfd935ca5..5a3a2f68d01f 100644 --- a/frmts/zarr/zarrdrivercore.cpp +++ b/frmts/zarr/zarrdrivercore.cpp @@ -19,7 +19,8 @@ static bool CheckExistenceOfOneZarrFile(const char *pszFilename) { - CPLString osMDFilename = CPLFormFilename(pszFilename, ".zarray", nullptr); + CPLString osMDFilename = + CPLFormFilenameSafe(pszFilename, ".zarray", nullptr); VSIStatBufL sStat; if (VSIStatL(osMDFilename, &sStat) == 0) diff --git a/ogr/ogr_geocoding.cpp b/ogr/ogr_geocoding.cpp index 0f99efb033c6..5b6aaafd363f 100644 --- a/ogr/ogr_geocoding.cpp +++ b/ogr/ogr_geocoding.cpp @@ -240,7 +240,7 @@ OGRGeocodingSessionH OGRGeocodeCreateSession(char **papszOptions) const char *pszCacheFilename = OGRGeocodeGetParameter( papszOptions, "CACHE_FILE", DEFAULT_CACHE_SQLITE); - CPLString osExt = CPLGetExtension(pszCacheFilename); + CPLString osExt = CPLGetExtensionSafe(pszCacheFilename); if (!(STARTS_WITH_CI(pszCacheFilename, "PG:") || EQUAL(osExt, "csv") || EQUAL(osExt, "sqlite"))) { @@ -394,7 +394,7 @@ static OGRLayer *OGRGeocodeGetCacheLayer(OGRGeocodingSessionH hSession, int *pnIdxBlob) { GDALDataset *poDS = hSession->poDS; - CPLString osExt = CPLGetExtension(hSession->pszCacheFilename); + CPLString osExt = CPLGetExtensionSafe(hSession->pszCacheFilename); if (poDS == nullptr) { diff --git a/ogr/ograpispy.cpp b/ogr/ograpispy.cpp index 316efa3ac7c2..766f6fc50713 100644 --- a/ogr/ograpispy.cpp +++ b/ogr/ograpispy.cpp @@ -530,7 +530,8 @@ void OGRAPISpyOpen(const char *pszName, int bUpdate, int iSnapshot, { CPLString osBaseDir = CPLFormFilename( osSnapshotPath, CPLSPrintf("snapshot_%d", iSnapshot), nullptr); - CPLString osWorkingDir = CPLFormFilename(osBaseDir, "working", nullptr); + CPLString osWorkingDir = + CPLFormFilenameSafe(osBaseDir, "working", nullptr); osName = CPLFormFilename(osWorkingDir, CPLGetFilename(pszName), nullptr); pszName = osName.c_str(); diff --git a/ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp b/ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp index 64b0b5d602a5..df969d129501 100644 --- a/ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp +++ b/ogr/ogrsf_frmts/arrow/ogrfeatherdriver.cpp @@ -216,7 +216,7 @@ static GDALDataset *OGRFeatherDriverOpen(GDALOpenInfo *poOpenInfo) const bool bSeekable = !STARTS_WITH_CI(poOpenInfo->pszFilename, "ARROW_IPC_STREAM:") && strcmp(poOpenInfo->pszFilename, "/vsistdin/") != 0; - std::string osLayername = CPLGetBasename(poOpenInfo->pszFilename); + std::string osLayername = CPLGetBasenameSafe(poOpenInfo->pszFilename); if (osLayername.empty()) osLayername = "layer"; auto poLayer = std::make_unique( diff --git a/ogr/ogrsf_frmts/cad/gdalcaddataset.cpp b/ogr/ogrsf_frmts/cad/gdalcaddataset.cpp index 8fb4b351830c..7bc8f2e63125 100644 --- a/ogr/ogrsf_frmts/cad/gdalcaddataset.cpp +++ b/ogr/ogrsf_frmts/cad/gdalcaddataset.cpp @@ -226,7 +226,7 @@ int GDALCADDataset::Open(GDALOpenInfo *poOpenInfo, CADFileIO *pFileIO, { // TODO: Add support clipping region in neatline CPLString osImgFilename = pImage->getFilePath(); - CPLString osImgPath = CPLGetPath(osImgFilename); + CPLString osImgPath = CPLGetPathSafe(osImgFilename); if (osImgPath.empty()) { osImgFilename = diff --git a/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp b/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp index 4c26f86d57a7..01044611478f 100644 --- a/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp +++ b/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp @@ -476,7 +476,7 @@ OGRLayer *OGRCSVDataSource::GetLayer(int iLayer) CPLString OGRCSVDataSource::GetRealExtension(CPLString osFilename) { - const CPLString osExt = CPLGetExtension(osFilename); + const CPLString osExt = CPLGetExtensionSafe(osFilename); if (STARTS_WITH(osFilename, "/vsigzip/") && EQUAL(osExt, "gz")) { if (osFilename.size() > 7 && @@ -868,8 +868,8 @@ bool OGRCSVDataSource::OpenTable(const char *pszFilename, strstr(pszFilename, "/vsizip/") == nullptr) fp = VSICreateBufferedReaderHandle(fp); - CPLString osLayerName = CPLGetBasename(pszFilename); - CPLString osExt = CPLGetExtension(pszFilename); + CPLString osLayerName = CPLGetBasenameSafe(pszFilename); + CPLString osExt = CPLGetExtensionSafe(pszFilename); if (STARTS_WITH(pszFilename, "/vsigzip/") && EQUAL(osExt, "gz")) { if (strlen(pszFilename) > 7 && diff --git a/ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp b/ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp index 4cee91b39e67..fa42e93d2494 100644 --- a/ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp +++ b/ogr/ogrsf_frmts/filegdb/FGdbLayer.cpp @@ -198,7 +198,7 @@ int FGdbLayer::EditIndexesForFIDHack(const char *pszRadixTablename) { // Fix FIDs in .gdbtablx, .spx and .atx's - CPLString osGDBTablX = CPLResetExtension(pszRadixTablename, "gdbtablx"); + CPLString osGDBTablX = CPLResetExtensionSafe(pszRadixTablename, "gdbtablx"); CPLString osNewGDBTablX = CPLResetExtension(pszRadixTablename, "gdbtablx.new"); diff --git a/ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp b/ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp index bf9abaf108de..8a1478c1b7a4 100644 --- a/ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp +++ b/ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp @@ -39,7 +39,7 @@ bool GMLASResourceCache::RecursivelyCreateDirectoryIfNeeded( return true; } - std::string osParent = CPLGetDirname(osDirname.c_str()); + std::string osParent = CPLGetDirnameSafe(osDirname.c_str()); if (!osParent.empty() && osParent != ".") { if (!RecursivelyCreateDirectoryIfNeeded(osParent.c_str())) diff --git a/ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp b/ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp index abb0cad27086..36fbd41fdde9 100644 --- a/ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp +++ b/ogr/ogrsf_frmts/gmt/ogrgmtdatasource.cpp @@ -113,7 +113,7 @@ OGRGmtDataSource::ICreateLayer(const char *pszLayerName, /* layer name with the name from the gmt. */ /* -------------------------------------------------------------------- */ - CPLString osPath = CPLGetPath(GetDescription()); + CPLString osPath = CPLGetPathSafe(GetDescription()); CPLString osFilename(GetDescription()); const char *pszFlags = "wb+"; diff --git a/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp b/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp index 245274328fbe..8bf8681e16ab 100644 --- a/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp +++ b/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp @@ -2969,8 +2969,8 @@ GDALDataset *OGRMVTDataset::Open(GDALOpenInfo *poOpenInfo) return nullptr; } - CPLString osY = CPLGetBasename(osFilename); - CPLString osX = CPLGetBasename(CPLGetPathSafe(osFilename).c_str()); + CPLString osY = CPLGetBasenameSafe(osFilename); + CPLString osX = CPLGetBasenameSafe(CPLGetPathSafe(osFilename).c_str()); CPLString osZ = CPLGetBasename(CPLGetPathSafe(CPLGetPath(osFilename)).c_str()); size_t nPos = osY.find('.'); diff --git a/ogr/ogrsf_frmts/ngw/ogrngwdriver.cpp b/ogr/ogrsf_frmts/ngw/ogrngwdriver.cpp index 29815da4a84a..57d240141afb 100644 --- a/ogr/ogrsf_frmts/ngw/ogrngwdriver.cpp +++ b/ogr/ogrsf_frmts/ngw/ogrngwdriver.cpp @@ -277,7 +277,7 @@ static GDALDataset *OGRNGWDriverCreateCopy(const char *pszFilename, // Compress to minimize network transfer. const char *apszOptions[] = {"COMPRESS=LZW", "NUM_THREADS=ALL_CPUS", nullptr}; - std::string osTempFilename = CPLGenerateTempFilename("ngw_tmp"); + std::string osTempFilename = CPLGenerateTempFilenameSafe("ngw_tmp"); osTempFilename += ".tif"; GDALDataset *poTmpDS = poDriver->CreateCopy( osTempFilename.c_str(), poSrcDS, bStrict, diff --git a/ogr/ogrsf_frmts/openfilegdb/filegdbtable_write.cpp b/ogr/ogrsf_frmts/openfilegdb/filegdbtable_write.cpp index eaef715cdce9..74d6d9b1b9b9 100644 --- a/ogr/ogrsf_frmts/openfilegdb/filegdbtable_write.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/filegdbtable_write.cpp @@ -85,7 +85,8 @@ bool FileGDBTable::Create(const char *pszFilename, int nTablxOffsetSize, return false; } - const std::string osTableXName = CPLResetExtension(pszFilename, "gdbtablx"); + const std::string osTableXName = + CPLResetExtensionSafe(pszFilename, "gdbtablx"); m_fpTableX = VSIFOpenL(osTableXName.c_str(), "wb+"); if (m_fpTableX == nullptr) { diff --git a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp index 1da21a6cb4cb..4f7ca0a01556 100644 --- a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp @@ -1495,7 +1495,8 @@ OGRErr OGROpenFileGDBDataSource::DeleteLayer(int iLayer) } } - const std::string osDirname = CPLGetPath(poLayer->GetFilename().c_str()); + const std::string osDirname = + CPLGetPathSafe(poLayer->GetFilename().c_str()); const std::string osFilenameBase = CPLGetBasename(poLayer->GetFilename().c_str()); @@ -2398,7 +2399,7 @@ bool OGROpenFileGDBDataSource::BackupSystemTablesForTransaction() for (char **papszIter = papszFiles; papszIter != nullptr && *papszIter != nullptr; ++papszIter) { - const std::string osBasename = CPLGetBasename(*papszIter); + const std::string osBasename = CPLGetBasenameSafe(*papszIter); if (osBasename.size() == strlen("a00000001") && osBasename.compare(0, 8, "a0000000") == 0 && osBasename[8] >= '1' && osBasename[8] <= '8') @@ -2474,7 +2475,7 @@ OGRErr OGROpenFileGDBDataSource::RollbackTransaction() for (char **papszIter = papszFiles; papszIter != nullptr && *papszIter != nullptr; ++papszIter) { - const std::string osBasename = CPLGetBasename(*papszIter); + const std::string osBasename = CPLGetBasenameSafe(*papszIter); if (osBasename.size() == strlen("a00000001") && osBasename.compare(0, 8, "a0000000") == 0 && osBasename[8] >= '1' && osBasename[8] <= '8') @@ -2510,7 +2511,7 @@ OGRErr OGROpenFileGDBDataSource::RollbackTransaction() for (char **papszIter = papszFiles; papszIter != nullptr && *papszIter != nullptr; ++papszIter) { - const std::string osBasename = CPLGetBasename(*papszIter); + const std::string osBasename = CPLGetBasenameSafe(*papszIter); if (osBasename == osThisBasename) { std::string osDestFilename = diff --git a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdblayer_write.cpp b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdblayer_write.cpp index e760d726f233..62570468acf2 100644 --- a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdblayer_write.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdblayer_write.cpp @@ -2944,13 +2944,14 @@ bool OGROpenFileGDBLayer::BeginEmulatedTransaction() bool bRet = true; - const std::string osThisDirname = CPLGetPath(m_osGDBFilename.c_str()); - const std::string osThisBasename = CPLGetBasename(m_osGDBFilename.c_str()); + const std::string osThisDirname = CPLGetPathSafe(m_osGDBFilename.c_str()); + const std::string osThisBasename = + CPLGetBasenameSafe(m_osGDBFilename.c_str()); char **papszFiles = VSIReadDir(osThisDirname.c_str()); for (char **papszIter = papszFiles; papszIter != nullptr && *papszIter != nullptr; ++papszIter) { - const std::string osBasename = CPLGetBasename(*papszIter); + const std::string osBasename = CPLGetBasenameSafe(*papszIter); if (osBasename == osThisBasename) { std::string osDestFilename = CPLFormFilename( @@ -3019,8 +3020,9 @@ bool OGROpenFileGDBLayer::RollbackEmulatedTransaction() bool bRet = true; - const std::string osThisDirname = CPLGetPath(m_osGDBFilename.c_str()); - const std::string osThisBasename = CPLGetBasename(m_osGDBFilename.c_str()); + const std::string osThisDirname = CPLGetPathSafe(m_osGDBFilename.c_str()); + const std::string osThisBasename = + CPLGetBasenameSafe(m_osGDBFilename.c_str()); // Delete files in working directory that match our basename { @@ -3028,7 +3030,7 @@ bool OGROpenFileGDBLayer::RollbackEmulatedTransaction() for (char **papszIter = papszFiles; papszIter != nullptr && *papszIter != nullptr; ++papszIter) { - const std::string osBasename = CPLGetBasename(*papszIter); + const std::string osBasename = CPLGetBasenameSafe(*papszIter); if (osBasename == osThisBasename) { std::string osDestFilename = @@ -3046,7 +3048,7 @@ bool OGROpenFileGDBLayer::RollbackEmulatedTransaction() for (char **papszIter = papszFiles; papszIter != nullptr && *papszIter != nullptr; ++papszIter) { - const std::string osBasename = CPLGetBasename(*papszIter); + const std::string osBasename = CPLGetBasenameSafe(*papszIter); if (osBasename == osThisBasename) { bBackupFound = true; diff --git a/ogr/ogrsf_frmts/pds/ogrpdsdatasource.cpp b/ogr/ogrsf_frmts/pds/ogrpdsdatasource.cpp index 3d7dbf2cd197..0dd2a705cb62 100644 --- a/ogr/ogrsf_frmts/pds/ogrpdsdatasource.cpp +++ b/ogr/ogrsf_frmts/pds/ogrpdsdatasource.cpp @@ -146,7 +146,7 @@ bool OGRPDSDataSource::LoadTable(const char *pszFilename, int nRecordSize, osTableLink.c_str()); return false; } - CPLString osTPath = CPLGetPath(pszFilename); + CPLString osTPath = CPLGetPathSafe(pszFilename); CleanString(osTableFilename); osTableFilename = CPLFormCIFilename(osTPath, osTableFilename, nullptr); } @@ -178,7 +178,7 @@ bool OGRPDSDataSource::LoadTable(const char *pszFilename, int nRecordSize, } else { - CPLString osTPath = CPLGetPath(pszFilename); + CPLString osTPath = CPLGetPathSafe(pszFilename); CleanString(osTableFilename); osTableFilename = CPLFormCIFilename(osTPath, osTableFilename, nullptr); @@ -229,7 +229,7 @@ bool OGRPDSDataSource::LoadTable(const char *pszFilename, int nRecordSize, oKeywords.GetKeyword(MakeAttr(osTableID, "^STRUCTURE"), ""); if (!osTableStructure.empty()) { - CPLString osTPath = CPLGetPath(pszFilename); + CPLString osTPath = CPLGetPathSafe(pszFilename); CleanString(osTableStructure); osTableStructure = CPLFormCIFilename(osTPath, osTableStructure, nullptr); diff --git a/ogr/ogrsf_frmts/selafin/ogrselafindatasource.cpp b/ogr/ogrsf_frmts/selafin/ogrselafindatasource.cpp index 510cffbc5d29..150b32980afa 100644 --- a/ogr/ogrsf_frmts/selafin/ogrselafindatasource.cpp +++ b/ogr/ogrsf_frmts/selafin/ogrselafindatasource.cpp @@ -469,7 +469,7 @@ int OGRSelafinDataSource::OpenTable(const char *pszFilename) } */ // Get layer base name - CPLString osBaseLayerName = CPLGetBasename(pszFilename); + CPLString osBaseLayerName = CPLGetBasenameSafe(pszFilename); // Read header of file to get common information for all layers // poHeader now owns fp diff --git a/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp b/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp index 60c1e1f8f1eb..6d1d68f394cc 100644 --- a/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp +++ b/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp @@ -1876,7 +1876,7 @@ OGRErr OGRShapeLayer::CreateField(const OGRFieldDefn *poFieldDefn, bool bDBFJustCreated = false; if (hDBF == nullptr) { - const CPLString osFilename = CPLResetExtension(pszFullName, "dbf"); + const CPLString osFilename = CPLResetExtensionSafe(pszFullName, "dbf"); hDBF = DBFCreate(osFilename); if (hDBF == nullptr) @@ -3762,7 +3762,7 @@ OGRErr OGRShapeLayer::Rename(const char *pszNewName) CPLStringList oFileList; AddToFileList(oFileList); - const std::string osDirname = CPLGetPath(pszFullName); + const std::string osDirname = CPLGetPathSafe(pszFullName); for (int i = 0; i < oFileList.size(); ++i) { const std::string osRenamedFile = diff --git a/ogr/ogrsf_frmts/vrt/ogrvrtdatasource.cpp b/ogr/ogrsf_frmts/vrt/ogrvrtdatasource.cpp index e96c85ff0115..9caf46103252 100644 --- a/ogr/ogrsf_frmts/vrt/ogrvrtdatasource.cpp +++ b/ogr/ogrsf_frmts/vrt/ogrvrtdatasource.cpp @@ -738,7 +738,7 @@ bool OGRVRTDataSource::Initialize(CPLXMLNode *psTreeIn, const char *pszNewName, // Set name, and capture the directory path so we can use it // for relative datasources. - CPLString osVRTDirectory = CPLGetPath(pszNewName); + CPLString osVRTDirectory = CPLGetPathSafe(pszNewName); // Look for the OGRVRTDataSource node, it might be after an node. CPLXMLNode *psVRTDSXML = CPLGetXMLNode(psTree, "=OGRVRTDataSource");