Opened 7 years ago

Last modified 7 years ago

#11910 new Bugs

recursive_directory_iterator doesn't set end iterator properly when exception is thrown in increment()

Reported by: David Raphael <draphael@…> Owned by: Beman Dawes
Milestone: To Be Determined Component: filesystem
Version: Boost 1.60.0 Severity: Problem
Keywords: Cc:

Description

in operations.hpp the increment function looks like this:

    void increment()
    { 
      BOOST_ASSERT_MSG(m_imp.get(),
        "increment of end recursive_directory_iterator");
      m_imp->increment(0);
      if (m_imp->m_stack.empty())
        m_imp.reset(); // done, so make end iterator
    }

The problem occurs when you are incrementing using the ++ operator. If there is an exception, the stack is never checked to see if it is empty because the m_imp->increment() throws an exception. This leaves the recursive iterator in a bad state.

I fixed it by doing this:

    void increment()
    { 
      BOOST_ASSERT_MSG(m_imp.get(),
        "increment of end recursive_directory_iterator");
      try
      {
        m_imp->increment(0);
      }
      catch(filesystem_error e)
      {
        if (m_imp->m_stack.empty())
          m_imp.reset(); // done, so make end iterator
        throw(e);
      }
      if (m_imp->m_stack.empty())
        m_imp.reset(); // done, so make end iterator
    }

but I'm not sure that is the right way to address this issue.

Change History (1)

comment:1 by David Raphael <draphael@…>, 7 years ago

Summary: recursive_directory_iterator doesn't set end_iterator properly when exception is thrown in increment()recursive_directory_iterator doesn't set end iterator properly when exception is thrown in increment()
Note: See TracTickets for help on using tickets.