Skip to content

[libc++] Fix wraparound issue with -fsanitize=integer in string operator>> #106263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions libcxx/include/__type_traits/make_unsigned.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ template <class _Tp>
using make_unsigned_t = __make_unsigned_t<_Tp>;
#endif

#ifndef _LIBCPP_CXX03_LANG
template <class _Tp>
_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 <class _Tp, class _Up>
using __copy_unsigned_t = __conditional_t<is_unsigned<_Tp>::value, __make_unsigned_t<_Up>, _Up>;
Expand Down
18 changes: 12 additions & 6 deletions libcxx/include/istream
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ template <class Stream, class T>
#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 <bitset>
Expand Down Expand Up @@ -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<streamsize>::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<ctype<_CharT> >(__is.getloc());
while (__c < __n) {
typename _Traits::int_type __i = __is.rdbuf()->sgetc();
Expand Down
Loading