Re: [Boost-bugs] [Boost C++ Libraries] #5652: recursive_directory_iterator fails on cyclic symbolic links

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #5652: recursive_directory_iterator fails on cyclic symbolic links
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-10-15 19:32:32


#5652: recursive_directory_iterator fails on cyclic symbolic links
----------------------------------------------+-----------------------------
  Reporter: Geurt Vos <geurt.vos@…> | Owner: bemandawes
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: filesystem
   Version: Boost 1.46.1 | Severity: Problem
Resolution: | Keywords:
----------------------------------------------+-----------------------------

Comment (by Daniel Aarno <macbishop@…>):

 I can confirm this issue and there is an easy fix. To reproduce and test
 the issue the following simple program that counts the files (recursively)
 in a directory can be used on a directory containing the "symlink to self"
 as described by Geurt.

 #include <string>
 #include <iostream>

 #include <boost/filesystem.hpp>

 int
 ftw(const std::string &root) {
      using namespace boost::filesystem;
      typedef recursive_directory_iterator walker;
      int count = 0;
      for (walker w(root); w != walker(); ++w)
           ++count;

      return count;
 }

 int
 main (int argc, const char **argv) {
      int count = ftw(argv[1]);
      std::cout << count << std::endl;
 }

 The problem is in the increment method in recur_dir_itr_imp. It checks the
 file with is_directory, which will end up in a "stat" call on the file. Of
 course if the file is a symlink to itself stat will try to follow the link
 indefinitely (since it will try to deference the symlink until a real file
 is found). This causes stat to generate an ELOOP error and the exception
 is thrown.

 The solution is to not check if the file is a directory if it is a symlink
 - unless of course we are following symlinks during recursion, in which
 case the exception should occur. We can test this by adding
 symlink_option::recurse as an option to the walker constructor in the
 above example.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5652#comment:1>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:07 UTC