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/const char \*([a-zA-Z_][a-zA-Z0-9_]*)\(${func}\(([^()]*(\([^()]*\))?[^()]*)\)\);/const char *\1 = ${func}(\2);/g" {} \;
done

for func in "${funcs[@]}"; do
  find frmts ogr -name "*.cpp" -exec sed -i -E "s/CPLString\(${func}\(([^()]*(\([^()]*\))?[^()]*)\)\)/CPLString(${func}Safe(\1))/g" {} \;
  find frmts ogr -name "*.cpp" -exec sed -i -E "s/std::string\(${func}\(([^()]*(\([^()]*\))?[^()]*)\)\)/std::string(${func}Safe(\1))/g" {} \;
done

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

for func in "${funcs[@]}"; do
  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" {} \;
  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" {} \;
done

for func in "${funcs[@]}"; do
  find frmts ogr -name "*.cpp" -exec sed -i -E "s/\(${func}\(([^()]*(\([^()]*\))?[^()]*)\)/(${func}Safe(\1).c_str()/g" {} \;
  find frmts ogr -name "*.cpp" -exec sed -i -E "s/, ${func}\(([^()]*(\([^()]*\))?[^()]*)\)/, ${func}Safe(\1).c_str()/g" {} \;
done
```
  • Loading branch information
rouault committed Jan 12, 2025
1 parent 0f95b37 commit 48542cf
Show file tree
Hide file tree
Showing 121 changed files with 601 additions and 473 deletions.
13 changes: 7 additions & 6 deletions frmts/aaigrid/aaigriddataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1218,9 +1218,10 @@ GDALDataset *AAIGDataset::CommonOpen(GDALOpenInfo *poOpenInfo,
}

// Try to read projection file.
char *const pszDirname = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename));
char *const pszDirname =
CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str());
char *const pszBasename =
CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename));
CPLStrdup(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str());

poDS->osPrjFilename = CPLFormFilename(pszDirname, pszBasename, "prj");
int nRet = 0;
Expand Down Expand Up @@ -1557,10 +1558,10 @@ GDALDataset *AAIGDataset::CreateCopy(const char *pszFilename,
const char *pszOriginalProjection = poSrcDS->GetProjectionRef();
if (!EQUAL(pszOriginalProjection, ""))
{
char *pszDirname = CPLStrdup(CPLGetPath(pszFilename));
char *pszBasename = CPLStrdup(CPLGetBasename(pszFilename));
char *pszPrjFilename =
CPLStrdup(CPLFormFilename(pszDirname, pszBasename, "prj"));
char *pszDirname = CPLStrdup(CPLGetPathSafe(pszFilename).c_str());
char *pszBasename = CPLStrdup(CPLGetBasenameSafe(pszFilename).c_str());
char *pszPrjFilename = CPLStrdup(
CPLFormFilenameSafe(pszDirname, pszBasename, "prj").c_str());
VSILFILE *fp = VSIFOpenL(pszPrjFilename, "wt");
if (fp != nullptr)
{
Expand Down
8 changes: 4 additions & 4 deletions frmts/adrg/adrgdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ char **ADRGDataset::GetGENListFromTHF(const char *pszFileName)
{
CPLDebug("ADRG", "Found GEN file in THF : %s",
osSubFileName.c_str());
CPLString osGENFileName(CPLGetDirname(pszFileName));
CPLString osGENFileName(CPLGetDirnameSafe(pszFileName));
char **tokens =
CSLTokenizeString2(osSubFileName.c_str(), "/\"", 0);
char **ptr = tokens;
Expand Down Expand Up @@ -1472,7 +1472,7 @@ char **ADRGDataset::GetIMGListFromGEN(const char *pszFileName,
CPLDebug("ADRG", "BAD=%s", osBAD.c_str());

/* Build full IMG file name from BAD value */
CPLString osGENDir(CPLGetDirname(pszFileName));
CPLString osGENDir(CPLGetDirnameSafe(pszFileName));

const CPLString osFileName =
CPLFormFilename(osGENDir.c_str(), osBAD.c_str(), nullptr);
Expand Down Expand Up @@ -1704,7 +1704,7 @@ GDALDataset *ADRGDataset::Create(const char *pszFilename, int nXSize,
return nullptr;
}

CPLString osBaseFileName(CPLGetBasename(pszFilename));
CPLString osBaseFileName(CPLGetBasenameSafe(pszFilename));
if (osBaseFileName.size() != 8 || osBaseFileName[6] != DIGIT_ZERO ||
osBaseFileName[7] != '1')
{
Expand Down Expand Up @@ -1733,7 +1733,7 @@ GDALDataset *ADRGDataset::Create(const char *pszFilename, int nXSize,
return nullptr;
}

CPLString osDirname(CPLGetDirname(pszFilename));
CPLString osDirname(CPLGetDirnameSafe(pszFilename));
CPLString osTransh01THF(
CPLFormFilename(osDirname.c_str(), "TRANSH01.THF", nullptr));
VSILFILE *fdTHF = VSIFOpenL(osTransh01THF.c_str(), "wb");
Expand Down
7 changes: 4 additions & 3 deletions frmts/adrg/srpdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ char **SRPDataset::GetGENListFromTHF(const char *pszFileName)
if (!module.Open(pszFileName, TRUE))
return papszFileNames;

CPLString osDirName(CPLGetDirname(pszFileName));
CPLString osDirName(CPLGetDirnameSafe(pszFileName));

while (true)
{
Expand Down Expand Up @@ -1163,7 +1163,8 @@ char **SRPDataset::GetGENListFromTHF(const char *pszFileName)
{
if (EQUAL(CPLGetExtensionSafe(*ptrDir).c_str(),
"GEN") &&
EQUALN(CPLGetBasename(*ptrDir), osName, 6))
EQUALN(CPLGetBasenameSafe(*ptrDir).c_str(),
osName, 6))
{
bFound = 1;
osGENFileName = CPLFormFilename(
Expand Down Expand Up @@ -1401,7 +1402,7 @@ char **SRPDataset::GetIMGListFromGEN(const char *pszFileName,
CPLDebug("SRP", "BAD=%s", osBAD.c_str());

/* Build full IMG file name from BAD value */
const CPLString osGENDir(CPLGetDirname(pszFileName));
const CPLString osGENDir(CPLGetDirnameSafe(pszFileName));

const CPLString osFileName =
CPLFormFilename(osGENDir.c_str(), osBAD.c_str(), nullptr);
Expand Down
5 changes: 3 additions & 2 deletions frmts/ceos2/sar_ceosdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1822,8 +1822,9 @@ GDALDataset *SAR_CEOSDataset::Open(GDALOpenInfo *poOpenInfo)
/* -------------------------------------------------------------------- */
/* Try the various filenames. */
/* -------------------------------------------------------------------- */
char *pszPath = CPLStrdup(CPLGetPath(poOpenInfo->pszFilename));
char *pszBasename = CPLStrdup(CPLGetBasename(poOpenInfo->pszFilename));
char *pszPath = CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str());
char *pszBasename =
CPLStrdup(CPLGetBasenameSafe(poOpenInfo->pszFilename).c_str());
char *pszExtension =
CPLStrdup(CPLGetExtensionSafe(poOpenInfo->pszFilename).c_str());

Expand Down
5 changes: 3 additions & 2 deletions frmts/coasp/coasp_dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,9 @@ GDALDataset *COASPDataset::Open(GDALOpenInfo *poOpenInfo)
poDS->pszFileName = VSIStrdup(poOpenInfo->pszFilename);

/* determine the file name prefix */
char *pszBaseName = VSIStrdup(CPLGetBasename(poDS->pszFileName));
char *pszDir = VSIStrdup(CPLGetPath(poDS->pszFileName));
char *pszBaseName =
VSIStrdup(CPLGetBasenameSafe(poDS->pszFileName).c_str());
char *pszDir = VSIStrdup(CPLGetPathSafe(poDS->pszFileName).c_str());
const char *pszExt = "rc";
int nNull = static_cast<int>(strlen(pszBaseName)) - 1;
if (nNull <= 0)
Expand Down
3 changes: 2 additions & 1 deletion frmts/esric/esric_dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ GDALDataset *ECDataset::Open(GDALOpenInfo *poOpenInfo,
tileBundlesPath.erase(0, 2);
}

ds->dname.Printf("%s/%s", CPLGetDirname(poOpenInfo->pszFilename),
ds->dname.Printf("%s/%s",
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(),
tileBundlesPath.c_str());
CPLErr error = ds->InitializeFromJSON(oRoot);
if (CE_None != error)
Expand Down
9 changes: 5 additions & 4 deletions frmts/gti/gdaltileindexdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,16 @@ static std::string GetAbsoluteFileName(const char *pszTileName,
const std::string osRet =
CPLIsFilenameRelative(osPath.c_str())
? oSubDSInfo->ModifyPathComponent(
CPLProjectRelativeFilename(CPLGetPath(pszVRTName),
osPath.c_str()))
CPLProjectRelativeFilename(
CPLGetPathSafe(pszVRTName).c_str(),
osPath.c_str()))
: std::string(pszTileName);
GDALDestroySubdatasetInfo(oSubDSInfo);
return osRet;
}

const std::string osRelativeMadeAbsolute =
CPLProjectRelativeFilename(CPLGetPath(pszVRTName), pszTileName);
const std::string osRelativeMadeAbsolute = CPLProjectRelativeFilename(
CPLGetPathSafe(pszVRTName).c_str(), pszTileName);
VSIStatBufL sStat;
if (VSIStatL(osRelativeMadeAbsolute.c_str(), &sStat) == 0)
return osRelativeMadeAbsolute;
Expand Down
3 changes: 2 additions & 1 deletion frmts/gtiff/cogdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ static CPLString GetTmpFilename(const char *pszFilename, const char *pszExt)
if (!bSupportsRandomWrite ||
CPLGetConfigOption("CPL_TMPDIR", nullptr) != nullptr)
{
osTmpFilename = CPLGenerateTempFilename(CPLGetBasename(pszFilename));
osTmpFilename =
CPLGenerateTempFilename(CPLGetBasenameSafe(pszFilename).c_str());
}
else
osTmpFilename = pszFilename;
Expand Down
2 changes: 1 addition & 1 deletion frmts/gtiff/gtiffdataset_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5771,7 +5771,7 @@ CSLConstList GTiffDataset::GetSiblingFiles()
const int nMaxFiles =
atoi(CPLGetConfigOption("GDAL_READDIR_LIMIT_ON_OPEN", "1000"));
CPLStringList aosSiblingFiles(
VSIReadDirEx(CPLGetDirname(m_pszFilename), nMaxFiles));
VSIReadDirEx(CPLGetDirnameSafe(m_pszFilename).c_str(), nMaxFiles));
if (nMaxFiles > 0 && aosSiblingFiles.size() > nMaxFiles)
{
CPLDebug("GTiff", "GDAL_READDIR_LIMIT_ON_OPEN reached on %s",
Expand Down
2 changes: 1 addition & 1 deletion frmts/gtiff/gtiffdataset_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5515,7 +5515,7 @@ TIFF *GTiffDataset::CreateLL(const char *pszFilename, int nXSize, int nYSize,
CPLTestBool(CPLGetConfigOption("CHECK_DISK_FREE_SPACE", "TRUE")))
{
GIntBig nFreeDiskSpace =
VSIGetDiskFreeSpace(CPLGetDirname(pszFilename));
VSIGetDiskFreeSpace(CPLGetDirnameSafe(pszFilename).c_str());
if (nFreeDiskSpace >= 0 && nFreeDiskSpace < dfUncompressedImageSize)
{
ReportError(pszFilename, CE_Failure, CPLE_FileIO,
Expand Down
2 changes: 1 addition & 1 deletion frmts/hdf4/hdf4imagedataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ CPLErr HDF4ImageRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff,
/* to look in to find the external files. Normally this is the */
/* directory holding the hdf file. */
/* -------------------------------------------------------------------- */
HXsetdir(CPLGetPath(poGDS->pszFilename));
HXsetdir(CPLGetPathSafe(poGDS->pszFilename).c_str());

/* -------------------------------------------------------------------- */
/* Handle different configurations. */
Expand Down
11 changes: 6 additions & 5 deletions frmts/hdf5/s100.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,9 @@ std::string S100ReadMetadata(GDALDataset *poDS, const std::string &osFilename,
const char *pszVal = poAttr->ReadAsString();
if (pszVal && pszVal[0])
{
osMetadataFile = CPLFormFilename(CPLGetPath(osFilename.c_str()),
pszVal, nullptr);
osMetadataFile =
CPLFormFilename(CPLGetPathSafe(osFilename.c_str()).c_str(),
pszVal, nullptr);
VSIStatBufL sStat;
if (VSIStatL(osMetadataFile.c_str(), &sStat) != 0)
{
Expand All @@ -427,9 +428,9 @@ std::string S100ReadMetadata(GDALDataset *poDS, const std::string &osFilename,
// but the actual filename does not start with "MD_"...
if (STARTS_WITH(pszVal, "MD_"))
{
osMetadataFile =
CPLFormFilename(CPLGetPath(osFilename.c_str()),
pszVal + strlen("MD_"), nullptr);
osMetadataFile = CPLFormFilename(
CPLGetPathSafe(osFilename.c_str()).c_str(),
pszVal + strlen("MD_"), nullptr);
if (VSIStatL(osMetadataFile.c_str(), &sStat) != 0)
{
osMetadataFile.clear();
Expand Down
6 changes: 3 additions & 3 deletions frmts/hfa/hfaband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ CPLErr HFABand::LoadOverviews()
if (psHFA == nullptr)
{
char *pszBasename =
CPLStrdup(CPLGetBasename(psInfo->pszFilename));
CPLStrdup(CPLGetBasenameSafe(psInfo->pszFilename).c_str());

pszJustFilename =
CPLStrdup(CPLFormFilename(nullptr, pszBasename, "rrd"));
pszJustFilename = CPLStrdup(
CPLFormFilenameSafe(nullptr, pszBasename, "rrd").c_str());
CPLDebug("HFA",
"Failed to find overview file with "
"expected name, try %s instead.",
Expand Down
20 changes: 10 additions & 10 deletions frmts/hfa/hfaopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ HFAHandle HFAOpen(const char *pszFilename, const char *pszAccess)
static_cast<HFAInfo_t *>(CPLCalloc(sizeof(HFAInfo_t), 1));

psInfo->pszFilename = CPLStrdup(CPLGetFilename(pszFilename));
psInfo->pszPath = CPLStrdup(CPLGetPath(pszFilename));
psInfo->pszPath = CPLStrdup(CPLGetPathSafe(pszFilename).c_str());
psInfo->fp = fp;
if (EQUAL(pszAccess, "r") || EQUAL(pszAccess, "rb"))
psInfo->eAccess = HFA_ReadOnly;
Expand Down Expand Up @@ -340,8 +340,8 @@ HFAInfo_t *HFAGetDependent(HFAInfo_t *psBase, const char *pszFilename)
// Try to open the dependent file.
const char *pszMode = psBase->eAccess == HFA_Update ? "r+b" : "rb";

char *pszDependent =
CPLStrdup(CPLFormFilename(psBase->pszPath, pszFilename, nullptr));
char *pszDependent = CPLStrdup(
CPLFormFilenameSafe(psBase->pszPath, pszFilename, nullptr).c_str());

VSILFILE *fp = VSIFOpenL(pszDependent, pszMode);
if (fp != nullptr)
Expand Down Expand Up @@ -1808,7 +1808,7 @@ HFAHandle HFACreateLL(const char *pszFilename)
psInfo->pProParameters = nullptr;
psInfo->bTreeDirty = false;
psInfo->pszFilename = CPLStrdup(CPLGetFilename(pszFilename));
psInfo->pszPath = CPLStrdup(CPLGetPath(pszFilename));
psInfo->pszPath = CPLStrdup(CPLGetPathSafe(pszFilename).c_str());

// Write out the Ehfa_HeaderTag.
bool bRet = VSIFWriteL((void *)"EHFA_HEADER_TAG", 1, 16, fp) > 0;
Expand Down Expand Up @@ -2957,14 +2957,14 @@ bool HFACreateSpillStack(HFAInfo_t *psInfo, int nXSize, int nYSize, int nLayers,
{
const auto osExt = CPLGetExtensionSafe(psInfo->pszFilename);
if (EQUAL(osExt.c_str(), "rrd"))
psInfo->pszIGEFilename =
CPLStrdup(CPLResetExtension(psInfo->pszFilename, "rde"));
psInfo->pszIGEFilename = CPLStrdup(
CPLResetExtensionSafe(psInfo->pszFilename, "rde").c_str());
else if (EQUAL(osExt.c_str(), "aux"))
psInfo->pszIGEFilename =
CPLStrdup(CPLResetExtension(psInfo->pszFilename, "axe"));
psInfo->pszIGEFilename = CPLStrdup(
CPLResetExtensionSafe(psInfo->pszFilename, "axe").c_str());
else
psInfo->pszIGEFilename =
CPLStrdup(CPLResetExtension(psInfo->pszFilename, "ige"));
psInfo->pszIGEFilename = CPLStrdup(
CPLResetExtensionSafe(psInfo->pszFilename, "ige").c_str());
}

char *pszFullFilename = CPLStrdup(
Expand Down
3 changes: 2 additions & 1 deletion frmts/http/httpdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ static GDALDataset *HTTPOpen(GDALOpenInfo *poOpenInfo)
CPLString osTempFilename;

#ifdef _WIN32
const char *pszPath = CPLGetPath(CPLGenerateTempFilename(NULL));
const char *pszPath =
CPLGetPath(CPLGenerateTempFilenameSafe(NULL).c_str());
#else
const char *pszPath = "/tmp";
#endif
Expand Down
9 changes: 5 additions & 4 deletions frmts/idrisi/IdrisiDataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2393,8 +2393,9 @@ CPLErr IdrisiGeoReference2Wkt(const char *pszFilename, const char *pszRefSystem,
// Search for georeference file <RefSystem>.ref
// ------------------------------------------------------------------

const char *pszFName = CPLSPrintf("%s%c%s.ref", CPLGetDirname(pszFilename),
PATHDELIM, pszRefSystem);
const char *pszFName =
CPLSPrintf("%s%c%s.ref", CPLGetDirnameSafe(pszFilename).c_str(),
PATHDELIM, pszRefSystem);

if (!FileExists(pszFName))
{
Expand Down Expand Up @@ -3129,10 +3130,10 @@ CPLErr IdrisiDataset::Wkt2GeoReference(const OGRSpatialReference &oSRS,
papszRef =
CSLAddNameValue(papszRef, refSTANDL_2, CPLSPrintf("%.9g", dfStdP2));
myCSLSetNameValueSeparator(papszRef, ": ");
SaveAsCRLF(papszRef, CPLResetExtension(pszFilename, extREF));
SaveAsCRLF(papszRef, CPLResetExtensionSafe(pszFilename, extREF).c_str());
CSLDestroy(papszRef);

*pszRefSystem = CPLStrdup(CPLGetBasename(pszFilename));
*pszRefSystem = CPLStrdup(CPLGetBasenameSafe(pszFilename).c_str());
*pszRefUnit = CPLStrdup(pszLinearUnit);

CPLFree(pszGeorefName);
Expand Down
4 changes: 2 additions & 2 deletions frmts/ilwis/ilwiscoordinatesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,8 @@ CPLErr ILWISDataset::WriteProjection()
OGRSpatialReference *poGeogSRS = nullptr;

std::string csFileName = CPLResetExtension(osFileName, "csy");
std::string pszBaseName = std::string(CPLGetBasename(osFileName));
// std::string pszPath = std::string(CPLGetPath( osFileName ));
std::string pszBaseName = std::string(CPLGetBasenameSafe(osFileName));
// std::string pszPath = std::string(CPLGetPathSafe( osFileName ));
const bool bHaveSRS = !m_oSRS.IsEmpty();

const IlwisDatums *piwDatum = iwDatums;
Expand Down
Loading

0 comments on commit 48542cf

Please sign in to comment.