From: Jonathan Wakely Date: Fri, 1 Dec 2017 16:10:31 +0000 (+0000) Subject: Fix narrowing conversions in string_view types X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ad8fda29e6274dd28aa11d15e98cc777f56ffb37;p=gcc.git Fix narrowing conversions in string_view types * include/experimental/string_view (basic_string_view::_S_compare): Use value-init so narrowing conversions are not ill-formed. * include/std/string_view (basic_string_view::_S_compare): Likewise. From-SVN: r255321 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 14b65e3575e..601bc8a065d 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,9 @@ 2017-12-01 Jonathan Wakely + * include/experimental/string_view (basic_string_view::_S_compare): + Use value-init so narrowing conversions are not ill-formed. + * include/std/string_view (basic_string_view::_S_compare): Likewise. + * include/bits/basic_string.h (operator""s): Add pragmas to disable -Wliteral-suffix warnings. * include/experimental/string_view (operator""sv): Likewise. diff --git a/libstdc++-v3/include/experimental/string_view b/libstdc++-v3/include/experimental/string_view index 96d1f58f8e9..ef171ecc025 100644 --- a/libstdc++-v3/include/experimental/string_view +++ b/libstdc++-v3/include/experimental/string_view @@ -422,11 +422,11 @@ inline namespace fundamentals_v1 static constexpr int _S_compare(size_type __n1, size_type __n2) noexcept { - return difference_type{__n1 - __n2} > std::numeric_limits::max() + return difference_type(__n1 - __n2) > std::numeric_limits::max() ? std::numeric_limits::max() - : difference_type{__n1 - __n2} < std::numeric_limits::min() + : difference_type(__n1 - __n2) < std::numeric_limits::min() ? std::numeric_limits::min() - : static_cast(difference_type{__n1 - __n2}); + : static_cast(difference_type(__n1 - __n2)); } size_t _M_len; diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index 1266a07d04f..3b2901ab3c6 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -408,7 +408,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static constexpr int _S_compare(size_type __n1, size_type __n2) noexcept { - const difference_type __diff{__n1 - __n2}; + const difference_type __diff = __n1 - __n2; if (__diff > std::numeric_limits::max()) return std::numeric_limits::max(); if (__diff < std::numeric_limits::min())