Skip to content

Commit

Permalink
Automated use of RFC105 safe functions
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
rouault committed Jan 12, 2025
1 parent 26bfaf6 commit 3e1327c
Show file tree
Hide file tree
Showing 53 changed files with 151 additions and 134 deletions.
2 changes: 1 addition & 1 deletion frmts/adrg/adrgdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
11 changes: 6 additions & 5 deletions frmts/adrg/srpdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -1593,7 +1594,7 @@ GDALDataset *SRPDataset::Open(GDALOpenInfo *poOpenInfo)

nRecordIndex = static_cast<int>(CPLScanLong(basename + 6, 2));

CPLString path = CPLGetDirname(osFileName);
CPLString path = CPLGetDirnameSafe(osFileName);
CPLString basename01 = ResetTo01(basename);
osFileName = CPLFormFilename(path, basename01, ".IMG");

Expand Down
2 changes: 1 addition & 1 deletion frmts/aigrid/aigdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions frmts/derived/deriveddataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
18 changes: 11 additions & 7 deletions frmts/dimap/dimapdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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++)
{
Expand Down
4 changes: 2 additions & 2 deletions frmts/ers/ersdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion frmts/gif/biggifdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
8 changes: 4 additions & 4 deletions frmts/hfa/hfadataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
8 changes: 4 additions & 4 deletions frmts/hfa/hfaopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions frmts/ilwis/ilwiscoordinatesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions frmts/ilwis/ilwisdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,7 @@ GDALDataset *KmlSingleDocRasterDataset::Open(const char *pszFilename,
return nullptr;

std::vector<KmlSingleDocRasterTilesDesc> aosDescs;
CPLString osDirname = CPLGetPath(osFilename);
CPLString osDirname = CPLGetPathSafe(osFilename);
KmlSingleDocCollectTiles(psRootFolder, aosDescs, osDirname);
if (aosDescs.empty())
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion frmts/map/mapdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion frmts/nitf/nitfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion frmts/pcidsk/pcidskdataset2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ char **PCIDSK2Dataset::GetFileList()

{
char **papszFileList = GDALPamDataset::GetFileList();
CPLString osBaseDir = CPLGetPath(GetDescription());
CPLString osBaseDir = CPLGetPathSafe(GetDescription());

try
{
Expand Down
2 changes: 1 addition & 1 deletion frmts/pdf/pdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 4 additions & 4 deletions frmts/pds/isis2dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ 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);
poDS->osExternalCube = osTargetFile;
}
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);
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions frmts/pds/isis3dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions frmts/pds/pds4vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
10 changes: 5 additions & 5 deletions frmts/pds/pdsdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 =
Expand Down
Loading

0 comments on commit 3e1327c

Please sign in to comment.