[libcxx-commits] [libcxx] 602c193 - [libc++] Make sure __clear_and_shrink() maintains string invariants

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 7 06:17:07 PDT 2020


Author: Louis Dionne
Date: 2020-10-07T09:16:59-04:00
New Revision: 602c193e2aeb19f5d2e48075281601a2386e8758

URL: https://github.com/llvm/llvm-project/commit/602c193e2aeb19f5d2e48075281601a2386e8758
DIFF: https://github.com/llvm/llvm-project/commit/602c193e2aeb19f5d2e48075281601a2386e8758.diff

LOG: [libc++] Make sure __clear_and_shrink() maintains string invariants

__clear_and_shrink() was added in D41976, and a test was added alongside
it to make sure that the string invariants were maintained. However, it
appears that the test never ran under UBSan before, which would have
highlighted the fact that it doesn't actually maintain the string
invariants.

Differential Revision: https://reviews.llvm.org/D88849

Added: 
    

Modified: 
    libcxx/include/string

Removed: 
    


################################################################################
diff  --git a/libcxx/include/string b/libcxx/include/string
index b6380da95c64..c900d9609ed4 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -3941,7 +3941,7 @@ basic_string<_CharT, _Traits, _Allocator>::__invariants() const
         return false;
     if (data() == 0)
         return false;
-    if (data()[size()] != value_type(0))
+    if (data()[size()] != value_type())
         return false;
     return true;
 }
@@ -3959,6 +3959,7 @@ basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
         __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
         __set_long_cap(0);
         __set_short_size(0);
+        traits_type::assign(*__get_short_pointer(), value_type());
     }
 }
 


        


More information about the libcxx-commits mailing list