[PATCH] Fix concatenation bug in filesystem::path

Jonathan Wakely jwakely@redhat.com
Fri Jan 4 11:43:00 GMT 2019


When erasing a trailing empty filename component, the output iterator
was not decremented, causing the next component to be created at the
wrong position.

	* src/filesystem/std-path.cc (path::operator+=(const path&)): Fix
	incorrect treatment of empty filename after trailing slash.
	* testsuite/27_io/filesystem/path/concat/path.cc: Test problem case.

Tested x86_64-linux, committed to trunk.


-------------- next part --------------
commit cb0b6ed6f18823971bbd2a555849278653b2aef0
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Jan 4 11:35:03 2019 +0000

    Fix concatenation bug in filesystem::path
    
    When erasing a trailing empty filename component, the output iterator
    was not decremented, causing the next component to be created at the
    wrong position.
    
            * src/filesystem/std-path.cc (path::operator+=(const path&)): Fix
            incorrect treatment of empty filename after trailing slash.
            * testsuite/27_io/filesystem/path/concat/path.cc: Test problem case.

diff --git a/libstdc++-v3/src/filesystem/std-path.cc b/libstdc++-v3/src/filesystem/std-path.cc
index bf6f37711eb..b7315ad1686 100644
--- a/libstdc++-v3/src/filesystem/std-path.cc
+++ b/libstdc++-v3/src/filesystem/std-path.cc
@@ -945,7 +945,7 @@ path::operator+=(const path& p)
       else if (orig_filenamelen == 0 && it != last)
 	{
 	  // Remove empty filename at end of original path.
-	  _M_cmpts.erase(std::prev(output));
+	  _M_cmpts.erase(--output);
 	}
 
       if (it != last && it->_M_type() == _Type::_Root_name)
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/concat/path.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/concat/path.cc
index b653219a7f7..e2a14bd8fcc 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/path/concat/path.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/concat/path.cc
@@ -59,9 +59,18 @@ test02()
   }
 }
 
+void
+test03()
+{
+  path p = "a/";
+  p += path("/b");
+  compare_paths(p, "a//b");
+}
+
 int
 main()
 {
   test01();
   test02();
+  test03();
 }


More information about the Libstdc++ mailing list