summaryrefslogtreecommitdiff
path: root/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp
diff options
context:
space:
mode:
authorJames Y Knight <jyknight@google.com>2023-09-24 09:12:57 -0400
committerGitHub <noreply@github.com>2023-09-24 09:12:57 -0400
commitb0e19cfb6205d7cf36aa0fc3cb395205f7dc36ba (patch)
tree64016f5949ee2c67ce29e345a0e4bc0b8d184e2c /libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp
parent8b36d082c48c81454dcc66d9e70e473ae4b2b7f8 (diff)
[libcxx] Don't deallocate non-pointer data in string assignment. (#67200)
Previously, assignment to a std::basic_string type with a _custom_ allocator could under certain conditions attempt to interpret part of the target string's "short" string-content as if it was a "long" data pointer, and attempt to deallocate a garbage value. This is a serious bug, but code in which it might happen is rare. It required: 1. the basic_string must be using a custom allocator type which sets the propagate_on_container_copy_assignment trait to true (thus, it does not affect the default allocator, nor most custom allocators). 2. the allocator for the target string must compare not equal to the allocator for the source string (many allocators always compare equal). 3. the source of the copy must currently contain a "long" string, and the assignment-target must currently contain a "short" string. Finally, the issue would've typically been innocuous when the bytes misinterpreted as a pointer were all zero, as deallocating a nullptr is typically a no-op. This is why existing test cases did not exhibit an issue: they were all zero-length strings, which do not have data in the bytes interpreted as a pointer.
Diffstat (limited to 'libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp')
-rw-r--r--libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp1
1 files changed, 1 insertions, 0 deletions
diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp
index 311778c35dcb..6db086e78ed0 100644
--- a/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp
@@ -90,6 +90,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
testAlloc(S(A(5)), S("1"), A());
testAlloc(S(A(5)), S("1", A(7)), A(7));
testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7));
+ testAlloc(S("12345678901234567890", A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7));
}
#if TEST_STD_VER >= 11