From 39bab48deef84199be9409b9858dfa5b67d6b3e7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 10 Jun 2019 18:07:57 +0200 Subject: [PATCH] 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. --- include/wx/fs_mem.h | 5 ++--- src/common/fs_mem.cpp | 16 +++++----------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/include/wx/fs_mem.h b/include/wx/fs_mem.h index f62d21c2fd..08f879b4d1 100644 --- a/include/wx/fs_mem.h +++ b/include/wx/fs_mem.h @@ -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; }; diff --git a/src/common/fs_mem.cpp b/src/common/fs_mem.cpp index 281cdbbc88..dd74323c36 100644 --- a/src/common/fs_mem.cpp +++ b/src/common/fs_mem.cpp @@ -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();