Boost C++ Libraries: Ticket #257: filesystem::directory_iterator returns incorrect listing https://svn.boost.org/trac10/ticket/257 <pre class="wiki">directory_iterator doesn't work correctly under debian linux (sarge/testing). Returns an invalid iterator value, which can cause a segmentation-fault, and drops the last item from the resulting listing. path dir = "my_dir"; if ( exists(dir) || !is_directory(dir) ) return; directory_iterator itr(dir), end; while ( itr != end ) { const path &amp;p = *itr++; //-- do something with it } When I had a directory with only one file, no actual filenames were returned. When calling methods such as leaf() on const path &amp;p, it would cause a seg-fault. </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/257 Trac 1.4.3 Vladimir Prus Mon, 19 Apr 2004 14:10:38 GMT <link>https://svn.boost.org/trac10/ticket/257#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/257#comment:1</guid> <description> <pre class="wiki">Logged In: YES user_id=321498 Jonathan, could you provide complete program which reproduces a behaviour? I'm on Debian box so I could have tried your case, but only if I can easily get it. </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>s-a-t-a-n</dc:creator> <pubDate>Tue, 20 Apr 2004 02:35:40 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/257#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/257#comment:2</guid> <description> <pre class="wiki">Logged In: YES user_id=390595 Sample runs: when directory doesn't exist, program terminates properly with message. when directory exists, but is empty, program terminates gracefully with no output. when directory exists, and contains a single file, program terminates with segmentation fault. in my_directory, to create a single file, I did: touch DUMMY Regards, Jonathan </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Tue, 20 Apr 2004 12:23:15 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/257#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/257#comment:3</guid> <description> <pre class="wiki">Logged In: YES user_id=321498 Confirmed. This crashes for me too. </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Tue, 20 Apr 2004 12:39:50 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/257#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/257#comment:4</guid> <description> <pre class="wiki">Logged In: YES user_id=321498 valgrind reports: ==22798== Invalid read of size 4 ==22798== at 0x3C0C83BA: std::string::_M_data() const (basic_string.h:249) ==22798== by 0x3C0C844C: std::string::size() const (basic_string.h:408) ==22798== by 0x3C0C9A70: std::string::empty() const (basic_string.h:432) ==22798== by 0x804983C: main (dir_it.cpp:20) ==22798== Address 0x3C284ED4 is 0 bytes inside a block of size 8 free'd ==22798== at 0x3C021A3C: operator delete(void*) (vg_replace_malloc.c:129) ==22798== by 0x3C033439: void boost::checked_delete&lt;boost::filesystem::detail::dir_itr_imp&gt;(boost::filesystem::detail::dir_itr_imp*) (checked_delete.hpp:36) ==22798== by 0x3C0333F4: boost::checked_deleter&lt;boost::filesystem::detail::dir_itr_imp&gt;::operator() (boost::filesystem::detail::dir_itr_imp*) const (checked_delete.hpp:54) ==22798== by 0x3C0335B7: boost::detail::sp_counted_base_impl&lt;boost::filesystem::detail::dir_itr_imp*, boost::checked_deleter&lt;boost::filesystem::detail::dir_itr_imp&gt; &gt;::dispose() (shared_count.hpp:265) </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Tue, 20 Apr 2004 12:44:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/257#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/257#comment:5</guid> <description> <pre class="wiki">Logged In: YES user_id=321498 I think it's a problem with your code. directory_iterator is a single_traversal iterator. It means that if you bind a reference to result of operator*, then this value is no longer valid after you increment the interator. I think you should use const path p = *it++; instead of const path&amp; p = *it++; </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>s-a-t-a-n</dc:creator> <pubDate>Tue, 20 Apr 2004 20:18:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/257#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/257#comment:6</guid> <description> <pre class="wiki">Logged In: YES user_id=390595 No, it's definitely a bug =) Besides, code works flawlessly on win32. First, some more tests. Two files: (DUMMY, DUMMY2) Directory Entry: DUMMY2 Segmentation Fault Three files: (DUMMY, DUMMY2, DUMMY3) Directory Entry: DUMMY2 Directory Entry: DUMMY3 Segmentation Fault Change to code as you suggested to make my path variable a copy rather than a reference: Three files: (DUMMY, DUMMY2, DUMMY3) Directory Entry: DUMMY2 Directory Entry: DUMMY3 Directory Entry: DUMMY3 It is _definitely_ a bug in the iterator code. </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Wed, 21 Apr 2004 05:53:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/257#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/257#comment:7</guid> <description> <pre class="wiki">Logged In: YES user_id=321498 There are two parts. First, using const path&amp; p = *intr++; is still a bug on your part. The reference returned by *intr is invalidated when the iterator is incremented. The fact that it works on win32 is just a luck -- what happens when memory is freed is platform-dependent. On one system you still will be able to access the memory for some time, on another you get segfault in no time. Second, the const path p = *itr++; really does not work correctly when there are two files. I get: Directory Entry: 'foo' Directory Entry: 'foo' Gotta look into this... </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>beman_dawes</dc:creator> <pubDate>Mon, 28 Nov 2005 19:39:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/257#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/257#comment:8</guid> <description> <pre class="wiki">Logged In: YES user_id=51042 It isn't clear if this was ever resolved. I've just now added ab operations_test case to cover the problem Vladimir reports. Passes on Windows; I'll wait a couple of days to see if it also passes on other platforms and then close the issue. </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>beman_dawes</dc:creator> <pubDate>Wed, 30 Nov 2005 22:50:03 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/257#comment:9 https://svn.boost.org/trac10/ticket/257#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> </ul> Ticket