[gcc/devel/omp/gcc-9] PR libstdc++/90299 make filesystem::absolute overloads consistent

Tobias Burnus burnus@gcc.gnu.org
Thu Mar 5 13:49:00 GMT 2020


https://gcc.gnu.org/g:c679489ec63ce6660498c2dde3891cfc585d810f

commit c679489ec63ce6660498c2dde3891cfc585d810f
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri May 17 00:00:26 2019 +0100

    PR libstdc++/90299 make filesystem::absolute overloads consistent
    
    In this implementation it is an error to pass the empty path to absolute,
    because the empty path doesn't represent any file in the filesystem so
    the function cannot meet its postcondition.
    
    Currently the absolute(const path&, error_code&) overload reports an
    error for the empty path, but using errc::no_such_file_or_directory, and
    the other overload does not report an error. This patch makes them
    consistntly report an errc::invalid_argument error for the empty path.
    
    Backport from mainline
    2019-05-04  Jonathan Wakely  <jwakely@redhat.com>
    
    	PR libstdc++/90299
    	* src/c++17/fs_ops.cc (absolute(const path&)): Report an error if the
    	argument is an empty path.
    	(absolute(const path&, error_code&)): Use invalid_argument as error
    	code instead of no_such_file_or_directory.
    	* testsuite/27_io/filesystem/operations/absolute.cc: Check handling
    	of non-existent paths and empty paths with both overloads of absolute.
    
    Backport from mainline
    2019-05-16  Jonathan Wakely  <jwakely@redhat.com>
    
    	* src/c++17/fs_ops.cc (absolute(const path&, error_code&))
    	[_GLIBCXX_FILESYSTEM_IS_WINDOWS]: Remove bogus assertion.
    
    From-SVN: r271301

Diff:
---
 libstdc++-v3/ChangeLog                             | 19 +++++++++++++++
 libstdc++-v3/src/c++17/fs_ops.cc                   |  9 +++----
 .../27_io/filesystem/operations/absolute.cc        | 28 ++++++++++++++++++++++
 3 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index a658eb2..0868a6f 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,22 @@
+2019-05-16  Jonathan Wakely  <jwakely@redhat.com>
+
+	Backport from mainline
+	2019-05-16  Jonathan Wakely  <jwakely@redhat.com>
+
+	* src/c++17/fs_ops.cc (absolute(const path&, error_code&))
+	[_GLIBCXX_FILESYSTEM_IS_WINDOWS]: Remove bogus assertion.
+
+	Backport from mainline
+	2019-05-04  Jonathan Wakely  <jwakely@redhat.com>
+
+	PR libstdc++/90299
+	* src/c++17/fs_ops.cc (absolute(const path&)): Report an error if the
+	argument is an empty path.
+	(absolute(const path&, error_code&)): Use invalid_argument as error
+	code instead of no_such_file_or_directory.
+	* testsuite/27_io/filesystem/operations/absolute.cc: Check handling
+	of non-existent paths and empty paths with both overloads of absolute.
+
 2019-05-15  Jonathan Wakely  <jwakely@redhat.com>
 
 	Backport from mainline
diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc
index 5ca5238..274ee7f 100644
--- a/libstdc++-v3/src/c++17/fs_ops.cc
+++ b/libstdc++-v3/src/c++17/fs_ops.cc
@@ -72,6 +72,9 @@ fs::absolute(const path& p)
 					     ec));
   return ret;
 #else
+  if (p.empty())
+    _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot make absolute path", p,
+	  make_error_code(std::errc::invalid_argument)));
   return current_path() / p;
 #endif
 }
@@ -82,7 +85,7 @@ fs::absolute(const path& p, error_code& ec)
   path ret;
   if (p.empty())
     {
-      ec = make_error_code(std::errc::no_such_file_or_directory);
+      ec = make_error_code(std::errc::invalid_argument);
       return ret;
     }
   ec.clear();
@@ -93,6 +96,7 @@ fs::absolute(const path& p, error_code& ec)
     }
 
 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
+  // s must remain null-terminated
   wstring_view s = p.native();
 
   if (p.has_root_directory()) // implies !p.has_root_name()
@@ -105,9 +109,6 @@ fs::absolute(const path& p, error_code& ec)
       s.remove_prefix(std::min(s.length(), pos) - 1);
     }
 
-  // s must be null-terminated
-  __glibcxx_assert(!s.empty() && s.back() == 0);
-
   uint32_t len = 1024;
   wstring buf;
   do
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/absolute.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/absolute.cc
index 45f66ac..156e68a 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/operations/absolute.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/absolute.cc
@@ -67,9 +67,37 @@ test02()
 #endif
 }
 
+void
+test03()
+{
+  // PR libstdc++/90299
+  const path p = __gnu_test::nonexistent_path();
+  std::error_code ec;
+  const path pabs = absolute(p, ec);
+  VERIFY( !ec );
+  VERIFY( pabs.is_absolute() );
+
+  const path pabs2 = absolute(p);
+  VERIFY( pabs2 == pabs );
+
+  const path eabs = absolute(path{}, ec);
+  VERIFY( ec == std::errc::invalid_argument );
+  VERIFY( eabs.empty() );
+
+  try {
+    absolute(path{});
+    VERIFY( false );
+  } catch (const std::filesystem::filesystem_error& e) {
+    VERIFY( e.code() == std::errc::invalid_argument );
+    VERIFY( e.path1().empty() );
+    VERIFY( e.path2().empty() );
+  }
+}
+
 int
 main()
 {
   test01();
   test02();
+  test03();
 }



More information about the Libstdc++-cvs mailing list