Skip to content

Commit 3b5b5c1

Browse files
vitalybukallvmbot
authored andcommitted
[libcxx] Align __recommend() + 1 by __endian_factor (#90292)
This is detected by asan after #83774 Allocation size will be divided by `__endian_factor` before storing. If it's not aligned, we will not be able to recover allocation size to pass into `__alloc_traits::deallocate`. we have code like this ``` auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1); __p = __allocation.ptr; __set_long_cap(__allocation.count); void __set_long_cap(size_type __s) _NOEXCEPT { __r_.first().__l.__cap_ = __s / __endian_factor; __r_.first().__l.__is_long_ = true; } size_type __get_long_cap() const _NOEXCEPT { return __r_.first().__l.__cap_ * __endian_factor; } inline ~basic_string() { __annotate_delete(); if (__is_long()) __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap()); } ``` 1. __recommend() -> even size 2. `std::__allocate_at_least(__alloc(), __recommend(__sz) + 1)` - > not even size 3. ` __set_long_cap() `- > lose one bit of size for __endian_factor == 2 (see `/ __endian_factor`) 4. `__alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap())` -> uses even size (see `__get_long_cap`) (cherry picked from commit d129ea8)
1 parent 72c9425 commit 3b5b5c1

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Diff for: libcxx/include/string

+2-2
Original file line numberDiff line numberDiff line change
@@ -1943,10 +1943,10 @@ private:
19431943
if (__s < __min_cap) {
19441944
return static_cast<size_type>(__min_cap) - 1;
19451945
}
1946-
const size_type __boundary = sizeof(value_type) < __alignment ? __alignment / sizeof(value_type) : 1;
1946+
const size_type __boundary = sizeof(value_type) < __alignment ? __alignment / sizeof(value_type) : __endian_factor;
19471947
size_type __guess = __align_it<__boundary>(__s + 1) - 1;
19481948
if (__guess == __min_cap)
1949-
++__guess;
1949+
__guess += __endian_factor;
19501950
return __guess;
19511951
}
19521952

0 commit comments

Comments
 (0)