[libcxx-commits] [libcxx] [libc++] Fix wraparound issue with -fsanitize=integer in string operator>> (PR #106263)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 27 13:03:11 PDT 2024


================
@@ -1211,12 +1211,12 @@ 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;
+      static_assert(numeric_limits<_Size>::max() >= numeric_limits<streamsize>::max(),
----------------
zhihaoy wrote:

`__str.max_size()` may not be representable in `streamsize`, either. And we'd better not let `__n` exceed `max_size()` in practice, so it looks reasonable to count by `size_type`. If `__is.width()` is larger than `max_size()` at runtime, stores `max_size()`.

https://github.com/llvm/llvm-project/pull/106263


More information about the libcxx-commits mailing list