From 1e84f4f024197cafedab4b9bd06bb86ce03b47c2 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Fri, 10 Oct 2025 14:55:22 -0700 Subject: [PATCH 1/2] Add casts for ASan. --- stl/inc/xstring | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/stl/inc/xstring b/stl/inc/xstring index 0f92b332971..e42f8f95a49 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -948,7 +948,7 @@ private: _Traits::assign(_My_data._Bx._Buf[_Count], _Elem()); } else { // _Strat == _Construct_strategy::_From_string #ifdef _INSERT_STRING_ANNOTATION - _Traits::copy(_My_data._Bx._Buf, _Arg, _Count + 1); + _Traits::copy(_My_data._Bx._Buf, _Arg, static_cast(_Count + 1)); #else // ^^^ _INSERT_STRING_ANNOTATION / !_INSERT_STRING_ANNOTATION vvv _Traits::copy(_My_data._Bx._Buf, _Arg, _BUF_SIZE); #endif // ^^^ !_INSERT_STRING_ANNOTATION ^^^ @@ -1570,7 +1570,7 @@ public: // append _Count * _Ch const size_type _Old_size = _Mypair._Myval2._Mysize; if (_Count <= _Mypair._Myval2._Myres - _Old_size) { - _ASAN_STRING_MODIFY(*this, _Old_size, _Old_size + _Count); + _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Count)); _Mypair._Myval2._Mysize = static_cast(_Old_size + _Count); _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _Traits::assign(_Old_ptr + _Old_size, static_cast(_Count), _Ch); @@ -1759,7 +1759,7 @@ public: _Mypair._Myval2._Check_offset(_Off); const size_type _Old_size = _Mypair._Myval2._Mysize; if (_Count <= _Mypair._Myval2._Myres - _Old_size) { - _ASAN_STRING_MODIFY(*this, _Old_size, _Old_size + _Count); + _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Count)); _Mypair._Myval2._Mysize = static_cast(_Old_size + _Count); _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _Elem* const _Insert_at = _Old_ptr + _Off; @@ -2202,7 +2202,7 @@ public: _CONSTEXPR20 void push_back(const _Elem _Ch) { // insert element at end const size_type _Old_size = _Mypair._Myval2._Mysize; if (_Old_size < _Mypair._Myval2._Myres) { - _ASAN_STRING_MODIFY(*this, _Old_size, _Old_size + 1); + _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + 1)); _Mypair._Myval2._Mysize = static_cast(_Old_size + 1); _Elem* const _Ptr = _Mypair._Myval2._Myptr(); _Traits::assign(_Ptr[_Old_size], _Ch); @@ -3026,7 +3026,7 @@ private: const size_type _Old_size = _Mypair._Myval2._Mysize; if (_Count <= _Mypair._Myval2._Myres - _Old_size) { - _ASAN_STRING_MODIFY(*this, _Old_size, _Old_size + _Count); + _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Count)); _Mypair._Myval2._Mysize = static_cast(_Old_size + _Count); _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _STD _Traits_move_batch<_Traits>(_Old_ptr + _Old_size, _Ptr, static_cast(_Count)); @@ -3088,7 +3088,7 @@ private: #endif // ^^^ !_HAS_CXX20 ^^^ if (_Check_overlap) { - _ASAN_STRING_MODIFY(*this, _Old_size, _Old_size + _Count); + _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Count)); _Mypair._Myval2._Mysize = static_cast(_Old_size + _Count); _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _Elem* const _Insert_at = _Old_ptr + _Off; @@ -3159,7 +3159,7 @@ private: #endif // _HAS_CXX20 { if (_Growth <= _Mypair._Myval2._Myres - _Old_size) { // growth fits - _ASAN_STRING_MODIFY(*this, _Old_size, _Old_size + _Growth); + _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Growth)); _Mypair._Myval2._Mysize = static_cast(_Old_size + _Growth); _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _Elem* const _Insert_at = _Old_ptr + _Off; From 6ec1d9255f4047b855e7c9b0ff484eabe10f036c Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Fri, 10 Oct 2025 15:02:29 -0700 Subject: [PATCH 2/2] Extract `_New_size`. --- stl/inc/xstring | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/stl/inc/xstring b/stl/inc/xstring index e42f8f95a49..108ac5192f9 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -1570,8 +1570,9 @@ public: // append _Count * _Ch const size_type _Old_size = _Mypair._Myval2._Mysize; if (_Count <= _Mypair._Myval2._Myres - _Old_size) { - _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Count)); - _Mypair._Myval2._Mysize = static_cast(_Old_size + _Count); + const auto _New_size = static_cast(_Old_size + _Count); + _ASAN_STRING_MODIFY(*this, _Old_size, _New_size); + _Mypair._Myval2._Mysize = _New_size; _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _Traits::assign(_Old_ptr + _Old_size, static_cast(_Count), _Ch); _Traits::assign(_Old_ptr[_Old_size + _Count], _Elem()); @@ -1759,8 +1760,9 @@ public: _Mypair._Myval2._Check_offset(_Off); const size_type _Old_size = _Mypair._Myval2._Mysize; if (_Count <= _Mypair._Myval2._Myres - _Old_size) { - _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Count)); - _Mypair._Myval2._Mysize = static_cast(_Old_size + _Count); + const auto _New_size = static_cast(_Old_size + _Count); + _ASAN_STRING_MODIFY(*this, _Old_size, _New_size); + _Mypair._Myval2._Mysize = _New_size; _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _Elem* const _Insert_at = _Old_ptr + _Off; _Traits::move( @@ -2202,8 +2204,9 @@ public: _CONSTEXPR20 void push_back(const _Elem _Ch) { // insert element at end const size_type _Old_size = _Mypair._Myval2._Mysize; if (_Old_size < _Mypair._Myval2._Myres) { - _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + 1)); - _Mypair._Myval2._Mysize = static_cast(_Old_size + 1); + const auto _New_size = static_cast(_Old_size + 1); + _ASAN_STRING_MODIFY(*this, _Old_size, _New_size); + _Mypair._Myval2._Mysize = _New_size; _Elem* const _Ptr = _Mypair._Myval2._Myptr(); _Traits::assign(_Ptr[_Old_size], _Ch); _Traits::assign(_Ptr[_Old_size + 1], _Elem()); @@ -3026,8 +3029,9 @@ private: const size_type _Old_size = _Mypair._Myval2._Mysize; if (_Count <= _Mypair._Myval2._Myres - _Old_size) { - _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Count)); - _Mypair._Myval2._Mysize = static_cast(_Old_size + _Count); + const auto _New_size = static_cast(_Old_size + _Count); + _ASAN_STRING_MODIFY(*this, _Old_size, _New_size); + _Mypair._Myval2._Mysize = _New_size; _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _STD _Traits_move_batch<_Traits>(_Old_ptr + _Old_size, _Ptr, static_cast(_Count)); _Traits::assign(_Old_ptr[_Old_size + _Count], _Elem()); @@ -3088,8 +3092,9 @@ private: #endif // ^^^ !_HAS_CXX20 ^^^ if (_Check_overlap) { - _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Count)); - _Mypair._Myval2._Mysize = static_cast(_Old_size + _Count); + const auto _New_size = static_cast(_Old_size + _Count); + _ASAN_STRING_MODIFY(*this, _Old_size, _New_size); + _Mypair._Myval2._Mysize = _New_size; _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _Elem* const _Insert_at = _Old_ptr + _Off; // the range [_Ptr, _Ptr + _Ptr_shifted_after) is left alone by moving the suffix out, @@ -3159,8 +3164,9 @@ private: #endif // _HAS_CXX20 { if (_Growth <= _Mypair._Myval2._Myres - _Old_size) { // growth fits - _ASAN_STRING_MODIFY(*this, _Old_size, static_cast(_Old_size + _Growth)); - _Mypair._Myval2._Mysize = static_cast(_Old_size + _Growth); + const auto _New_size = static_cast(_Old_size + _Growth); + _ASAN_STRING_MODIFY(*this, _Old_size, _New_size); + _Mypair._Myval2._Mysize = _New_size; _Elem* const _Old_ptr = _Mypair._Myval2._Myptr(); _Elem* const _Insert_at = _Old_ptr + _Off; _Elem* const _Suffix_at = _Insert_at + _Nx;