diff --git a/libcxx/include/__type_traits/make_unsigned.h b/libcxx/include/__type_traits/make_unsigned.h index 282cd2d9113166..8757f451eb807b 100644 --- a/libcxx/include/__type_traits/make_unsigned.h +++ b/libcxx/include/__type_traits/make_unsigned.h @@ -86,12 +86,10 @@ template using make_unsigned_t = __make_unsigned_t<_Tp>; #endif -#ifndef _LIBCPP_CXX03_LANG template -_LIBCPP_HIDE_FROM_ABI constexpr __make_unsigned_t<_Tp> __to_unsigned_like(_Tp __x) noexcept { +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __make_unsigned_t<_Tp> __to_unsigned_like(_Tp __x) _NOEXCEPT { return static_cast<__make_unsigned_t<_Tp> >(__x); } -#endif template using __copy_unsigned_t = __conditional_t::value, __make_unsigned_t<_Up>, _Up>; diff --git a/libcxx/include/istream b/libcxx/include/istream index d2b577a9ad9efc..7c65a24bc313d9 100644 --- a/libcxx/include/istream +++ b/libcxx/include/istream @@ -165,6 +165,7 @@ template #include <__type_traits/conjunction.h> #include <__type_traits/enable_if.h> #include <__type_traits/is_base_of.h> +#include <__type_traits/make_unsigned.h> #include <__utility/declval.h> #include <__utility/forward.h> #include @@ -1211,12 +1212,17 @@ operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _ try { #endif __str.clear(); - streamsize __n = __is.width(); - if (__n <= 0) - __n = __str.max_size(); - if (__n <= 0) - __n = numeric_limits::max(); - streamsize __c = 0; + using _Size = typename basic_string<_CharT, _Traits, _Allocator>::size_type; + streamsize const __width = __is.width(); + _Size const __max_size = __str.max_size(); + _Size __n; + if (__width <= 0) { + __n = __max_size; + } else { + __n = std::__to_unsigned_like(__width) < __max_size ? static_cast<_Size>(__width) : __max_size; + } + + _Size __c = 0; const ctype<_CharT>& __ct = std::use_facet >(__is.getloc()); while (__c < __n) { typename _Traits::int_type __i = __is.rdbuf()->sgetc();