[gcc r8-10390] libstdc++: Fix unnecessary allocations in read_symlink [PR 96484]

Jonathan Wakely redi@gcc.gnu.org
Thu Aug 6 20:25:27 GMT 2020


https://gcc.gnu.org/g:8b692f8b4c0e47bc8e11d9c3ab83049f68b2edbc

commit r8-10390-g8b692f8b4c0e47bc8e11d9c3ab83049f68b2edbc
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Aug 6 18:44:50 2020 +0100

    libstdc++: Fix unnecessary allocations in read_symlink [PR 96484]
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/96484
            * src/filesystem/ops.cc (fs::read_symlink): Return an error
            immediately for non-symlinks.
            * src/filesystem/std-ops.cc (fs::read_symlink): Likewise.
    
    (cherry picked from commit 6a13a4e3f29fc4ce5eff96d74ba965c9fdc02184)

Diff:
---
 libstdc++-v3/src/filesystem/ops.cc     | 6 ++++++
 libstdc++-v3/src/filesystem/std-ops.cc | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc
index f04b4c5fc17..4a14251cfe8 100644
--- a/libstdc++-v3/src/filesystem/ops.cc
+++ b/libstdc++-v3/src/filesystem/ops.cc
@@ -987,6 +987,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/std-ops.cc b/libstdc++-v3/src/filesystem/std-ops.cc
index 6d0bec48d44..8f3191c1a71 100644
--- a/libstdc++-v3/src/filesystem/std-ops.cc
+++ b/libstdc++-v3/src/filesystem/std-ops.cc
@@ -1248,6 +1248,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
     {


More information about the Libstdc++-cvs mailing list