public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Fix brainwrong in path::_S_convert(T) [PR102743]
Date: Thu, 14 Oct 2021 15:18:10 +0100	[thread overview]
Message-ID: <YWg8ImM5cT5K0yM1@redhat.com> (raw)
In-Reply-To: <YWc2cp/MJDN0/LDa@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2278 bytes --]

On 13/10/21 20:41 +0100, Jonathan Wakely wrote:
>Adjust the __detail::__effective_range overloads so they always return a
>string or string view using std::char_traits, because we don't care
>about the traits of an incoming string.
>
>Use std::contiguous_iterator in the __effective_range(const Source&)
>overload, to allow returning a basic_string_view in more cases. For the
>non-contiguous cases in both __effective_range and __string_from_range,
>return a std::string instead of std::u8string when the value type of the
>range is char8_t.  These changes avoid unnecessary basic_string
>temporaries.
>
>Also simplify __string_from_range(Iter, Iter) to not need
>std::__to_address for the contiguous case.
>
>Combine the _S_convert(string_type) and _S_convert(const T&) overloads
>into a single _S_convert(T) function which also avoids the dangling
>view problem of PR 102592 (should that recur somehow).
>
>libstdc++-v3/ChangeLog:
>
>	* include/bits/fs_path.h (__detail::__is_contiguous): New
>	variable template to identify contiguous iterators.
>	(__detail::__unified_char8_t): New alias template to decide when
>	to treat char8_t as char without encoding conversion.
>	(__detail::__effective_range(const basic_string<C,T>&)): Use
>	std::char_traits<C> for returned string view.
>	(__detail::__effective_range(const basic_string_view<C,T>&)):
>	Likewise.
>	(__detail::__effective_range(const Source&)): Use
>	__is_contiguous to detect mode cases of contiguous iterators.
>	Use __unified_char8_t to return a std::string instead of
>	std::u8string.
>
>Tested powerpc64le-linux. Committed to trunk.


>     template<typename _Tp>
>       static auto
>-      _S_convert(const _Tp& __str)
>+      _S_convert(_Tp __str)
>       {
>-	if constexpr (is_same_v<_Tp, string_type>)
>-	  return __str;
>-	else if constexpr (is_same_v<_Tp, basic_string_view<value_type>>)
>-	  return __str;
>-	else if constexpr (is_same_v<typename _Tp::value_type, value_type>)
>-	  return basic_string_view<value_type>(__str.data(), __str.size());
>+	if constexpr (is_same_v<_Tp, typename _Tp::value_type>)
>+	  return __str; // No conversion needed.

Yikes, this condition is wrong! Causing PR 102743 on Windows.

Fixed by the attached patch, tested x86_64-linux and x86_64-mingw32,
pushed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 1566 bytes --]

commit 5e3f88838994b67580594c4679c839fff7cdeba0
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Oct 14 13:20:57 2021

    libstdc++: Fix brainwrong in path::_S_convert(T) [PR102743]
    
    This function was supposed to check whether the parameter's value type
    is the same as path::value_type, and therefore needs no conversion.
    Instead it checks whether the parameter is the same as its own value
    type, which is never true. This means we incorrectly return a string
    view for the case where T is path::string_type, instead of just
    returning the string itself. The only place that happens is
    path::_S_convert_loc for Windows, where we call _S_convert with a
    std::wstring rvalue.
    
    This fixes the condition in _S_convert(T).
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/102743
            * include/bits/fs_path.h (path::_S_convert(T)): Fix condition
            for returning the same string unchanged.

diff --git a/libstdc++-v3/include/bits/fs_path.h b/libstdc++-v3/include/bits/fs_path.h
index c51bfa3095a..a63e4b9ab07 100644
--- a/libstdc++-v3/include/bits/fs_path.h
+++ b/libstdc++-v3/include/bits/fs_path.h
@@ -625,7 +625,7 @@ namespace __detail
       static auto
       _S_convert(_Tp __str)
       {
-	if constexpr (is_same_v<_Tp, typename _Tp::value_type>)
+	if constexpr (is_same_v<typename _Tp::value_type, value_type>)
 	  return __str; // No conversion needed.
 #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
 	else if constexpr (is_same_v<_Tp, std::u8string>)

      parent reply	other threads:[~2021-10-14 14:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-13 19:41 [committed] libstdc++: Refactor filesystem::path encoding conversions Jonathan Wakely
2021-10-13 20:19 ` Jonathan Wakely
2021-10-13 22:57   ` [committed] libstdc++: Fix regression in memory use when constructing paths Jonathan Wakely
2021-10-14 14:18 ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YWg8ImM5cT5K0yM1@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).