Opened 11 years ago

#6778 new Bugs

directory_iterator and recursive_directory_iterator has inconsistent behavior with STL iterator when assignment operator=() is involved.

Reported by: Chih-Yao Hsieh <josephsieh@…> Owned by: Beman Dawes
Milestone: To Be Determined Component: filesystem
Version: Boost 1.49.0 Severity: Problem
Keywords: directory_iterator, recursive_directory_iterator, assignment operator=() Cc:

Description

For example, in std::vector, the following code shows that *vIterator_1 prints out intVec[1] and *vIteraotr_2 prints out intVec[0]. (The assignment operator=() does not make those two iterators link together...)

std::vector<int> intVec(10);
intVec[1] = 1;
std::vector<int>::iterator vIterator_1 = intVec.begin();

// iterator assignment operator=() now get involved.
std::vector<int>::iterator vIterator_2 = vIterator_1;  
++vIterator_1;  // vIterator_2 does not increment.
std::cout << "*vIterator_1: " << *vIterator_1;
std::cout << "*vIterator_2: " << *vIterator_2;

On the other hand, the following example shows that dirIterator1->path() and dirIterator2->path() both contains '/home/user/test/2.txt' which is not consistent with STL iterator behavior. Since iterators behaves like pointers, after calling vIterator_2 = vIterator_1;, vIterator_2 and vIterator_1 should stays independent. The problem seems to be directory_iterator forgot to implement the copy constructor and assignment operator=().

// assuming that under /home/user/test, there are 1.txt and 2.txt
boost::filesystem::path rootPath("/home/user/test");
boost::filesystem::directory_iterator dirIterator1(rootPath);

// Now assignment operator=() got involved.
boost::filesystem::directory_iterator dirIterator2 = dirIterator1;

// This line causes dirIterator1 and dirIterator2 both got incremented ...
++dirIterator1;  

std::cout << "dirItertor1: " << dirIterator1->path().string() << std::endl;
std::cout << "dirItertor2: " << dirIterator2->path().string() << std::endl;

Note

The symptom occurred in:

boost::filesystem::directory_iterator
boost::filesystem2::directory_iterator
boost::filesystem3::directory_iterator
boost::filesystem::recursive_directory_iterator
boost::filesystem2::recursive_directory_iterator
boost::filesystem3::recursive_directory_iterator

under all platforms.

Change History (0)

Note: See TracTickets for help on using tickets.