Skip to content

Commit

Permalink
Fix string::replace under ASan (#3884)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicole Mazzuca <[email protected]>
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
3 people authored Jul 20, 2023
1 parent 34f86c5 commit 8a93121
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -3807,12 +3807,13 @@ public:
// either we are shrinking, or the growth fits
// may temporarily overflow; OK because size_type must be unsigned
const auto _New_size = _Old_size + _Count - _Nx;
_ASAN_STRING_MODIFY(*this, _Old_size, _New_size);
_ASAN_STRING_REMOVE(*this);
_Mypair._Myval2._Mysize = _New_size;
_Elem* const _Old_ptr = _Mypair._Myval2._Myptr();
_Elem* const _Insert_at = _Old_ptr + _Off;
_Traits::move(_Insert_at + _Count, _Insert_at + _Nx, _Old_size - _Nx - _Off + 1);
_Traits::assign(_Insert_at, _Count, _Ch);
_ASAN_STRING_CREATE(*this);
return *this;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/std/tests/GH_002030_asan_annotate_string/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,13 @@ void test_DevCom_10109507() {
assert(s == "xyefbcd");
}

void test_gh_3883() {
// GH-3883 <string>: basic_string::replace fails under ASan when pos + count > size, and count2 < count
string t = "0123456789ABCDEF"; // large string
t.replace(0, 30, 7, 'A');
assert(t == "AAAAAAA");
}

int main() {
run_allocator_matrix<char>();
#ifdef __cpp_char8_t
Expand All @@ -1924,4 +1931,5 @@ int main() {

test_DevCom_10116361();
test_DevCom_10109507();
test_gh_3883();
}

0 comments on commit 8a93121

Please sign in to comment.