libstdc++: Fix unnecessary allocations in read_symlink [PR 96484]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 6 Aug 2020 17:44:50 +0000 (18:44 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 6 Aug 2020 17:44:50 +0000 (18:44 +0100)
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.

libstdc++-v3/src/c++17/fs_ops.cc
libstdc++-v3/src/filesystem/ops.cc

index 873f93aacfc4cf5eebd3ec02df3cd0d113a0ad1a..c685b1824f9d67403fd332b5cfb7fc09874d0d01 100644 (file)
@@ -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
     {
index 29ea9c0ce87536655bf66d614f7552d50982cd4a..8c8854bf28e8958d9f8c8dc4d1b657146dafca2d 100644 (file)
@@ -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
     {