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 2 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
57 changes: 57 additions & 0 deletions tests/std/tests/GH_003883_replace_asan_failure/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) Microsoft Corporation.
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# This test matrix is the usual test matrix, with all currently unsupported options removed, crossed with the ASan flags.

# TRANSITION, google/sanitizers#328 - clang-cl does not currently support targeting /MDd or /MTd.
RUNALL_INCLUDE ..\prefix.lst
RUNALL_CROSSLIST
PM_CL="/Zi /wd4611 /w14640 /Zc:threadSafeInit-" PM_LINK="/debug"
RUNALL_CROSSLIST
PM_CL="-fsanitize=address /BE /c /EHsc /MD /std:c++14"
PM_CL="-fsanitize=address /BE /c /EHsc /MDd /std:c++17 /permissive-"
PM_CL="-fsanitize=address /BE /c /EHsc /MT /std:c++20 /permissive-"
PM_CL="-fsanitize=address /BE /c /EHsc /MTd /std:c++latest /permissive-"
PM_CL="-fsanitize=address /EHsc /MD /std:c++14"
PM_CL="-fsanitize=address /EHsc /MD /std:c++17"
PM_CL="-fsanitize=address /EHsc /MD /std:c++20"
PM_CL="-fsanitize=address /EHsc /MD /std:c++latest /permissive- /Zc:char8_t- /Zc:preprocessor"
PM_CL="-fsanitize=address /EHsc /MD /std:c++latest /permissive- /Zc:noexceptTypes-"
PM_CL="-fsanitize=address /EHsc /MDd /std:c++14 /fp:except /Zc:preprocessor"
PM_CL="-fsanitize=address /EHsc /MDd /std:c++17 /permissive-"
PM_CL="-fsanitize=address /EHsc /MDd /std:c++20 /permissive-"
PM_CL="-fsanitize=address /EHsc /MDd /std:c++latest /permissive- /Zc:wchar_t-"
PM_CL="-fsanitize=address /EHsc /MDd /std:c++latest /permissive-"
PM_CL="-fsanitize=address /EHsc /MT /std:c++latest /permissive- /analyze:only /analyze:autolog-"
PM_CL="-fsanitize=address /EHsc /MT /std:c++latest /permissive-"
PM_CL="-fsanitize=address /EHsc /MTd /std:c++latest /permissive"
PM_CL="-fsanitize=address /EHsc /MTd /std:c++latest /permissive- /analyze:only /analyze:autolog-"
PM_CL="-fsanitize=address /EHsc /MTd /std:c++latest /permissive- /fp:strict"
PM_CL="-fsanitize=address /EHsc /MTd /std:c++latest /permissive-"
PM_CL="/D_ANNOTATE_STRING /BE /c /EHsc /MD /std:c++14"
PM_CL="/D_ANNOTATE_STRING /BE /c /EHsc /MDd /std:c++17 /permissive-"
PM_CL="/D_ANNOTATE_STRING /BE /c /EHsc /MT /std:c++20 /permissive-"
PM_CL="/D_ANNOTATE_STRING /BE /c /EHsc /MTd /std:c++latest /permissive-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MD /std:c++14"
PM_CL="/D_ANNOTATE_STRING /EHsc /MD /std:c++14 /Zc:char8_t"
PM_CL="/D_ANNOTATE_STRING /EHsc /MD /std:c++17"
PM_CL="/D_ANNOTATE_STRING /EHsc /MD /std:c++17 /Zc:char8_t"
PM_CL="/D_ANNOTATE_STRING /EHsc /MD /std:c++20"
PM_CL="/D_ANNOTATE_STRING /EHsc /MD /std:c++latest /permissive- /Zc:char8_t- /Zc:preprocessor"
PM_CL="/D_ANNOTATE_STRING /EHsc /MD /std:c++latest /permissive- /Zc:noexceptTypes-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MDd /std:c++14 /fp:except /Zc:preprocessor"
PM_CL="/D_ANNOTATE_STRING /EHsc /MDd /std:c++17 /permissive-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MDd /std:c++20 /permissive-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MDd /std:c++latest /permissive- /Zc:wchar_t-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MDd /std:c++latest /permissive-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MT /std:c++latest /permissive- /analyze:only /analyze:autolog-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MT /std:c++latest /permissive-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MTd /std:c++latest /permissive"
PM_CL="/D_ANNOTATE_STRING /EHsc /MTd /std:c++latest /permissive- /analyze:only /analyze:autolog-"
PM_CL="/D_ANNOTATE_STRING /EHsc /MTd /std:c++latest /permissive- /fp:strict"
PM_CL="/D_ANNOTATE_STRING /EHsc /MTd /std:c++latest /permissive-"
# TRANSITION, clang-cl does not support /alternatename so we cannot test /D_ANNOTATE_STRING without -fsanitize=address
PM_COMPILER="clang-cl" PM_CL="-fsanitize=address -fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /EHsc /MD /std:c++14"
PM_COMPILER="clang-cl" PM_CL="-fsanitize=address -fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /EHsc /MD /std:c++17"
PM_COMPILER="clang-cl" PM_CL="-fsanitize=address -fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /EHsc /MT /std:c++20 /permissive-"
PM_COMPILER="clang-cl" PM_CL="-fsanitize=address -fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /EHsc /MT /std:c++latest /permissive- /fp:strict"
9 changes: 9 additions & 0 deletions tests/std/tests/GH_003883_replace_asan_failure/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <string>

int main() {
std::string t = "0123456789ABCDEF"; // large string
t.replace(0, 30, 7, 'A');
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
}