[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:43 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};
+ try {
+ v.append_range(a);
+ assert(false);
+ } catch (const std::length_error&) {
+ assert(v.size() == v.max_size() - 2);
+ for (std::size_t i = 0; i < v.size(); ++i)
+ assert(v[i] == true);
+ }
+ }
+ {
+ std::vector<bool, limited_allocator<bool, 10> > v(10, true);
+ bool a[10 * 65536] = {};
----------------
winner245 wrote:
This number `10 * 65536` preexisted prior to this patch, and has been used in my patch. The rationale behind choosing such a large number is to account for differences in the underlying storage type used by various `vector<bool>` implementations. For instance, the storage word types are `size_t` in libc++, `unsigned long` in GCC, and `unsigned int` in MSVC-STL. Consequently, appending a 64K-sized array helps ensure that an `std::length_error` exception occurs across all implementations.
This behavior was also documented in the following comment from a previous test:
https://github.com/llvm/llvm-project/blob/f242360e156b407902829d694c036b2d22211894/libcxx/test/std/containers/sequences/vector.bool/reserve.pass.cpp#L62-L73
As you commented, I also realized that this number is a bit `ad-hoc` can be confusing without context. Since it's used in multiple places in this patch, I’ve replaced it with `v.max_size()` or `v.max_size() + 1` where appropriate. This makes the intention clearer and avoids the need for an explanatory comment.
https://github.com/llvm/llvm-project/pull/131953
More information about the libcxx-commits
mailing list