From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id 7B76D3851C0C; Wed, 17 Jun 2020 19:32:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B76D3851C0C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1592422331; bh=KG3NwpuxkKTP0u0x+gvs8izffI9spSBv7UX8JA+8MDg=; h=From:To:Subject:Date:From; b=VfRJ751DCngy1o+cUFJMPphL1LG51c6YyIz4tUA4ZQMCYL6Q5jcMv6zrf1FQSQxvC dh+/efk4sfMa11yuAcCwSaaEP4WKBV18QJs6tgH3yN6BbTEUpElpjokaF2OPLgwWOS qjKRTjv19ZomgzZslJCKhCBNHO42At3F4LfS9yS8= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Aldy Hernandez To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc/devel/ranger] libstdc++: Handle type-changing path concatenations (PR 94063) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/devel/ranger X-Git-Oldrev: 81fa6d7321dd9b645d86de4a8a6967c603f176e3 X-Git-Newrev: ea182fe63634bb5b7913b3f1b6846e1900c5e0c4 Message-Id: <20200617193211.7B76D3851C0C@sourceware.org> Date: Wed, 17 Jun 2020 19:32:11 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Jun 2020 19:32:11 -0000 https://gcc.gnu.org/g:ea182fe63634bb5b7913b3f1b6846e1900c5e0c4 commit ea182fe63634bb5b7913b3f1b6846e1900c5e0c4 Author: Jonathan Wakely Date: Mon Mar 9 23:22:57 2020 +0000 libstdc++: Handle type-changing path concatenations (PR 94063) The filesystem::path::operator+= and filesystem::path::concat functions operate directly on the native format of the path and so can cause a path to mutate to a completely different type. For Windows combining a filename "x" with a filename ":" produces a root-name "x:". Similarly, a Cygwin root-directory "/" combined with a root-directory and filename "/x" produces a root-name "//x". Before this patch the implemenation didn't support those kind of mutations, assuming that concatenating two filenames would always produce a filename and concatenating with a root-dir would still have a root-dir. This patch fixes it simply by checking for the problem cases and creating a new path by re-parsing the result of the string concatenation. This is slightly suboptimal because the argument has already been parsed if it's a path, but more importantly it doesn't reuse any excess capacity that the path object being modified might already have allocated. That can be fixed later though. PR libstdc++/94063 * src/c++17/fs_path.cc (path::operator+=(const path&)): Add kluge to handle concatenations that change the type of the first component. (path::operator+=(basic_string_view)): Likewise. * testsuite/27_io/filesystem/path/concat/94063.cc: New test. Diff: --- libstdc++-v3/ChangeLog | 8 ++ libstdc++-v3/src/c++17/fs_path.cc | 40 ++++++++ .../27_io/filesystem/path/concat/94063.cc | 111 +++++++++++++++++++++ 3 files changed, 159 insertions(+) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index b3e8b9a25d2..be382dc4e63 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2020-03-09 Jonathan Wakely + + PR libstdc++/94063 + * src/c++17/fs_path.cc (path::operator+=(const path&)): Add kluge to + handle concatenations that change the type of the first component. + (path::operator+=(basic_string_view)): Likewise. + * testsuite/27_io/filesystem/path/concat/94063.cc: New test. + 2020-03-06 Patrick Palka * include/std/ranges (join_view::_Sentinel<_Const>): Befriend diff --git a/libstdc++-v3/src/c++17/fs_path.cc b/libstdc++-v3/src/c++17/fs_path.cc index 73071d8d052..5ff17741f81 100644 --- a/libstdc++-v3/src/c++17/fs_path.cc +++ b/libstdc++-v3/src/c++17/fs_path.cc @@ -852,6 +852,26 @@ path::operator+=(const path& p) return *this; } +#if _GLIBCXX_FILESYSTEM_IS_WINDOWS + if (_M_type() == _Type::_Root_name + || (_M_type() == _Type::_Filename && _M_pathname.size() == 1)) + { + // Handle path("C") += path(":") and path("C:") += path("/x") + // FIXME: do this more efficiently + *this = path(_M_pathname + p._M_pathname); + return *this; + } +#endif +#if SLASHSLASH_IS_ROOTNAME + if (_M_type() == _Type::_Root_dir) + { + // Handle path("/") += path("/x") and path("//") += path("x") + // FIXME: do this more efficiently + *this = path(_M_pathname + p._M_pathname); + return *this; + } +#endif + const auto orig_pathlen = _M_pathname.length(); const auto orig_type = _M_type(); const auto orig_size = _M_cmpts.size(); @@ -1038,6 +1058,26 @@ path::_M_concat(basic_string_view s) return; } +#if _GLIBCXX_FILESYSTEM_IS_WINDOWS + if (_M_type() == _Type::_Root_name + || (_M_type() == _Type::_Filename && _M_pathname.size() == 1)) + { + // Handle path("C") += ":" and path("C:") += "/x" + // FIXME: do this more efficiently + *this = path(_M_pathname + string_type(s)); + return; + } +#endif +#if SLASHSLASH_IS_ROOTNAME + if (_M_type() == _Type::_Root_dir) + { + // Handle path("/") += "/x" and path("//") += "x" + // FIXME: do this more efficiently + *this = path(_M_pathname + string_type(s)); + return; + } +#endif + const auto orig_pathlen = _M_pathname.length(); const auto orig_type = _M_type(); const auto orig_size = _M_cmpts.size(); diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/concat/94063.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/concat/94063.cc new file mode 100644 index 00000000000..9f4c9c0aa08 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/filesystem/path/concat/94063.cc @@ -0,0 +1,111 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target { *-*-*mingw* || *-*-cygwin } } } +// { dg-require-effective-target c++17 } + +#include +#include + +void +test01() +{ + using std::filesystem::path; + path p; + + // PR libstdc++/94063 +#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS + p = L"C"; + p += path(L":"); + VERIFY( p.has_root_name() ); + VERIFY( p.root_name() == p ); + p += path(L"\\"); + VERIFY( p.has_root_name() ); + VERIFY( p.has_root_directory() ); + VERIFY( p.root_name() == L"C:" ); + VERIFY( p.root_directory() == L"\\" ); + + p = L"C"; + p += L':'; + VERIFY( p.has_root_name() ); + VERIFY( p.root_name() == p ); + p += L'\\'; + VERIFY( p.has_root_name() ); + VERIFY( p.has_root_directory() ); + VERIFY( p.root_name() == L"C:" ); + VERIFY( p.root_directory() == L"\\" ); + + p = L"C:"; + p += path(L"/foo"); + VERIFY( p.has_root_name() ); + VERIFY( p.has_root_directory() ); + VERIFY( p.root_name() == L"C:" ); + VERIFY( p.root_directory() == L"/" ); + VERIFY( p.filename() == L"foo" ); + + p = L"C:"; + p += L"/foo"; + VERIFY( p.has_root_name() ); + VERIFY( p.has_root_directory() ); + VERIFY( p.root_name() == L"C:" ); + VERIFY( p.root_directory() == L"/" ); + VERIFY( p.filename() == L"foo" ); + + p = L"C"; + p += path(L":/foo"); + VERIFY( p.has_root_name() ); + VERIFY( p.has_root_directory() ); + VERIFY( p.root_name() == L"C:" ); + VERIFY( p.root_directory() == L"/" ); + VERIFY( p.filename() == L"foo" ); + + p = L"C"; + p += L":/foo"; + VERIFY( p.has_root_name() ); + VERIFY( p.has_root_directory() ); + VERIFY( p.root_name() == L"C:" ); + VERIFY( p.root_directory() == L"/" ); + VERIFY( p.filename() == L"foo" ); +#elif defined __CYGWIN__ + p = "/"; + p += path("/x"); + VERIFY( p.has_root_name() ); + VERIFY( p.root_name() == p ); + + p = "/"; + p += "/x"; + VERIFY( p.has_root_name() ); + VERIFY( p.root_name() == p ); + + p = "/"; + p += path("/"); + VERIFY( !p.has_root_name() ); + VERIFY( p.has_root_directory() ); + + p = "/"; + p += "/"; + VERIFY( !p.has_root_name() ); + VERIFY( p.has_root_directory() ); +#endif +} + +int +main() +{ + test01(); +}