Skip to content

Commit 602c193

Browse files
committed
[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
1 parent 7b5dfb4 commit 602c193

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

libcxx/include/string

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3941,7 +3941,7 @@ basic_string<_CharT, _Traits, _Allocator>::__invariants() const
39413941
return false;
39423942
if (data() == 0)
39433943
return false;
3944-
if (data()[size()] != value_type(0))
3944+
if (data()[size()] != value_type())
39453945
return false;
39463946
return true;
39473947
}
@@ -3959,6 +3959,7 @@ basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
39593959
__alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
39603960
__set_long_cap(0);
39613961
__set_short_size(0);
3962+
traits_type::assign(*__get_short_pointer(), value_type());
39623963
}
39633964
}
39643965

0 commit comments

Comments
 (0)