Opened 19 years ago
Closed 17 years ago
#257 closed Bugs (Works For Me)
filesystem::directory_iterator returns incorrect listing
| Reported by: | s-a-t-a-n | Owned by: | beman_dawes |
|---|---|---|---|
| Milestone: | Component: | None | |
| Version: | None | Severity: | |
| Keywords: | Cc: |
Description
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 &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 &p, it would cause a seg-fault.
Change History (9)
comment:2 by , 19 years ago
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
comment:4 by , 19 years ago
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<boost::filesystem::detail::dir_itr_imp>(boost::filesystem::detail::dir_itr_imp*) (checked_delete.hpp:36) ==22798== by 0x3C0333F4: boost::checked_deleter<boost::filesystem::detail::dir_itr_imp>::operator() (boost::filesystem::detail::dir_itr_imp*) const (checked_delete.hpp:54) ==22798== by 0x3C0335B7: boost::detail::sp_counted_base_impl<boost::filesystem::detail::dir_itr_imp*, boost::checked_deleter<boost::filesystem::detail::dir_itr_imp> >::dispose() (shared_count.hpp:265)
comment:5 by , 19 years ago
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& p = *it++;
comment:6 by , 19 years ago
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.
comment:7 by , 19 years ago
Logged In: YES
user_id=321498
There are two parts.
First, using
const path& 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...
comment:8 by , 17 years ago
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.
comment:9 by , 17 years ago
| Status: | assigned → closed |
|---|
Note:
See TracTickets
for help on using tickets.
