From 85eb32a8e622da568f0ff1cca2ede74b8e55787f Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 13 Jan 2025 01:19:07 +0100 Subject: [PATCH] frmts/ ogr/ .cpp: replace all remaining uses of CPLGetBasename() with CPLGetBasenameSafe() --- frmts/fits/fitsdataset.cpp | 4 +- frmts/grib/gribdataset.cpp | 2 +- frmts/hfa/hfaopen.cpp | 12 +++--- frmts/jaxapalsar/jaxapalsardataset.cpp | 5 ++- .../kmlsuperoverlaydataset.cpp | 8 ++-- frmts/nitf/nitfdataset.cpp | 23 ++++++------ frmts/pds/pds4dataset.cpp | 12 +++--- frmts/prf/phprfdataset.cpp | 2 +- frmts/rasterlite/rasterlitecreatecopy.cpp | 4 +- frmts/raw/ace2dataset.cpp | 3 +- frmts/raw/iscedataset.cpp | 4 +- frmts/saga/sagadataset.cpp | 14 +++---- frmts/sentinel2/sentinel2dataset.cpp | 12 +++--- frmts/tiledb/tiledbdense.cpp | 27 ++++++-------- frmts/tiledb/tiledbsparse.cpp | 5 ++- frmts/xpm/xpmdataset.cpp | 2 +- frmts/zarr/zarr_sharedresource.cpp | 4 +- frmts/zarr/zarrdriver.cpp | 10 ++--- ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp | 4 +- ogr/ogrsf_frmts/geojson/ogresrijsonreader.cpp | 13 ++++--- ogr/ogrsf_frmts/geojson/ogrgeojsonreader.cpp | 37 ++++++++++++------- .../geojson/ogrgeojsonseqdriver.cpp | 4 +- .../gpkg/ogrgeopackagedatasource.cpp | 12 +++--- ogr/ogrsf_frmts/jsonfg/ogrjsonfgdataset.cpp | 2 +- .../libkml/ogrlibkmldatasource.cpp | 3 +- .../miramon/ogrmiramondatasource.cpp | 18 +++++---- ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp | 13 ++++--- ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp | 5 ++- ogr/ogrsf_frmts/openfilegdb/filegdbindex.cpp | 27 ++++++++------ ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp | 30 ++++++++------- .../openfilegdb/filegdbtable_write.cpp | 12 +++--- .../ogropenfilegdbdatasource_write.cpp | 4 +- .../openfilegdb/ogropenfilegdbdrivercore.cpp | 7 ++-- ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp | 22 +++++------ ogr/ogrsf_frmts/shape/ogrshapelayer.cpp | 6 +-- ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp | 4 +- ogr/ogrsf_frmts/sqlite/rasterlite2.cpp | 12 +++--- ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp | 5 ++- 38 files changed, 210 insertions(+), 183 deletions(-) diff --git a/frmts/fits/fitsdataset.cpp b/frmts/fits/fitsdataset.cpp index 67209ae32d2e..b62a824ab454 100644 --- a/frmts/fits/fitsdataset.cpp +++ b/frmts/fits/fitsdataset.cpp @@ -2543,7 +2543,7 @@ GDALDataset *FITSDataset::Open(GDALOpenInfo *poOpenInfo) osPath.resize(1024); if (CPLGetExecPath(&osPath[0], static_cast(osPath.size()))) { - osPath = CPLGetBasename(osPath.c_str()); + osPath = CPLGetBasenameSafe(osPath.c_str()); } if (osPath == "gdalinfo") { @@ -2578,7 +2578,7 @@ GDALDataset *FITSDataset::Open(GDALOpenInfo *poOpenInfo) osPath.resize(1024); if (CPLGetExecPath(&osPath[0], static_cast(osPath.size()))) { - osPath = CPLGetBasename(osPath.c_str()); + osPath = CPLGetBasenameSafe(osPath.c_str()); } if (osPath == "ogrinfo") { diff --git a/frmts/grib/gribdataset.cpp b/frmts/grib/gribdataset.cpp index f4f3e26c5b66..f99407267ae7 100644 --- a/frmts/grib/gribdataset.cpp +++ b/frmts/grib/gribdataset.cpp @@ -901,7 +901,7 @@ static bool IsGdalinfoInteractive() osPath.resize(1024); if (CPLGetExecPath(&osPath[0], static_cast(osPath.size()))) { - osPath = CPLGetBasename(osPath.c_str()); + osPath = CPLGetBasenameSafe(osPath.c_str()); } return osPath == "gdalinfo"; } diff --git a/frmts/hfa/hfaopen.cpp b/frmts/hfa/hfaopen.cpp index 98b47c42db4b..c88c22b7251c 100644 --- a/frmts/hfa/hfaopen.cpp +++ b/frmts/hfa/hfaopen.cpp @@ -2908,13 +2908,15 @@ const char *HFAGetIGEFilename(HFAHandle hHFA) const CPLString osExtension = CPLGetExtensionSafe(pszRawFilename); const CPLString osBasename = - CPLGetBasename(hHFA->pszFilename); - osFullFilename = - CPLFormFilename(hHFA->pszPath, osBasename, osExtension); + CPLGetBasenameSafe(hHFA->pszFilename); + osFullFilename = CPLFormFilenameSafe( + hHFA->pszPath, osBasename, osExtension); if (VSIStatL(osFullFilename, &sStatBuf) == 0) - hHFA->pszIGEFilename = CPLStrdup( - CPLFormFilename(nullptr, osBasename, osExtension)); + hHFA->pszIGEFilename = + CPLStrdup(CPLFormFilenameSafe(nullptr, osBasename, + osExtension) + .c_str()); else hHFA->pszIGEFilename = CPLStrdup(pszRawFilename); } diff --git a/frmts/jaxapalsar/jaxapalsardataset.cpp b/frmts/jaxapalsar/jaxapalsardataset.cpp index e6be1eee7173..684bf898bd52 100644 --- a/frmts/jaxapalsar/jaxapalsardataset.cpp +++ b/frmts/jaxapalsar/jaxapalsardataset.cpp @@ -514,8 +514,9 @@ int PALSARJaxaDataset::Identify(GDALOpenInfo *poOpenInfo) /* First, check that this is a PALSAR image indeed */ if (!STARTS_WITH_CI((char *)(poOpenInfo->pabyHeader + 60), "AL") || - !STARTS_WITH_CI(CPLGetBasename((char *)(poOpenInfo->pszFilename)) + 4, - "ALPSR")) + !STARTS_WITH_CI( + CPLGetBasenameSafe(poOpenInfo->pszFilename).substr(4).c_str(), + "ALPSR")) { return 0; } diff --git a/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp b/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp index 5d8463b1c080..7d34036f186b 100644 --- a/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp +++ b/frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp @@ -168,15 +168,15 @@ static int GenerateRootKml(const char *filename, const char *kmlfilename, } int minlodpixels = tilesize / 2; - const char *tmpfilename = CPLGetBasename(kmlfilename); - if (pszOverlayName == nullptr) - pszOverlayName = tmpfilename; + const std::string osOverlayName = pszOverlayName + ? std::string(pszOverlayName) + : CPLGetBasenameSafe(kmlfilename); // If we have not written any features yet, output the layer's schema. VSIFPrintfL(fp, "\n"); VSIFPrintfL(fp, "\n"); VSIFPrintfL(fp, "\t\n"); - char *pszEncoded = CPLEscapeString(pszOverlayName, -1, CPLES_XML); + char *pszEncoded = CPLEscapeString(osOverlayName.c_str(), -1, CPLES_XML); VSIFPrintfL(fp, "\t\t%s\n", pszEncoded); CPLFree(pszEncoded); if (pszOverlayDescription == nullptr) diff --git a/frmts/nitf/nitfdataset.cpp b/frmts/nitf/nitfdataset.cpp index e0b266009af3..67177f1cedcf 100644 --- a/frmts/nitf/nitfdataset.cpp +++ b/frmts/nitf/nitfdataset.cpp @@ -1422,23 +1422,24 @@ NITFDataset *NITFDataset::OpenInternal(GDALOpenInfo *poOpenInfo, /* -------------------------------------------------------------------- */ // get _rpc.txt file - const char *pszDirName = CPLGetDirname(pszFilename); - const char *pszBaseName = CPLGetBasename(pszFilename); - const char *pszRPCTXTFilename = - CPLFormFilename(pszDirName, CPLSPrintf("%s_rpc", pszBaseName), "txt"); - if (CPLCheckForFile((char *)pszRPCTXTFilename, - poOpenInfo->GetSiblingFiles())) + const std::string osDirName = CPLGetDirnameSafe(pszFilename); + const std::string osBaseName = CPLGetBasenameSafe(pszFilename); + std::string osRPCTXTFilename = CPLFormFilenameSafe( + osDirName.c_str(), std::string(osBaseName).append("_rpc").c_str(), + "txt"); + if (CPLCheckForFile(osRPCTXTFilename.data(), poOpenInfo->GetSiblingFiles())) { - poDS->m_osRPCTXTFilename = pszRPCTXTFilename; + poDS->m_osRPCTXTFilename = osRPCTXTFilename; } else { - pszRPCTXTFilename = CPLFormFilename( - pszDirName, CPLSPrintf("%s_RPC", pszBaseName), "TXT"); - if (CPLCheckForFile((char *)pszRPCTXTFilename, + osRPCTXTFilename = CPLFormFilenameSafe( + osDirName.c_str(), std::string(osBaseName).append("_RPC").c_str(), + "TXT"); + if (CPLCheckForFile(osRPCTXTFilename.data(), poOpenInfo->GetSiblingFiles())) { - poDS->m_osRPCTXTFilename = pszRPCTXTFilename; + poDS->m_osRPCTXTFilename = osRPCTXTFilename; } } bool bHasLoadedRPCTXT = false; diff --git a/frmts/pds/pds4dataset.cpp b/frmts/pds/pds4dataset.cpp index 829c443c5cc8..3a3c79db6e3e 100644 --- a/frmts/pds/pds4dataset.cpp +++ b/frmts/pds/pds4dataset.cpp @@ -4169,8 +4169,8 @@ OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName, if (bSameDirectory) { osFullFilename = - CPLFormFilename(CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(), - osBasename.c_str(), pszExt); + CPLFormFilenameSafe(CPLGetPathSafe(m_osXMLFilename.c_str()).c_str(), + osBasename.c_str(), pszExt); VSIStatBufL sStat; if (VSIStatL(osFullFilename, &sStat) == 0) { @@ -4183,9 +4183,9 @@ OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName, } else { - CPLString osDirectory = - CPLFormFilename(CPLGetPathSafe(m_osXMLFilename).c_str(), - CPLGetBasename(m_osXMLFilename), nullptr); + CPLString osDirectory = CPLFormFilenameSafe( + CPLGetPathSafe(m_osXMLFilename).c_str(), + CPLGetBasenameSafe(m_osXMLFilename).c_str(), nullptr); VSIStatBufL sStat; if (VSIStatL(osDirectory, &sStat) != 0 && VSIMkdir(osDirectory, 0755) != 0) @@ -4195,7 +4195,7 @@ OGRLayer *PDS4Dataset::ICreateLayer(const char *pszName, return nullptr; } osFullFilename = - CPLFormFilename(osDirectory, osBasename.c_str(), pszExt); + CPLFormFilenameSafe(osDirectory, osBasename.c_str(), pszExt); } if (EQUAL(pszTableType, "DELIMITED")) diff --git a/frmts/prf/phprfdataset.cpp b/frmts/prf/phprfdataset.cpp index bffdd5cbf3f6..1b41f2ce26ed 100644 --- a/frmts/prf/phprfdataset.cpp +++ b/frmts/prf/phprfdataset.cpp @@ -387,7 +387,7 @@ GDALDataset *PhPrfDataset::Open(GDALOpenInfo *poOpenInfo) GDALDataType eResultDatatype = GDT_Unknown; CPLString osPartsBasePath(CPLGetPathSafe(poOpenInfo->pszFilename)); CPLString osPartsPath(osPartsBasePath + "/" + - CPLGetBasename(poOpenInfo->pszFilename)); + CPLGetBasenameSafe(poOpenInfo->pszFilename)); CPLString osPartsExt; double adfGeoTrans[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; bool bGeoTransOk = false; diff --git a/frmts/rasterlite/rasterlitecreatecopy.cpp b/frmts/rasterlite/rasterlitecreatecopy.cpp index 70f424cbc0e9..cb78648b3043 100644 --- a/frmts/rasterlite/rasterlitecreatecopy.cpp +++ b/frmts/rasterlite/rasterlitecreatecopy.cpp @@ -405,7 +405,7 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS, if (nTokens == 0) { osDBName = pszFilenameWithoutPrefix; - osTableName = CPLGetBasename(pszFilenameWithoutPrefix); + osTableName = CPLGetBasenameSafe(pszFilenameWithoutPrefix); } else { @@ -439,7 +439,7 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS, "specified"); return nullptr; } - osTableName = CPLGetBasename(osDBName.c_str()); + osTableName = CPLGetBasenameSafe(osDBName.c_str()); } CPLString osRasterLayer; diff --git a/frmts/raw/ace2dataset.cpp b/frmts/raw/ace2dataset.cpp index 04741d2f1e49..1acc5a01408b 100644 --- a/frmts/raw/ace2dataset.cpp +++ b/frmts/raw/ace2dataset.cpp @@ -208,7 +208,8 @@ GDALDataset *ACE2Dataset::Open(GDALOpenInfo *poOpenInfo) if (!Identify(poOpenInfo)) return nullptr; - const char *pszBasename = CPLGetBasename(poOpenInfo->pszFilename); + const std::string osBasename = CPLGetBasenameSafe(poOpenInfo->pszFilename); + const char *pszBasename = osBasename.c_str(); if (strlen(pszBasename) < 7) return nullptr; diff --git a/frmts/raw/iscedataset.cpp b/frmts/raw/iscedataset.cpp index 5b7c23710788..9c106ba17caf 100644 --- a/frmts/raw/iscedataset.cpp +++ b/frmts/raw/iscedataset.cpp @@ -239,10 +239,10 @@ CPLErr ISCEDataset::FlushCache(bool bAtClosing) CPLAddXMLAttributeAndValue(psTmpNode, "name", "ACCESS_MODE"); CPLCreateXMLElementAndValue(psTmpNode, "value", "read"); - const char *pszFilename = CPLGetBasename(pszXMLFilename); psTmpNode = CPLCreateXMLNode(psDocNode, CXT_Element, "property"); CPLAddXMLAttributeAndValue(psTmpNode, "name", "FILE_NAME"); - CPLCreateXMLElementAndValue(psTmpNode, "value", pszFilename); + CPLCreateXMLElementAndValue(psTmpNode, "value", + CPLGetBasenameSafe(pszXMLFilename).c_str()); /* -------------------------------------------------------------------- */ /* Then, add the ISCE domain metadata. */ diff --git a/frmts/saga/sagadataset.cpp b/frmts/saga/sagadataset.cpp index af73c24e94e8..32a76acf821e 100644 --- a/frmts/saga/sagadataset.cpp +++ b/frmts/saga/sagadataset.cpp @@ -444,17 +444,17 @@ GDALDataset *SAGADataset::Open(GDALOpenInfo *poOpenInfo) CSLDestroy(filesinzip); - osFullname = CPLFormFilename(osPath, file, nullptr); - osName = CPLGetBasename(file); - osHDRFilename = - CPLFormFilename(osPath, CPLGetBasenameSafe(file).c_str(), "sgrd"); + osFullname = CPLFormFilenameSafe(osPath, file, nullptr); + osName = CPLGetBasenameSafe(file); + osHDRFilename = CPLFormFilenameSafe( + osPath, CPLGetBasenameSafe(file).c_str(), "sgrd"); } else { osFullname = poOpenInfo->pszFilename; - osPath = CPLGetPath(poOpenInfo->pszFilename); - osName = CPLGetBasename(poOpenInfo->pszFilename); - osHDRFilename = CPLFormCIFilename( + osPath = CPLGetPathSafe(poOpenInfo->pszFilename); + osName = CPLGetBasenameSafe(poOpenInfo->pszFilename); + osHDRFilename = CPLFormCIFilenameSafe( osPath, CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str(), "sgrd"); } diff --git a/frmts/sentinel2/sentinel2dataset.cpp b/frmts/sentinel2/sentinel2dataset.cpp index 4eb51ecfa578..541260a140d9 100644 --- a/frmts/sentinel2/sentinel2dataset.cpp +++ b/frmts/sentinel2/sentinel2dataset.cpp @@ -1816,9 +1816,9 @@ static void SENTINEL2GetResolutionSetAndMainMDFromGranule( if (atoi(osBandName) < 10) osBandName = "0" + osBandName; - CPLString osTile( - SENTINEL2GetTilename(CPLGetPathSafe(pszFilename).c_str(), - CPLGetBasename(pszFilename), osBandName)); + CPLString osTile(SENTINEL2GetTilename( + CPLGetPathSafe(pszFilename).c_str(), + CPLGetBasenameSafe(pszFilename).c_str(), 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(CPLGetPathSafe(osFilename).c_str(), - CPLGetBasename(osFilename), aosBands[i])); + CPLString osTile(SENTINEL2GetTilename( + CPLGetPathSafe(osFilename).c_str(), + CPLGetBasenameSafe(osFilename).c_str(), aosBands[i])); if (SENTINEL2GetTileInfo(osTile, &nCols, &nRows, &nBits)) { if (nBits <= 16) diff --git a/frmts/tiledb/tiledbdense.cpp b/frmts/tiledb/tiledbdense.cpp index 5b1172af747e..9c8d765c4b38 100644 --- a/frmts/tiledb/tiledbdense.cpp +++ b/frmts/tiledb/tiledbdense.cpp @@ -348,9 +348,8 @@ CPLErr TileDBRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, if (eErr == CE_None) { - CPLString osName = CPLString().Printf( - "%s", CPLGetBasenameSafe(poAttrDS->GetDescription()) - .c_str()); + CPLString osName = + CPLGetBasenameSafe(poAttrDS->GetDescription()); SetBuffer(poQuery.get(), eAttrType, osName, pAttrBlock, nValues); @@ -1353,7 +1352,6 @@ GDALDataset *TileDBRasterDataset::OpenInternal(GDALOpenInfo *poOpenInfo, poDS->nTimestamp = std::strtoull(pszTimestamp, nullptr, 10); CPLString osURI; - CPLString osAux; CPLString osSubdataset; std::string osAttrNameTmp; @@ -1388,8 +1386,8 @@ GDALDataset *TileDBRasterDataset::OpenInternal(GDALOpenInfo *poOpenInfo, osURI = TileDBDataset::VSI_to_tiledb_uri(poOpenInfo->pszFilename); } - const char *pszArrayName = CPLGetBasename(osURI); - osAux.Printf("%s.tdb", pszArrayName); + CPLString osAux = CPLGetBasenameSafe(osURI); + osAux += ".tdb"; // aux file is in array folder poDS->SetPhysicalFilename( @@ -2159,12 +2157,11 @@ TileDBRasterDataset *TileDBRasterDataset::CreateLL(const char *pszFilename, } } - CPLString osAux; - const char *pszArrayName = CPLGetBasename(pszFilename); - osAux.Printf("%s.tdb", pszArrayName); + CPLString osAux = CPLGetBasenameSafe(pszFilename); + osAux += ".tdb"; poDS->SetPhysicalFilename( - CPLFormFilename(pszFilename, osAux.c_str(), nullptr)); + CPLFormFilenameSafe(pszFilename, osAux.c_str(), nullptr).c_str()); // Initialize PAM information. poDS->SetDescription(pszFilename); @@ -2287,13 +2284,13 @@ bool TileDBRasterDataset::DeferredCreate(bool bCreateArray) // be reported as subdatasets on future reads for (const auto &poAttrDS : m_lpoAttributeDS) { - const char *pszAttrName = - CPLGetBasename(poAttrDS->GetDescription()); + const std::string osAttrName = + CPLGetBasenameSafe(poAttrDS->GetDescription()); GDALRasterBand *poAttrBand = poAttrDS->GetRasterBand(1); int bHasNoData = false; const double dfNoData = poAttrBand->GetNoDataValue(&bHasNoData); - CreateAttribute(poAttrBand->GetRasterDataType(), pszAttrName, 1, - CPL_TO_BOOL(bHasNoData), dfNoData); + CreateAttribute(poAttrBand->GetRasterDataType(), osAttrName.c_str(), + 1, CPL_TO_BOOL(bHasNoData), dfNoData); } if (bCreateArray) @@ -2604,7 +2601,7 @@ TileDBRasterDataset *TileDBRasterDataset::Create(const char *pszFilename, { aosImageStruct.SetNameValue( CPLString().Printf("TILEDB_ATTRIBUTE_%i", ++i), - CPLGetBasename(poAttrDS->GetDescription())); + CPLGetBasenameSafe(poAttrDS->GetDescription()).c_str()); } } poDS->SetMetadata(aosImageStruct.List(), "IMAGE_STRUCTURE"); diff --git a/frmts/tiledb/tiledbsparse.cpp b/frmts/tiledb/tiledbsparse.cpp index e4ede677f9e2..2cbaccce388a 100644 --- a/frmts/tiledb/tiledbsparse.cpp +++ b/frmts/tiledb/tiledbsparse.cpp @@ -229,8 +229,9 @@ GDALDataset *OGRTileDBDataset::Open(GDALOpenInfo *poOpenInfo, { auto poLayer = std::make_unique( poDS.get(), osLayerFilename.c_str(), - osLayerName.has_value() ? (*osLayerName).c_str() - : CPLGetBasename(osLayerFilename.c_str()), + osLayerName.has_value() + ? (*osLayerName).c_str() + : CPLGetBasenameSafe(osLayerFilename.c_str()).c_str(), wkbUnknown, nullptr); poLayer->m_bUpdatable = poOpenInfo->eAccess == GA_Update; if (!poLayer->InitFromStorage(poDS->m_ctx.get(), nTimestamp, diff --git a/frmts/xpm/xpmdataset.cpp b/frmts/xpm/xpmdataset.cpp index 55168688f4d0..2623ce75eed1 100644 --- a/frmts/xpm/xpmdataset.cpp +++ b/frmts/xpm/xpmdataset.cpp @@ -332,7 +332,7 @@ static GDALDataset *XPMCreateCopy(const char *pszFilename, GDALDataset *poSrcDS, /* -------------------------------------------------------------------- */ bool bOK = VSIFPrintfL(fpPBM, "/* XPM */\n") >= 0; bOK &= VSIFPrintfL(fpPBM, "static char *%s[] = {\n", - CPLGetBasename(pszFilename)) >= 0; + CPLGetBasenameSafe(pszFilename).c_str()) >= 0; bOK &= VSIFPrintfL(fpPBM, "/* width height num_colors chars_per_pixel */\n") >= 0; diff --git a/frmts/zarr/zarr_sharedresource.cpp b/frmts/zarr/zarr_sharedresource.cpp index 76bed033c08f..615a16626171 100644 --- a/frmts/zarr/zarr_sharedresource.cpp +++ b/frmts/zarr/zarr_sharedresource.cpp @@ -99,7 +99,7 @@ std::shared_ptr ZarrSharedResource::OpenRootGroup() } } const std::string osArrayName( - CPLGetBasename(m_osRootDirectoryName.c_str())); + CPLGetBasenameSafe(m_osRootDirectoryName.c_str())); if (!poRG->LoadArray(osArrayName, osZarrayFilename, oRoot, false, CPLJSONObject())) return nullptr; @@ -165,7 +165,7 @@ std::shared_ptr ZarrSharedResource::OpenRootGroup() if (osNodeType == "array") { const std::string osArrayName( - CPLGetBasename(m_osRootDirectoryName.c_str())); + CPLGetBasenameSafe(m_osRootDirectoryName.c_str())); poRG_V3->SetExplored(); if (!poRG_V3->LoadArray(osArrayName, osZarrJsonFilename, oRoot)) return nullptr; diff --git a/frmts/zarr/zarrdriver.cpp b/frmts/zarr/zarrdriver.cpp index 57f7d361a039..87bff6e1ac93 100644 --- a/frmts/zarr/zarrdriver.cpp +++ b/frmts/zarr/zarrdriver.cpp @@ -1180,8 +1180,8 @@ GDALDataset *ZarrDataset::Create(const char *pszName, int nXSize, int nYSize, nBandsIn) : nullptr); - const char *pszNonNullArrayName = - pszArrayName ? pszArrayName : CPLGetBasename(pszName); + const std::string osNonNullArrayName = + pszArrayName ? std::string(pszArrayName) : CPLGetBasenameSafe(pszName); if (poBandDim) { const std::vector> apoDims( @@ -1192,8 +1192,8 @@ GDALDataset *ZarrDataset::Create(const char *pszName, int nXSize, int nYSize, : std::vector>{ poDS->m_poDimY, poDS->m_poDimX, poBandDim}); poDS->m_poSingleArray = poRG->CreateMDArray( - pszNonNullArrayName, apoDims, GDALExtendedDataType::Create(eType), - papszOptions); + osNonNullArrayName.c_str(), apoDims, + GDALExtendedDataType::Create(eType), papszOptions); if (!poDS->m_poSingleArray) { CleanupCreatedFiles(); @@ -1215,7 +1215,7 @@ GDALDataset *ZarrDataset::Create(const char *pszName, int nXSize, int nYSize, for (int i = 0; i < nBandsIn; i++) { auto poArray = poRG->CreateMDArray( - nBandsIn == 1 ? pszNonNullArrayName + nBandsIn == 1 ? osNonNullArrayName.c_str() : pszArrayName ? CPLSPrintf("%s_band%d", pszArrayName, i + 1) : CPLSPrintf("Band%d", i + 1), apoDims, GDALExtendedDataType::Create(eType), papszOptions); diff --git a/ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp b/ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp index c97bb0c444a8..68085054ba87 100644 --- a/ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp +++ b/ogr/ogrsf_frmts/adbc/ogradbcdataset.cpp @@ -404,9 +404,9 @@ bool OGRADBCDataset::Open(const GDALOpenInfo *poOpenInfo) { if (m_osParquetFilename.empty()) m_osParquetFilename = pszFilename; - osLayerName = CPLGetBasename(m_osParquetFilename.c_str()); + osLayerName = CPLGetBasenameSafe(m_osParquetFilename.c_str()); if (osLayerName == "*") - osLayerName = CPLGetBasename( + osLayerName = CPLGetBasenameSafe( CPLGetDirnameSafe(m_osParquetFilename.c_str()).c_str()); if (!pszSQL) { diff --git a/ogr/ogrsf_frmts/geojson/ogresrijsonreader.cpp b/ogr/ogrsf_frmts/geojson/ogresrijsonreader.cpp index 7b1290589e1c..3c5610047fd5 100644 --- a/ogr/ogrsf_frmts/geojson/ogresrijsonreader.cpp +++ b/ogr/ogrsf_frmts/geojson/ogresrijsonreader.cpp @@ -96,13 +96,13 @@ void OGRESRIJSONReader::ReadLayers(OGRGeoJSONDataSource *poDS, OGRSpatialReference *poSRS = OGRESRIJSONReadSpatialReference(poGJObject_); - const char *pszName = "ESRIJSON"; + std::string osName = "ESRIJSON"; if (eSourceType == eGeoJSONSourceFile) { - pszName = poDS->GetDescription(); - if (STARTS_WITH_CI(pszName, "ESRIJSON:")) - pszName += strlen("ESRIJSON:"); - pszName = CPLGetBasename(pszName); + osName = poDS->GetDescription(); + if (STARTS_WITH_CI(osName.c_str(), "ESRIJSON:")) + osName = osName.substr(strlen("ESRIJSON:")); + osName = CPLGetBasenameSafe(osName.c_str()); } auto eGeomType = OGRESRIJSONGetGeometryType(poGJObject_); @@ -141,7 +141,8 @@ void OGRESRIJSONReader::ReadLayers(OGRGeoJSONDataSource *poDS, } } - poLayer_ = new OGRGeoJSONLayer(pszName, poSRS, eGeomType, poDS, nullptr); + poLayer_ = + new OGRGeoJSONLayer(osName.c_str(), poSRS, eGeomType, poDS, nullptr); if (poSRS != nullptr) poSRS->Release(); diff --git a/ogr/ogrsf_frmts/geojson/ogrgeojsonreader.cpp b/ogr/ogrsf_frmts/geojson/ogrgeojsonreader.cpp index fddf907519f1..41e41b2146a5 100644 --- a/ogr/ogrsf_frmts/geojson/ogrgeojsonreader.cpp +++ b/ogr/ogrsf_frmts/geojson/ogrgeojsonreader.cpp @@ -415,14 +415,15 @@ bool OGRGeoJSONReader::FirstPassReadLayer(OGRGeoJSONDataSource *poDS, VSIFSeekL(fp, 0, SEEK_SET); bFirstSeg_ = true; - const char *pszName = poDS->GetDescription(); - if (STARTS_WITH_CI(pszName, "GeoJSON:")) - pszName += strlen("GeoJSON:"); - pszName = CPLGetBasename(pszName); - pszName = OGRGeoJSONLayer::GetValidLayerName(pszName); - - OGRGeoJSONLayer *poLayer = new OGRGeoJSONLayer( - pszName, nullptr, OGRGeoJSONLayer::DefaultGeometryType, poDS, this); + std::string osName = poDS->GetDescription(); + if (STARTS_WITH_CI(osName.c_str(), "GeoJSON:")) + osName = osName.substr(strlen("GeoJSON:")); + osName = CPLGetBasenameSafe(osName.c_str()); + osName = OGRGeoJSONLayer::GetValidLayerName(osName.c_str()); + + OGRGeoJSONLayer *poLayer = + new OGRGeoJSONLayer(osName.c_str(), nullptr, + OGRGeoJSONLayer::DefaultGeometryType, poDS, this); OGRGeoJSONReaderStreamingParser oParser(*this, poLayer, true, bStoreNativeData_); @@ -911,7 +912,12 @@ void OGRGeoJSONReader::ReadLayer(OGRGeoJSONDataSource *poDS, CPLErrorReset(); // Figure out layer name - if (pszName == nullptr) + std::string osName; + if (pszName) + { + osName = pszName; + } + else { if (GeoJSONObject::eFeatureCollection == objType) { @@ -922,20 +928,25 @@ void OGRGeoJSONReader::ReadLayer(OGRGeoJSONDataSource *poDS, pszName = json_object_get_string(poName); } } - if (pszName == nullptr) + if (pszName) + { + osName = pszName; + } + else { const char *pszDesc = poDS->GetDescription(); if (strchr(pszDesc, '?') == nullptr && strchr(pszDesc, '{') == nullptr) { - pszName = CPLGetBasename(pszDesc); + osName = CPLGetBasenameSafe(pszDesc); } } } - pszName = OGRGeoJSONLayer::GetValidLayerName(pszName); + osName = OGRGeoJSONLayer::GetValidLayerName(osName.c_str()); OGRGeoJSONLayer *poLayer = new OGRGeoJSONLayer( - pszName, nullptr, OGRGeoJSONLayer::DefaultGeometryType, poDS, nullptr); + osName.c_str(), nullptr, OGRGeoJSONLayer::DefaultGeometryType, poDS, + nullptr); OGRSpatialReference *poSRS = OGRGeoJSONReadSpatialReference(poObj); bool bDefaultSRS = false; diff --git a/ogr/ogrsf_frmts/geojson/ogrgeojsonseqdriver.cpp b/ogr/ogrsf_frmts/geojson/ogrgeojsonseqdriver.cpp index a2562b507ef9..3688e95a91db 100644 --- a/ogr/ogrsf_frmts/geojson/ogrgeojsonseqdriver.cpp +++ b/ogr/ogrsf_frmts/geojson/ogrgeojsonseqdriver.cpp @@ -789,13 +789,13 @@ bool OGRGeoJSONSeqDataSource::Open(GDALOpenInfo *poOpenInfo, { if (pszUnprefixedFilename != poOpenInfo->pszFilename) { - osLayerName = CPLGetBasename(pszUnprefixedFilename); + osLayerName = CPLGetBasenameSafe(pszUnprefixedFilename); m_fp = VSIFOpenL(pszUnprefixedFilename, poOpenInfo->eAccess == GA_Update ? "rb+" : "rb"); } else { - osLayerName = CPLGetBasename(poOpenInfo->pszFilename); + osLayerName = CPLGetBasenameSafe(poOpenInfo->pszFilename); std::swap(m_fp, poOpenInfo->fpL); } } diff --git a/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp b/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp index 38bd3cd5f50a..95c09a6295e6 100644 --- a/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp +++ b/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp @@ -5555,9 +5555,9 @@ int GDALGeoPackageDataset::Create(const char *pszFilename, int nXSize, if (nBandsIn != 0) { - const char *pszTableName = CPLGetBasename(m_pszFilename); - m_osRasterTable = - CSLFetchNameValueDef(papszOptions, "RASTER_TABLE", pszTableName); + const std::string osTableName = CPLGetBasenameSafe(m_pszFilename); + m_osRasterTable = CSLFetchNameValueDef(papszOptions, "RASTER_TABLE", + osTableName.c_str()); if (m_osRasterTable.empty()) { CPLError(CE_Failure, CPLE_AppDefined, @@ -6026,9 +6026,9 @@ GDALDataset *GDALGeoPackageDataset::CreateCopy(const char *pszFilename, CSLFetchNameValueDef(papszOptions, "APPEND_SUBDATASET", "NO")) && CSLFetchNameValue(papszOptions, "RASTER_TABLE") == nullptr) { - CPLString osBasename( - CPLGetBasename(GetUnderlyingDataset(poSrcDS)->GetDescription())); - apszUpdatedOptions.SetNameValue("RASTER_TABLE", osBasename); + const std::string osBasename(CPLGetBasenameSafe( + GetUnderlyingDataset(poSrcDS)->GetDescription())); + apszUpdatedOptions.SetNameValue("RASTER_TABLE", osBasename.c_str()); } if (nBands != 1 && nBands != 2 && nBands != 3 && nBands != 4) diff --git a/ogr/ogrsf_frmts/jsonfg/ogrjsonfgdataset.cpp b/ogr/ogrsf_frmts/jsonfg/ogrjsonfgdataset.cpp index b055f091048d..c10829ac0a1c 100644 --- a/ogr/ogrsf_frmts/jsonfg/ogrjsonfgdataset.cpp +++ b/ogr/ogrsf_frmts/jsonfg/ogrjsonfgdataset.cpp @@ -199,7 +199,7 @@ bool OGRJSONFGDataset::Open(GDALOpenInfo *poOpenInfo, return false; } SetDescription(pszUnprefixed); - osDefaultLayerName = CPLGetBasename(pszUnprefixed); + osDefaultLayerName = CPLGetBasenameSafe(pszUnprefixed); eAccess = poOpenInfo->eAccess; // Ingests the first bytes of the file in pszGeoData_ diff --git a/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp b/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp index 318741dc565f..d6aeaefd7422 100644 --- a/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp +++ b/ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp @@ -1289,8 +1289,7 @@ int OGRLIBKMLDataSource::OpenKmz(const char *pszFilename, int bUpdateIn) const std::string osLayerName = poKmlNetworkLink->has_name() ? poKmlNetworkLink->get_name() - : std::string( - CPLGetBasename(oKmlHref.get_path().c_str())); + : CPLGetBasenameSafe(oKmlHref.get_path().c_str()); AddLayer(osLayerName.c_str(), wkbUnknown, nullptr, this, std::move(poKmlLyrRoot), poKmlLyrContainer, diff --git a/ogr/ogrsf_frmts/miramon/ogrmiramondatasource.cpp b/ogr/ogrsf_frmts/miramon/ogrmiramondatasource.cpp index a9e182ea78d7..31011c337432 100644 --- a/ogr/ogrsf_frmts/miramon/ogrmiramondatasource.cpp +++ b/ogr/ogrsf_frmts/miramon/ogrmiramondatasource.cpp @@ -50,14 +50,18 @@ bool OGRMiraMonDataSource::Open(const char *pszFilename, VSILFILE *fp, if (!m_osRootName.empty()) { - const char *pszExtension = CPLGetExtension(m_osRootName.c_str()); - if (!EQUAL(pszExtension, "pol") && !EQUAL(pszExtension, "arc") && - !EQUAL(pszExtension, "pnt")) + const std::string osExtension = + CPLGetExtensionSafe(m_osRootName.c_str()); + if (!EQUAL(osExtension.c_str(), "pol") && + !EQUAL(osExtension.c_str(), "arc") && + !EQUAL(osExtension.c_str(), "pnt")) { CPLStrlcpy(m_MMMap.pszMapName, - CPLFormFilename(m_osRootName.c_str(), - CPLGetBasename(m_osRootName.c_str()), - "mmm"), + CPLFormFilenameSafe( + m_osRootName.c_str(), + CPLGetBasenameSafe(m_osRootName.c_str()).c_str(), + "mmm") + .c_str(), sizeof(m_MMMap.pszMapName)); if (!m_MMMap.nNumberOfLayers) { @@ -79,7 +83,7 @@ bool OGRMiraMonDataSource::Open(const char *pszFilename, VSILFILE *fp, VSIFPrintfL(m_MMMap.fMMMap, "\n"); VSIFPrintfL(m_MMMap.fMMMap, "[DOCUMENT]\n"); VSIFPrintfL(m_MMMap.fMMMap, "Titol= %s(map)\n", - CPLGetBasename(poLayer->GetName())); + CPLGetBasenameSafe(poLayer->GetName()).c_str()); VSIFPrintfL(m_MMMap.fMMMap, "\n"); } } diff --git a/ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp b/ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp index 00f0c076dc34..24523402488b 100644 --- a/ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp +++ b/ogr/ogrsf_frmts/miramon/ogrmiramonlayer.cpp @@ -2996,36 +2996,37 @@ void OGRMiraMonLayer::AddToFileList(CPLStringList &oFileList) szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + A.rel - const char *pszBaseArcName = CPLGetBasename(szCompleteArcFileName); - CPLStrlcpy(szAuxFile, pszBaseArcName, MM_CPL_PATH_BUF_SIZE); + const std::string osBaseArcName = + CPLGetBasenameSafe(szCompleteArcFileName); + CPLStrlcpy(szAuxFile, osBaseArcName.c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? "A.rel" : "A.REL", MM_CPL_PATH_BUF_SIZE); oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( CPLGetDirname(szCompleteArcFileName), szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + A.dbf - CPLStrlcpy(szAuxFile, pszBaseArcName, MM_CPL_PATH_BUF_SIZE); + CPLStrlcpy(szAuxFile, osBaseArcName.c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? "A.dbf" : "A.DBF", MM_CPL_PATH_BUF_SIZE); oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( CPLGetDirname(szCompleteArcFileName), szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + .nod - CPLStrlcpy(szAuxFile, pszBaseArcName, MM_CPL_PATH_BUF_SIZE); + CPLStrlcpy(szAuxFile, osBaseArcName.c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? ".nod" : ".NOD", MM_CPL_PATH_BUF_SIZE); oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( CPLGetDirname(szCompleteArcFileName), szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + N.rel - CPLStrlcpy(szAuxFile, pszBaseArcName, MM_CPL_PATH_BUF_SIZE); + CPLStrlcpy(szAuxFile, osBaseArcName.c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? "N.rel" : "N.REL", MM_CPL_PATH_BUF_SIZE); oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( CPLGetDirname(szCompleteArcFileName), szAuxFile, nullptr))); // FILE_NAME_WITHOUT_EXTENSION.arc --> FILE_NAME_WITHOUT_EXTENSION + N.dbf - CPLStrlcpy(szAuxFile, pszBaseArcName, MM_CPL_PATH_BUF_SIZE); + CPLStrlcpy(szAuxFile, osBaseArcName.c_str(), MM_CPL_PATH_BUF_SIZE); CPLStrlcat(szAuxFile, (pszMMExt[0] == 'p') ? "N.dbf" : "N.DBF", MM_CPL_PATH_BUF_SIZE); oFileList.AddStringDirectly(VSIGetCanonicalFilename(CPLFormFilename( diff --git a/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp b/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp index 8bf8681e16ab..4b2e132a8f1e 100644 --- a/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp +++ b/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp @@ -1668,8 +1668,9 @@ void OGRMVTDirectoryLayer::OpenTileIfNeeded() if (m_bUseReadDir) { while (m_nYIndex < m_aosSubDirContent.Count() && - (CPLGetValueType(CPLGetBasename( - m_aosSubDirContent[m_nYIndex])) != CPL_VALUE_INTEGER || + (CPLGetValueType( + CPLGetBasenameSafe(m_aosSubDirContent[m_nYIndex]) + .c_str()) != CPL_VALUE_INTEGER || !IsBetween(atoi(m_aosSubDirContent[m_nYIndex]), m_nFilterMinY, m_nFilterMaxY))) { diff --git a/ogr/ogrsf_frmts/openfilegdb/filegdbindex.cpp b/ogr/ogrsf_frmts/openfilegdb/filegdbindex.cpp index e96ca20007e7..e5f9dafb4e49 100644 --- a/ogr/ogrsf_frmts/openfilegdb/filegdbindex.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/filegdbindex.cpp @@ -1130,12 +1130,14 @@ int FileGDBIndexIterator::SetConstraint(int nFieldIdx, FileGDBSQLOp op, return FALSE; } - const char *pszAtxName = - CPLFormFilename(CPLGetPath(poParent->GetFilename().c_str()), - CPLGetBasename(poParent->GetFilename().c_str()), - CPLSPrintf("%s.atx", poIndex->GetIndexName().c_str())); - - if (!ReadTrailer(pszAtxName)) + const std::string osAtxName = + CPLFormFilenameSafe( + CPLGetPathSafe(poParent->GetFilename().c_str()).c_str(), + CPLGetBasenameSafe(poParent->GetFilename().c_str()).c_str(), + poIndex->GetIndexName().c_str()) + .append(".atx"); + + if (!ReadTrailer(osAtxName.c_str())) return FALSE; returnErrorIf(m_nValueCountInIdx > static_cast(poParent->GetValidRecordCount())); @@ -2483,11 +2485,11 @@ bool FileGDBSpatialIndexIteratorImpl::Init() { const bool errorRetValue = false; - const char *pszSpxName = - CPLFormFilename(CPLGetPath(poParent->GetFilename().c_str()), - CPLGetBasename(poParent->GetFilename().c_str()), "spx"); + const std::string osSpxName = CPLFormFilenameSafe( + CPLGetPathSafe(poParent->GetFilename().c_str()).c_str(), + CPLGetBasenameSafe(poParent->GetFilename().c_str()).c_str(), "spx"); - if (!ReadTrailer(pszSpxName)) + if (!ReadTrailer(osSpxName.c_str())) return false; returnErrorIf(m_nValueSize != sizeof(uint64_t)); @@ -2511,7 +2513,8 @@ bool FileGDBSpatialIndexIteratorImpl::Init() // The FileGDB driver does not use the .spx file in that situation, // so do we. CPLDebug("OpenFileGDB", - "Cannot use %s as the grid resolution is invalid", pszSpxName); + "Cannot use %s as the grid resolution is invalid", + osSpxName.c_str()); return false; } @@ -2544,7 +2547,7 @@ bool FileGDBSpatialIndexIteratorImpl::Init() CPLError(CE_Warning, CPLE_AppDefined, "Cannot use %s as the index depth(=1) is suspicious " "(it should rather be 2)", - pszSpxName); + osSpxName.c_str()); return false; } } diff --git a/ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp b/ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp index 025264628b7c..be9464396c6d 100644 --- a/ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp @@ -870,14 +870,15 @@ bool FileGDBTable::Open(const char *pszFilename, bool bUpdate, m_nHeaderBufferMaxSize = GetInt32(abyHeader + 8, 0); - CPLString osTableXName; + std::string osTableXName; if (m_bUpdate || (m_nValidRecordCount > 0 && !CPLTestBool(CPLGetConfigOption( "OPENFILEGDB_IGNORE_GDBTABLX", "false")))) { - osTableXName = CPLFormFilename(CPLGetPathSafe(pszFilename).c_str(), - CPLGetBasename(pszFilename), "gdbtablx"); - m_fpTableX = VSIFOpenL(osTableXName, m_bUpdate ? "r+b" : "rb"); + osTableXName = CPLFormFilenameSafe( + CPLGetPathSafe(pszFilename).c_str(), + CPLGetBasenameSafe(pszFilename).c_str(), "gdbtablx"); + m_fpTableX = VSIFOpenL(osTableXName.c_str(), m_bUpdate ? "r+b" : "rb"); if (m_fpTableX == nullptr) { if (m_bUpdate) @@ -2478,14 +2479,15 @@ int FileGDBTable::GetIndexCount() m_bHasReadGDBIndexes = TRUE; - const char *pszIndexesName = - CPLFormFilename(CPLGetPathSafe(m_osFilename.c_str()).c_str(), - CPLGetBasename(m_osFilename.c_str()), "gdbindexes"); - VSILFILE *fpIndexes = VSIFOpenL(pszIndexesName, "rb"); + const std::string osIndexesName = CPLFormFilenameSafe( + CPLGetPathSafe(m_osFilename.c_str()).c_str(), + CPLGetBasenameSafe(m_osFilename.c_str()).c_str(), "gdbindexes"); + VSILFILE *fpIndexes = VSIFOpenL(osIndexesName.c_str(), "rb"); VSIStatBufL sStat; if (fpIndexes == nullptr) { - if (VSIStatExL(pszIndexesName, &sStat, VSI_STAT_EXISTS_FLAG) == 0) + if (VSIStatExL(osIndexesName.c_str(), &sStat, VSI_STAT_EXISTS_FLAG) == + 0) returnError(); else return 0; @@ -2574,7 +2576,7 @@ int FileGDBTable::GetIndexCount() // Cf files a00000029.gdbindexes, a000000ea.gdbindexes, a000000ed.gdbindexes, // a000000f8.gdbindexes, a000000fb.gdbindexes, a00000103.gdbindexes // from https://github.com/OSGeo/gdal/issues/11295#issuecomment-2491158506 - CPLDebug("OpenFileGDB", "Reading %s", pszIndexesName); + CPLDebug("OpenFileGDB", "Reading %s", osIndexesName.c_str()); CPLDebug( "OpenFileGDB", "Strange (deleted?) index descriptor at index %u of name %s", i, @@ -2669,12 +2671,12 @@ bool FileGDBTable::HasSpatialIndex() { if (m_nHasSpatialIndex < 0) { - const char *pszSpxName = - CPLFormFilename(CPLGetPathSafe(m_osFilename.c_str()).c_str(), - CPLGetBasename(m_osFilename.c_str()), "spx"); + const std::string osSpxName = CPLFormFilenameSafe( + CPLGetPathSafe(m_osFilename.c_str()).c_str(), + CPLGetBasenameSafe(m_osFilename.c_str()).c_str(), "spx"); VSIStatBufL sStat; m_nHasSpatialIndex = - (VSIStatExL(pszSpxName, &sStat, VSI_STAT_EXISTS_FLAG) == 0); + (VSIStatExL(osSpxName.c_str(), &sStat, VSI_STAT_EXISTS_FLAG) == 0); } return m_nHasSpatialIndex != FALSE; } diff --git a/ogr/ogrsf_frmts/openfilegdb/filegdbtable_write.cpp b/ogr/ogrsf_frmts/openfilegdb/filegdbtable_write.cpp index 74d6d9b1b9b9..bc09671cec58 100644 --- a/ogr/ogrsf_frmts/openfilegdb/filegdbtable_write.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/filegdbtable_write.cpp @@ -2128,12 +2128,12 @@ bool FileGDBTable::WholeFileRewriter::Begin() #endif )); - m_osGdbTablx = CPLFormFilename( - CPLGetPath(m_oTable.m_osFilename.c_str()), - CPLGetBasename(m_oTable.m_osFilename.c_str()), "gdbtablx"); + m_osGdbTablx = CPLFormFilenameSafe( + CPLGetPathSafe(m_oTable.m_osFilename.c_str()).c_str(), + CPLGetBasenameSafe(m_oTable.m_osFilename.c_str()).c_str(), "gdbtablx"); - m_osBackupGdbTable = - CPLResetExtension(m_oTable.m_osFilename.c_str(), "_backup.gdbtable"); + m_osBackupGdbTable = CPLResetExtensionSafe(m_oTable.m_osFilename.c_str(), + "_backup.gdbtable"); VSIStatBufL sStat; if (VSIStatL(m_osBackupGdbTable.c_str(), &sStat) == 0) { @@ -2144,7 +2144,7 @@ bool FileGDBTable::WholeFileRewriter::Begin() } m_osBackupGdbTablx = - CPLResetExtension(m_osGdbTablx.c_str(), "_backup.gdbtablx"); + CPLResetExtensionSafe(m_osGdbTablx.c_str(), "_backup.gdbtablx"); if (m_bModifyInPlace) { diff --git a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp index 4f7ca0a01556..623cf41f3d24 100644 --- a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource_write.cpp @@ -1498,7 +1498,7 @@ OGRErr OGROpenFileGDBDataSource::DeleteLayer(int iLayer) const std::string osDirname = CPLGetPathSafe(poLayer->GetFilename().c_str()); const std::string osFilenameBase = - CPLGetBasename(poLayer->GetFilename().c_str()); + CPLGetBasenameSafe(poLayer->GetFilename().c_str()); if (m_bInTransaction) { @@ -2504,7 +2504,7 @@ OGRErr OGROpenFileGDBDataSource::RollbackTransaction() for (auto poLayer : m_oSetLayersCreatedInTransaction) { const std::string osThisBasename = - CPLGetBasename(poLayer->GetFilename().c_str()); + CPLGetBasenameSafe(poLayer->GetFilename().c_str()); poLayer->Close(); char **papszFiles = VSIReadDir(m_osDirName.c_str()); diff --git a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdrivercore.cpp b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdrivercore.cpp index 56957e0dfd9a..2a2d7b93ae25 100644 --- a/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdrivercore.cpp +++ b/ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdrivercore.cpp @@ -106,11 +106,12 @@ GDALIdentifyEnum OGROpenFileGDBDriverIdentify(GDALOpenInfo *poOpenInfo, } #ifdef FOR_FUSIL /* To be able to test fuzzer on any auxiliary files used (indexes, etc.) */ - else if (strlen(CPLGetBasenameSafe(pszFilename).c_str()) == 9 && - CPLGetBasename(pszFilename)[0] == 'a') + else if (CPLGetBasenameSafe(pszFilename).size() == 9 && + CPLGetBasenameSafe(pszFilename)[0] == 'a') { pszFilename = CPLFormFilename(CPLGetPathSafe(pszFilename).c_str(), - CPLGetBasename(pszFilename), "gdbtable"); + CPLGetBasenameSafe(pszFilename).c_str(), + "gdbtable"); return GDAL_IDENTIFY_TRUE; } else if (strlen(CPLGetBasenameSafe(CPLGetBasename(pszFilename)).c_str()) == diff --git a/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp b/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp index 1a2ca1d1f91b..5bc06cdaa99e 100644 --- a/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp +++ b/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp @@ -268,8 +268,7 @@ bool OGRShapeDataSource::Open(GDALOpenInfo *poOpenInfo, bool bTestOpen, for (int iCan = 0; iCan < nCandidateCount; iCan++) { const char *pszCandidate = papszCandidates[iCan]; - const char *pszLayerName = CPLGetBasename(pszCandidate); - CPLString osLayerName(pszLayerName); + CPLString osLayerName(CPLGetBasenameSafe(pszCandidate)); #ifdef _WIN32 // On Windows, as filenames are case insensitive, a shapefile layer // can be made of foo.shp and FOO.DBF, so to detect unique layer @@ -310,8 +309,8 @@ bool OGRShapeDataSource::Open(GDALOpenInfo *poOpenInfo, bool bTestOpen, for (int iCan = 0; iCan < nCandidateCount; iCan++) { const char *pszCandidate = papszCandidates[iCan]; - const char *pszLayerName = CPLGetBasename(pszCandidate); - CPLString osLayerName(pszLayerName); + const std::string osLayerNameOri = CPLGetBasenameSafe(pszCandidate); + CPLString osLayerName(osLayerNameOri); #ifdef _WIN32 osLayerName.toupper(); #endif @@ -336,8 +335,9 @@ bool OGRShapeDataSource::Open(GDALOpenInfo *poOpenInfo, bool bTestOpen, { const char *pszCandidate2 = papszCandidates[iCan2]; - if (EQUALN(pszCandidate2, pszLayerName, strlen(pszLayerName)) && - EQUAL(pszCandidate2 + strlen(pszLayerName), ".tab")) + if (EQUALN(pszCandidate2, osLayerNameOri.c_str(), + osLayerNameOri.size()) && + EQUAL(pszCandidate2 + osLayerNameOri.size(), ".tab")) bFoundTAB = true; } @@ -969,12 +969,12 @@ int OGRShapeDataSource::GetLayerCount() for (size_t i = 0; i < oVectorLayerName.size(); i++) { const char *pszFilename = oVectorLayerName[i].c_str(); - const char *pszLayerName = CPLGetBasename(pszFilename); + const std::string osLayerName = CPLGetBasenameSafe(pszFilename); int j = 0; // Used after for. for (; j < nLayers; j++) { - if (strcmp(papoLayers[j]->GetName(), pszLayerName) == 0) + if (papoLayers[j]->GetName() == osLayerName) break; } if (j < nLayers) @@ -1034,16 +1034,16 @@ OGRLayer *OGRShapeDataSource::GetLayerByName(const char *pszLayerNameIn) for (size_t i = 0; i < oVectorLayerName.size(); i++) { const char *pszFilename = oVectorLayerName[i].c_str(); - const char *pszLayerName = CPLGetBasename(pszFilename); + const std::string osLayerName = CPLGetBasenameSafe(pszFilename); if (j == 0) { - if (strcmp(pszLayerName, pszLayerNameIn) != 0) + if (osLayerName != pszLayerNameIn) continue; } else { - if (!EQUAL(pszLayerName, pszLayerNameIn)) + if (!EQUAL(osLayerName.c_str(), pszLayerNameIn)) continue; } diff --git a/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp b/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp index 6d1d68f394cc..088cbb254b99 100644 --- a/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp +++ b/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp @@ -142,7 +142,7 @@ OGRShapeLayer::OGRShapeLayer(OGRShapeDataSource *poDSIn, SetMetadataItem("SOURCE_ENCODING", osEncoding, "SHAPEFILE"); poFeatureDefn = SHPReadOGRFeatureDefn( - CPLGetBasename(pszFullName), hSHP, hDBF, osEncoding, + CPLGetBasenameSafe(pszFullName).c_str(), hSHP, hDBF, osEncoding, CPLFetchBool(poDS->GetOpenOptions(), "ADJUST_TYPE", false)); // To make sure that @@ -2849,9 +2849,9 @@ OGRErr OGRShapeLayer::Repack() while (papszCandidates != nullptr && papszCandidates[i] != nullptr) { const CPLString osCandidateBasename = - CPLGetBasename(papszCandidates[i]); + CPLGetBasenameSafe(papszCandidates[i]); const CPLString osCandidateExtension = - CPLGetExtension(papszCandidates[i]); + CPLGetExtensionSafe(papszCandidates[i]); #ifdef _WIN32 // On Windows, as filenames are case insensitive, a shapefile layer can // be made of foo.shp and FOO.DBF, so use case insensitive comparison. diff --git a/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp b/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp index 6e4086e79686..d83156fb6bc7 100644 --- a/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp +++ b/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp @@ -179,11 +179,11 @@ static GDALDataset *OGRSQLiteDriverOpen(GDALOpenInfo *poOpenInfo) if (pszLastDot) *pszLastDot = '\0'; - const char *pszTableName = CPLGetBasename(pszSQLiteFilename); + const std::string osTableName = CPLGetBasenameSafe(pszSQLiteFilename); char *pszSQL = CPLStrdup(CPLSPrintf( "CREATE VIRTUAL TABLE %s USING VirtualShape(%s, CP1252, -1)", - pszTableName, pszSQLiteFilename)); + osTableName.c_str(), pszSQLiteFilename)); poDS->ExecuteSQL(pszSQL, nullptr, nullptr); CPLFree(pszSQL); CPLFree(pszSQLiteFilename); diff --git a/ogr/ogrsf_frmts/sqlite/rasterlite2.cpp b/ogr/ogrsf_frmts/sqlite/rasterlite2.cpp index ce87ce98780c..43edf9459a32 100644 --- a/ogr/ogrsf_frmts/sqlite/rasterlite2.cpp +++ b/ogr/ogrsf_frmts/sqlite/rasterlite2.cpp @@ -2004,8 +2004,8 @@ GDALDataset *OGRSQLiteDriverCreateCopy(const char *pszName, } } - CPLString osCoverageName(CSLFetchNameValueDef(papszOptions, "COVERAGE", - CPLGetBasename(pszName))); + const std::string osCoverageName(CSLFetchNameValueDef( + papszOptions, "COVERAGE", CPLGetBasenameSafe(pszName).c_str())); // Check if the coverage already exists rl2CoveragePtr cvg = nullptr; char *pszSQL = sqlite3_mprintf( @@ -2019,7 +2019,7 @@ GDALDataset *OGRSQLiteDriverCreateCopy(const char *pszName, if (nRowCount == 1) { cvg = rl2_create_coverage_from_dbms(poDS->GetDB(), nullptr, - osCoverageName); + osCoverageName.c_str()); if (cvg == nullptr) { delete poDS; @@ -2071,7 +2071,7 @@ GDALDataset *OGRSQLiteDriverCreateCopy(const char *pszName, } if (rl2_create_dbms_coverage( - poDS->GetDB(), osCoverageName, nSampleType, nPixelType, + poDS->GetDB(), osCoverageName.c_str(), nSampleType, nPixelType, nBandCount, nCompression, nQuality, nTileWidth, nTileHeight, nSRSId, dfXRes, dfYRes, pNoData, pPalette, bStrictResolution, bMixedResolutions, bSectionPaths, bSectionMD5, bSectionSummary, @@ -2092,7 +2092,7 @@ GDALDataset *OGRSQLiteDriverCreateCopy(const char *pszName, if (cvg == nullptr) { cvg = rl2_create_coverage_from_dbms(poDS->GetDB(), nullptr, - osCoverageName); + osCoverageName.c_str()); if (cvg == nullptr) { if (pPalette) @@ -2148,7 +2148,7 @@ GDALDataset *OGRSQLiteDriverCreateCopy(const char *pszName, GDALOpenInfo oOpenInfo( CPLSPrintf("RASTERLITE2:%s:%s", EscapeNameAndQuoteIfNeeded(pszName).c_str(), - EscapeNameAndQuoteIfNeeded(osCoverageName).c_str()), + EscapeNameAndQuoteIfNeeded(osCoverageName.c_str()).c_str()), GDAL_OF_RASTER | GDAL_OF_UPDATE); poDS->Open(&oOpenInfo); return poDS; diff --git a/ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp b/ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp index 2404dbd5ab52..edaacdaa8092 100644 --- a/ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp +++ b/ogr/ogrsf_frmts/tiger/ogrtigerdatasource.cpp @@ -363,8 +363,9 @@ int OGRTigerDataSource::Open(const char *pszFilename, int bTestOpen, size_t nCandidateLen = strlen(candidateFileList[i]); if (papszLimitedFileList != nullptr && - CSLFindString(papszLimitedFileList, - CPLGetBasename(candidateFileList[i])) == -1) + CSLFindString( + papszLimitedFileList, + CPLGetBasenameSafe(candidateFileList[i]).c_str()) == -1) { continue; }