diff mbox series

[committed] libstdc++: Fix unnecessary allocations in read_symlink [PR 96484]

Message ID 20200806174915.GA2376686@redhat.com
State New
Headers show
Series [committed] libstdc++: Fix unnecessary allocations in read_symlink [PR 96484] | expand

Commit Message

Jonathan Wakely Aug. 6, 2020, 5:49 p.m. UTC
libstdc++-v3/ChangeLog:

	PR libstdc++/96484
	* src/c++17/fs_ops.cc (fs::read_symlink): Return an error
	immediately for non-symlinks.
	* src/filesystem/ops.cc (fs::read_symlink): Likewise.

Tested x86_64-linux. Committed to trunk.

Backports to follow.
commit 6a13a4e3f29fc4ce5eff96d74ba965c9fdc02184
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Aug 6 18:44:50 2020

    libstdc++: Fix unnecessary allocations in read_symlink [PR 96484]
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/96484
            * src/c++17/fs_ops.cc (fs::read_symlink): Return an error
            immediately for non-symlinks.
            * src/filesystem/ops.cc (fs::read_symlink): Likewise.
diff mbox series

Patch

diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc
index 873f93aacfc..c685b1824f9 100644
--- a/libstdc++-v3/src/c++17/fs_ops.cc
+++ b/libstdc++-v3/src/c++17/fs_ops.cc
@@ -1180,6 +1180,12 @@  fs::path fs::read_symlink(const path& p, error_code& ec)
       ec.assign(errno, std::generic_category());
       return result;
     }
+  else if (!fs::is_symlink(make_file_status(st)))
+    {
+      ec.assign(EINVAL, std::generic_category());
+      return result;
+    }
+
   std::string buf(st.st_size ? st.st_size + 1 : 128, '\0');
   do
     {
diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc
index 29ea9c0ce87..8c8854bf28e 100644
--- a/libstdc++-v3/src/filesystem/ops.cc
+++ b/libstdc++-v3/src/filesystem/ops.cc
@@ -998,6 +998,12 @@  fs::path fs::read_symlink(const path& p [[gnu::unused]], error_code& ec)
       ec.assign(errno, std::generic_category());
       return result;
     }
+  else if (!fs::is_symlink(make_file_status(st)))
+    {
+      ec.assign(EINVAL, std::generic_category());
+      return result;
+    }
+
   std::string buf(st.st_size ? st.st_size + 1 : 128, '\0');
   do
     {