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:
@@ -64,11 +64,10 @@ protected:
|
|||||||
|
|
||||||
// the file name currently being searched for, i.e. the argument of the
|
// 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
|
// last FindFirst() call or empty string if FindFirst() hasn't been called
|
||||||
// yet or FindNext() didn't find anything
|
// yet
|
||||||
wxString m_findArgument;
|
wxString m_findArgument;
|
||||||
|
|
||||||
// iterator into m_Hash used by FindFirst/Next(), possibly m_Hash.end() or
|
// iterator into m_Hash used by FindFirst/Next(), possibly m_Hash.end()
|
||||||
// even invalid (can only be used when m_findArgument is not empty)
|
|
||||||
wxMemoryFSHash::const_iterator m_findIter;
|
wxMemoryFSHash::const_iterator m_findIter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -147,22 +147,16 @@ wxString wxMemoryFSHandlerBase::FindFirst(const wxString& url, int flags)
|
|||||||
|
|
||||||
wxString wxMemoryFSHandlerBase::FindNext()
|
wxString wxMemoryFSHandlerBase::FindNext()
|
||||||
{
|
{
|
||||||
// m_findArgument is used to indicate that search is in progress, we reset
|
while ( m_findIter != m_Hash.end() )
|
||||||
// it to empty string after iterating over all elements
|
|
||||||
while ( !m_findArgument.empty() )
|
|
||||||
{
|
{
|
||||||
// test for the match before (possibly) clearing m_findArgument below
|
const wxString& path = m_findIter->first;
|
||||||
const bool found = m_findIter->first.Matches(m_findArgument);
|
|
||||||
|
|
||||||
// advance m_findIter first as we need to do it anyhow, whether it
|
// advance m_findIter first as we need to do it anyhow, whether it
|
||||||
// matches or not
|
// matches or not
|
||||||
const wxMemoryFSHash::const_iterator current = m_findIter;
|
++m_findIter;
|
||||||
|
|
||||||
if ( ++m_findIter == m_Hash.end() )
|
if ( path.Matches(m_findArgument) )
|
||||||
m_findArgument.clear();
|
return "memory:" + path;
|
||||||
|
|
||||||
if ( found )
|
|
||||||
return "memory:" + current->first;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return wxString();
|
return wxString();
|
||||||
|
Reference in New Issue
Block a user