summaryrefslogtreecommitdiff
path: root/libstdc++-v3/src/filesystem/ops.cc
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2017-10-25 13:42:58 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2017-10-25 13:42:58 +0100
commit220645d0193c21b4662b2800bc5d2de493832fd7 (patch)
tree36a7d1b6e5939bc418cc29922e074780828a39a1 /libstdc++-v3/src/filesystem/ops.cc
parenteeb517d3e7fd3717f4fa7cc8a1e6d4499013fee3 (diff)
downloadgcc-220645d0193c21b4662b2800bc5d2de493832fd7.tar.gz
PR libstdc++/79283 fix filesystem::read_symlink for /proc
PR libstdc++/79283 * src/filesystem/ops.cc (read_symlink): Handle st_size being zero. * src/filesystem/std-ops.cc (read_symlink): Likewise. (do_copy_file) [!NEED_DO_COPY_FILE]: Avoid multiple definitions. From-SVN: r254076
Diffstat (limited to 'libstdc++-v3/src/filesystem/ops.cc')
-rw-r--r--libstdc++-v3/src/filesystem/ops.cc37
1 files changed, 28 insertions, 9 deletions
diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc
index 61d9c89e616..1ec8883fde9 100644
--- a/libstdc++-v3/src/filesystem/ops.cc
+++ b/libstdc++-v3/src/filesystem/ops.cc
@@ -962,26 +962,45 @@ fs::read_symlink(const path& p)
fs::path fs::read_symlink(const path& p, error_code& ec)
{
+ path result;
#ifdef _GLIBCXX_HAVE_SYS_STAT_H
stat_type st;
if (::lstat(p.c_str(), &st))
{
ec.assign(errno, std::generic_category());
- return {};
+ return result;
}
- std::string buf(st.st_size, '\0');
- ssize_t len = ::readlink(p.c_str(), &buf.front(), buf.size());
- if (len == -1)
+ std::string buf(st.st_size ? st.st_size + 1 : 128, '\0');
+ do
{
- ec.assign(errno, std::generic_category());
- return {};
+ ssize_t len = ::readlink(p.c_str(), buf.data(), buf.size());
+ if (len == -1)
+ {
+ ec.assign(errno, std::generic_category());
+ return result;
+ }
+ else if (len == (ssize_t)buf.size())
+ {
+ if (buf.size() > 4096)
+ {
+ ec.assign(ENAMETOOLONG, std::generic_category());
+ return result;
+ }
+ buf.resize(buf.size() * 2);
+ }
+ else
+ {
+ buf.resize(len);
+ result.assign(buf);
+ ec.clear();
+ break;
+ }
}
- ec.clear();
- return path{buf.data(), buf.data()+len};
+ while (true);
#else
ec = std::make_error_code(std::errc::not_supported);
- return {};
#endif
+ return result;
}