Skip to content
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

Fix string::replace under ASan #3884

Merged
merged 3 commits into from
Jul 20, 2023
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
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;
Comment on lines 3809 to 3811
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change requested: We could fuse _New_size now, or move its definition below the _ASAN_STRING_REMOVE. This is fine as-is though.

_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();
}