Fix dereferencing invalid iterator in wxMemoryFSHandlerBase

Don't assume that m_findIter is always valid initially, i.e. after
FindFirst() is called, as this is not the case if the memory FS is
empty, i.e. which no files had been added to it yet.

This also allows to simplify the iteration logic, as we don't need to
check m_findArgument (which, in turn, obviates the need to clear it when
we reach the end of the iteration) and can just use m_findIter directly.

Closes #18416.
This commit is contained in:
Vadim Zeitlin
2019-06-10 18:07:57 +02:00
parent 3ff6647886
commit 39bab48dee
2 changed files with 7 additions and 14 deletions

View File

@@ -64,11 +64,10 @@ protected:
// the file name currently being searched for, i.e. the argument of the
// last FindFirst() call or empty string if FindFirst() hasn't been called
// yet or FindNext() didn't find anything
// yet
wxString m_findArgument;
// iterator into m_Hash used by FindFirst/Next(), possibly m_Hash.end() or
// even invalid (can only be used when m_findArgument is not empty)
// iterator into m_Hash used by FindFirst/Next(), possibly m_Hash.end()
wxMemoryFSHash::const_iterator m_findIter;
};

View File

@@ -147,22 +147,16 @@ wxString wxMemoryFSHandlerBase::FindFirst(const wxString& url, int flags)
wxString wxMemoryFSHandlerBase::FindNext()
{
// m_findArgument is used to indicate that search is in progress, we reset
// it to empty string after iterating over all elements
while ( !m_findArgument.empty() )
while ( m_findIter != m_Hash.end() )
{
// test for the match before (possibly) clearing m_findArgument below
const bool found = m_findIter->first.Matches(m_findArgument);
const wxString& path = m_findIter->first;
// advance m_findIter first as we need to do it anyhow, whether it
// matches or not
const wxMemoryFSHash::const_iterator current = m_findIter;
++m_findIter;
if ( ++m_findIter == m_Hash.end() )
m_findArgument.clear();
if ( found )
return "memory:" + current->first;
if ( path.Matches(m_findArgument) )
return "memory:" + path;
}
return wxString();