infinite loop in std::filesystem::remove_all when removing non-empty directory with NUL byte in name

Bug #1792570 reported by Frank Benkstein
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gcc-8 (Ubuntu)
New
Undecided
Unassigned

Bug Description

Please have a look at this test program:

#include <string>
#include <iostream>
#include <fstream>

#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif

int main()
{
    std::string s{'t', 'e', 0, 's', 't'};
    fs::path p(s);
    fs::create_directory(p);
    std::ofstream(p / "foo");
    fs::remove_all(p);
}

You can compile it with the following command:

  g++-8 -std=c++17 -o test test.cc -lstdc++fs

When you run it (without any arguments), it will try to create a directory "te", create a file "foo" in it and then try to remove said directory and all files in it.

When I run it on my local machine it crashes with the following error message:

# ./test
terminate called after throwing an instance of 'std::experimental::filesystem::v1::__cxx11::filesystem_error'
  what(): filesystem error: cannot remove all: Directory not empty [te
[1] 8180 abort ./test

Through strace (log attached) I can tell that the crash is probably triggered by running out file descriptors.

This bug also happens when compiling with gcc-7 or clang so I guess it's really a bug in the std::filesystem library, not gcc itself.

ProblemType: Bug
DistroRelease: Ubuntu 18.04
Package: libstdc++-8-dev 8-20180414-1ubuntu2
ProcVersionSignature: Ubuntu 4.15.0-34.37-generic 4.15.18
Uname: Linux 4.15.0-34-generic x86_64
ApportVersion: 2.20.9-0ubuntu7.3
Architecture: amd64
CurrentDesktop: ubuntu:GNOME
Date: Fri Sep 14 13:56:03 2018
InstallationDate: Installed on 2018-03-05 (192 days ago)
InstallationMedia: Ubuntu 18.04 LTS "Bionic Beaver" - Alpha amd64 (20180305)
SourcePackage: gcc-8
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
Frank Benkstein (frank-launchpad) wrote :
Revision history for this message
Frank Benkstein (frank-launchpad) wrote :
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.