Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release/3.8] GPX: make detection of <extensions> element more robust (fixes #8827) #8845

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ogr/ogrsf_frmts/gpx/ogr_gpx.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ class OGRGPXDataSource final : public OGRDataSource

#ifdef HAVE_EXPAT
OGRGPXValidity validity;
int nElementsRead;
char *pszVersion;
XML_Parser oCurrentParser;
int nDataHandlerCounter;
Expand Down
15 changes: 9 additions & 6 deletions ogr/ogrsf_frmts/gpx/ogrgpxdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ OGRGPXDataSource::OGRGPXDataSource()
lastGPXGeomTypeWritten(GPX_NONE), bUseExtensions(false),
pszExtensionsNS(nullptr),
#ifdef HAVE_EXPAT
validity(GPX_VALIDITY_UNKNOWN), nElementsRead(0), pszVersion(nullptr),
validity(GPX_VALIDITY_UNKNOWN), pszVersion(nullptr),
oCurrentParser(nullptr), nDataHandlerCounter(0),
#endif
nLastRteId(-1), nLastTrkId(-1), nLastTrkSegId(-1)
Expand Down Expand Up @@ -224,7 +224,10 @@ void OGRGPXDataSource::startElementValidateCbk(const char *pszNameIn,
if (strcmp(ppszAttr[i], "version") == 0)
{
pszVersion = CPLStrdup(ppszAttr[i + 1]);
break;
}
else if (strcmp(ppszAttr[i], "xmlns:ogr") == 0)
{
bUseExtensions = true;
}
}
}
Expand Down Expand Up @@ -380,7 +383,6 @@ void OGRGPXDataSource::startElementValidateCbk(const char *pszNameIn,
{
bUseExtensions = true;
}
nElementsRead++;
}
m_nDepth++;
}
Expand Down Expand Up @@ -499,7 +501,6 @@ int OGRGPXDataSource::Open(const char *pszFilename, int bUpdateIn)
CPLFree(pszVersion);
pszVersion = nullptr;
bUseExtensions = false;
nElementsRead = 0;

XML_Parser oParser = OGRCreateExpatXMLParser();
oCurrentParser = oParser;
Expand All @@ -517,10 +518,12 @@ int OGRGPXDataSource::Open(const char *pszFilename, int bUpdateIn)
/* It *MUST* be the first element of an XML file */
/* So once we have read the first element, we know if we can */
/* handle the file or not with that driver */
uint64_t nTotalBytesRead = 0;
do
{
nDataHandlerCounter = 0;
nLen = static_cast<unsigned int>(VSIFReadL(aBuf, 1, sizeof(aBuf), fp));
nTotalBytesRead += nLen;
nDone = VSIFEofL(fp);
if (XML_Parse(oParser, aBuf, nLen, nDone) == XML_STATUS_ERROR)
{
Expand Down Expand Up @@ -548,10 +551,10 @@ int OGRGPXDataSource::Open(const char *pszFilename, int bUpdateIn)
{
/* If we have recognized the <gpx> element, now we try */
/* to recognize if they are <extensions> tags */
/* But we stop to look for after an arbitrary number of tags */
/* But we stop to look for after an arbitrary amount of bytes */
if (bUseExtensions)
break;
else if (nElementsRead > 200)
else if (nTotalBytesRead > 1024 * 1024)
break;
}
else
Expand Down
Loading