[libcxx-commits] [libcxx] [libc++] Fix improper static_cast in std::deque and __split_buffer (PR #119106)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 10 10:30:33 PST 2024


winner245 wrote:

Thank you for the feedback. I am not sure how to add a suitable test for this purpose. I have tried to find examples that can trigger issues due to the improper `static_cast`, but I couldn't find a good one. The reason is that casting an integral value to `std::size_t` instead of `size_type` generally only enlarges the data range (since any practical `size_type` cannot exceed `std::size_t`), which does not cause data loss.

However, the improper `static_cast` I pointed out in this PR is indeed redundant. For example, in the following code, the explicit `static_cast<size_t>` first casts to `size_t`. But it then implicitly casts to `size_type` due to the explicit template argument specified as size_type:

```cpp
size_type __c = std::max<size_type>(2 * static_cast<size_t>(__cap_ - __first_), 1);
```

I have also experimented with some custom allocators with different `size_type` definitions, e.g., uint_8, uint_16 (https://godbolt.org/z/cdse8Gh48). I found that `std::vector` strictly respects `max_size()` when calling `reserve()` and `resize()`, but `std::deque` seems to have completely ignored  `max_size()` (perhaps because it is not a contiguous container). I am not sure if we should add a test similar to what I have done with the custom allocator.

https://github.com/llvm/llvm-project/pull/119106


More information about the libcxx-commits mailing list