Skip to content

Commit

Permalink
VSIArchiveFilesystemHandler::SplitFilename(): revet no longer need ha…
Browse files Browse the repository at this point in the history
…ck of c80a461
  • Loading branch information
rouault committed Jan 15, 2025
1 parent df669f9 commit 99dc672
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions port/cpl_vsil_abstract_archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,20 +452,15 @@ char *VSIArchiveFilesystemHandler::SplitFilename(const char *pszFilename,

const std::vector<CPLString> oExtensions = GetExtensions();
int nAttempts = 0;
// If we are called with pszFilename being one of the TLS buffers returned
// by cpl_path.cpp functions, then a call to Stat() in the loop (and its
// cascaded calls to other cpl_path.cpp functions) might lead to altering
// the buffer to be altered, hence take a copy
const std::string osFilenameCopy(pszFilename);
while (i < static_cast<int>(osFilenameCopy.size()))
while (pszFilename[i])
{
int nToSkip = 0;

for (std::vector<CPLString>::const_iterator iter = oExtensions.begin();
iter != oExtensions.end(); ++iter)
{
const CPLString &osExtension = *iter;
if (EQUALN(osFilenameCopy.c_str() + i, osExtension.c_str(),
if (EQUALN(pszFilename + i, osExtension.c_str(),
osExtension.size()))
{
nToSkip = static_cast<int>(osExtension.size());
Expand All @@ -475,8 +470,7 @@ char *VSIArchiveFilesystemHandler::SplitFilename(const char *pszFilename,

#ifdef DEBUG
// For AFL, so that .cur_input is detected as the archive filename.
if (EQUALN(osFilenameCopy.c_str() + i, ".cur_input",
strlen(".cur_input")))
if (EQUALN(pszFilename + i, ".cur_input", strlen(".cur_input")))
{
nToSkip = static_cast<int>(strlen(".cur_input"));
}
Expand All @@ -492,7 +486,7 @@ char *VSIArchiveFilesystemHandler::SplitFilename(const char *pszFilename,
break;
}
VSIStatBufL statBuf;
char *archiveFilename = CPLStrdup(osFilenameCopy.c_str());
char *archiveFilename = CPLStrdup(pszFilename);
bool bArchiveFileExists = false;

if (IsEitherSlash(archiveFilename[i + nToSkip]))
Expand Down Expand Up @@ -533,11 +527,10 @@ char *VSIArchiveFilesystemHandler::SplitFilename(const char *pszFilename,

if (bArchiveFileExists)
{
if (IsEitherSlash(osFilenameCopy[i + nToSkip]) &&
i + nToSkip + 1 < static_cast<int>(osFilenameCopy.size()))
if (IsEitherSlash(pszFilename[i + nToSkip]))
{
osFileInArchive = CompactFilename(osFilenameCopy.c_str() +
i + nToSkip + 1);
osFileInArchive =
CompactFilename(pszFilename + i + nToSkip + 1);
}
else
{
Expand All @@ -549,25 +542,15 @@ char *VSIArchiveFilesystemHandler::SplitFilename(const char *pszFilename,
{
const char lastC = osFileInArchive.back();
if (IsEitherSlash(lastC))
osFileInArchive.pop_back();
osFileInArchive.resize(osFileInArchive.size() - 1);
}

// Messy! Restore the TLS buffer if it has been altered
if (osFilenameCopy != pszFilename)
strcpy(const_cast<char *>(pszFilename),
osFilenameCopy.c_str());

return archiveFilename;
}
CPLFree(archiveFilename);
}
i++;
}

// Messy! Restore the TLS buffer if it has been altered
if (osFilenameCopy != pszFilename)
strcpy(const_cast<char *>(pszFilename), osFilenameCopy.c_str());

return nullptr;
}

Expand Down

0 comments on commit 99dc672

Please sign in to comment.