The standard says "If *this and p are the same object, has no effect."
Previously we ended up clearing the path.

        * include/bits/fs_path.h (path::operator=(path&&)): Check for self
        assignment.
        * src/c++17/fs_path.cc (path::operator=(const path&)): Likewise.
        * testsuite/27_io/filesystem/path/assign/copy.cc: Test self
        assignment.

Tested powerpc64le-linux, committed to trunk.

commit 7c941ffdae78a02976e6abde4ac650470a70edf1
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Thu Apr 4 11:36:45 2019 +0100

    Make filesystem::path safe for self assignment
    
    The standard says "If *this and p are the same object, has no effect."
    Previously we ended up clearing the path.
    
            * include/bits/fs_path.h (path::operator=(path&&)): Check for self
            assignment.
            * src/c++17/fs_path.cc (path::operator=(const path&)): Likewise.
            * testsuite/27_io/filesystem/path/assign/copy.cc: Test self
            assignment.

diff --git a/libstdc++-v3/include/bits/fs_path.h 
b/libstdc++-v3/include/bits/fs_path.h
index 96033f68c36..bf7c65c9cad 100644
--- a/libstdc++-v3/include/bits/fs_path.h
+++ b/libstdc++-v3/include/bits/fs_path.h
@@ -877,6 +877,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
   inline path&
   path::operator=(path&& __p) noexcept
   {
+    if (&__p == this) [[__unlikely__]]
+      return *this;
+
     _M_pathname = std::move(__p._M_pathname);
     _M_cmpts = std::move(__p._M_cmpts);
     __p.clear();
diff --git a/libstdc++-v3/src/c++17/fs_path.cc 
b/libstdc++-v3/src/c++17/fs_path.cc
index 268b5621509..605f62cbf81 100644
--- a/libstdc++-v3/src/c++17/fs_path.cc
+++ b/libstdc++-v3/src/c++17/fs_path.cc
@@ -444,6 +444,9 @@ path::_List::reserve(int newcap, bool exact = false)
 path&
 path::operator=(const path& p)
 {
+  if (&p == this) [[__unlikely__]]
+    return *this;
+
   _M_pathname.reserve(p._M_pathname.length());
   _M_cmpts = p._M_cmpts;       // might throw
   _M_pathname = p._M_pathname; // won't throw because we reserved enough space
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/assign/copy.cc 
b/libstdc++-v3/testsuite/27_io/filesystem/path/assign/copy.cc
index 6c24e3ae7b9..775dbffad36 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/path/assign/copy.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/assign/copy.cc
@@ -20,6 +20,7 @@
 
 #include <filesystem>
 #include <testsuite_fs.h>
+#include <testsuite_hooks.h>
 
 using std::filesystem::path;
 using __gnu_test::compare_paths;
@@ -47,9 +48,26 @@ test02()
   }
 }
 
+void
+test03()
+{
+  // self assignment should have no effect
+  const path orig = "foo/bar/baz";
+  path p = orig;
+  const auto ptr1 = p.c_str();
+  const auto ptr2 = p.begin()->c_str();
+  p = std::move(p);
+  __gnu_test::compare_paths(p, orig);
+  p = p;
+  __gnu_test::compare_paths(p, orig);
+  VERIFY( ptr1 == p.c_str() );
+  VERIFY( ptr2 == p.begin()->c_str() );
+}
+
 int
 main()
 {
   test01();
   test02();
+  test03();
 }

Reply via email to