[libcxx-commits] [libcxx] [libc++] Validate exception throwing for vector mutators on max_size violation (PR #131953)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jun 20 07:58:39 PDT 2025


================
@@ -0,0 +1,59 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// UNSUPPORTED: no-exceptions
+
+// <vector>
+// vector<bool>
+
+// template<container-compatible-range<bool> R>
+//   constexpr void append_range(R&& rg); // C++23
+
+#include <cassert>
+#include <vector>
+
+#include "../insert_range_sequence_containers.h"
+#include "test_allocator.h"
+
+void test() {
+  // Note: `test_append_range_exception_safety_throwing_copy` doesn't apply because copying booleans cannot throw.
+  test_append_range_exception_safety_throwing_allocator<std::vector, bool>();
+
+  {
+    using Vec = std::vector<bool, limited_allocator<bool, 10> >;
+    Vec v(Vec().max_size() - 2, true);
+    bool a[] = {false, true, false};
----------------
winner245 wrote:

The way `vector<bool, limited_allocator<bool, 10>>` works with `limited_allocator<bool, 10>` is somewhat confusing. The `max_size()` in this case is `10 * sizeof(size_t) = 640` bits, on a typical 64-bit platform. 

```cpp
template <class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::size_type
vector<bool, _Allocator>::max_size() const _NOEXCEPT {
  size_type __amax = __storage_traits::max_size(__alloc_); // 10 for limited_allocator<T, 10>
  size_type __nmax = numeric_limits<difference_type>::max();
  return __nmax / __bits_per_word <= __amax ? __nmax : __internal_cap_to_external(__amax); // 10 * 64
}
```
Therefore, `Vec v(Vec().max_size() - 2, true)` initializes a vector of size 638. This is why I choose a length 3 array `a[]` to append, so that we trigger an exception: `638 + 3 > max_size()`. 


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


More information about the libcxx-commits mailing list