Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

directory_iterator runs into segfault

Tags:

This is my code:

#include <iostream>
#include <filesystem>

int main(int argc, char *argv[]) {
    auto iter = std::filesystem::directory_iterator("foo");
    for (auto &entry : iter) {
        std::cout << entry.path();
    }
}

When I run it and the directory foo exists, I get a SIGSEGV. So I started gdb:

(gdb) run
Starting program: /home/krausefx/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x0000555555556a87 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
    this=0x23) at /usr/include/c++/8/bits/stl_vector.h:567
567     std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
(gdb) backtrace
#0  0x0000555555556a87 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
    this=0x23) at /usr/include/c++/8/bits/stl_vector.h:567
#1  0x00005555555566aa in std::filesystem::__cxx11::path::~path (this=0x3) at /usr/include/c++/8/bits/fs_path.h:208
#2  0x0000555555557ebe in std::filesystem::__cxx11::path::_Cmpt::~_Cmpt (this=<incomplete type>) at /usr/include/c++/8/bits/fs_path.h:643
#3  0x0000555555557ed9 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt> (__pointer=0x3) at /usr/include/c++/8/bits/stl_construct.h:98
#4  0x0000555555557ced in std::_Destroy_aux<false>::__destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0)
    at /usr/include/c++/8/bits/stl_construct.h:108
#5  0x00005555555576de in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0)
    at /usr/include/c++/8/bits/stl_construct.h:137
#6  0x0000555555556fb9 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*, std::filesystem::__cxx11::path::_Cmpt> (__first=0x3, __last=0x0)
    at /usr/include/c++/8/bits/stl_construct.h:206
#7  0x0000555555556a9d in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
    this=0x7fffffffdcf0) at /usr/include/c++/8/bits/stl_vector.h:567
#8  0x00005555555566aa in std::filesystem::__cxx11::path::~path (this=0x7fffffffdcd0) at /usr/include/c++/8/bits/fs_path.h:208
#9  0x000055555555630d in main (argc=32767, argv=0x7ffff7fadf40 <std::wcout>) at test.cpp:5
(gdb) p this
$1 = (vector * const) 0x23

So apparently, when initializing the directory_iterator, the destructor of std::filesystem::path gets called for some reason, and somewhere in there, the destuctor of std::vector is called on a this value of 0x23, which obviously is a bad thing and leads to a SIGSEGV.

What's happening here? Am I doing something wrong? Is this a compiler bug (compiler is g++ 8.3.0)?

like image 301
flyx Avatar asked Jul 11 '19 11:07

flyx


1 Answers

I checked directory_iterator works fine using GCC 8 under Ubuntu.

Be sure to add the linker flag -lstdc++fs when compiling.

If you don't compilation ends successful but, at least in my system, I get a segfault as you do when it starts iterating.

like image 176
mma Avatar answered Sep 30 '22 21:09

mma