From b0e19cfb6205d7cf36aa0fc3cb395205f7dc36ba Mon Sep 17 00:00:00 2001 From: James Y Knight Date: Sun, 24 Sep 2023 09:12:57 -0400 Subject: [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. --- .../strings/basic.string/string.modifiers/string_assign/string.pass.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp') 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 -- cgit v1.2.3