https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79283

            Bug ID: 79283
           Summary: read_symlink fails with /proc symlinks
           Product: gcc
           Version: 6.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: achille.fouilleul+gcc at gadz dot org
  Target Milestone: ---

Consider the following program:

#include <cstdio>
#include <exception>
#include <experimental/filesystem>
//#include <boost/filesystem.hpp>

namespace fs = std::experimental::filesystem;
//namespace fs = boost::filesystem;

int main()
{
        try {
                fs::path symlinkPath("/proc/self");
                auto targetPath = fs::read_symlink(symlinkPath);
                auto s = targetPath.native();
                printf("%s\n", s.c_str());
                return 0;
        }
        catch (const std::exception& e)
        {
                fprintf(stderr, "%s\n", e.what());
                return 1;
        }
}

On Linux it fails with the following message:

filesystem error: read_symlink: Invalid argument [/proc/self]

When run with strace I get this:

...
lstat("/proc/self", {st_mode=S_IFLNK|0777, st_size=0, ...}) = 0
readlink("/proc/self", 0x7ffe21d33700, 0) = -1 EINVAL (Invalid argument)
...

Indeed in libstdc++-v3/src/filesystem/ops.cc the readlink call is preceeded by
a lstat call to determine the size of the buffer. This does not work with
Linux's /proc (or /sys for that matter), since the st_size of pseudo-filesystem
links is always zero.

Boost's read_symlink follows a different approach: it starts with a 64-byte
buffer and double its size until readlink succeeds.

Reply via email to