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::assign and ::resize_and_overwrite under ASan #3956

Merged
merged 8 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 2 additions & 4 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -3371,11 +3371,9 @@ public:
_In_reads_(_Count) const _Elem* const _Ptr, _CRT_GUARDOVERFLOW const size_type _Count) {
// assign [_Ptr, _Ptr + _Count)
if (_Count <= _Mypair._Myval2._Myres) {
_ASAN_STRING_MODIFY(*this, _Mypair._Myval2._Mysize, _Count);
achabense marked this conversation as resolved.
Show resolved Hide resolved
_Elem* const _Old_ptr = _Mypair._Myval2._Myptr();
_Mypair._Myval2._Mysize = _Count;
_Elem* const _Old_ptr = _Mypair._Myval2._Myptr();
_Traits::move(_Old_ptr, _Ptr, _Count);
_Traits::assign(_Old_ptr[_Count], _Elem());
_Eos(_Count);
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 @@ -1920,6 +1920,13 @@ void test_gh_3883() {
assert(t == "AAAAAAA");
}

void test_gh_3955() {
// GH-3955 <xstring>: ASAN report container-overflow in a legal case
string s(19, '0');
s = &s[3];
assert(s == string(16, '0'));
}

int main() {
run_allocator_matrix<char>();
#ifdef __cpp_char8_t
Expand All @@ -1932,4 +1939,5 @@ int main() {
test_DevCom_10116361();
test_DevCom_10109507();
test_gh_3883();
test_gh_3955();
}