[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:52 PDT 2025


================
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<container-compatible-range<T> 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() {
+  test_append_range_exception_safety_throwing_copy<std::vector>();
+  test_append_range_exception_safety_throwing_allocator<std::vector, int>();
+
+  {
+    std::vector<int, limited_allocator<int, 10> > v(8, 42);
+    int a[] = {1, 2, 3};
+    try {
+      v.append_range(a);
+      assert(false);
+    } catch (...) {
----------------
winner245 wrote:

My initial concern was that the standard doesn't appear to explicitly state that these modifiers—`append_range`, `push_back`, `emplace{,_back}`, and `insert{, _range}`—must throw `std::length_error`, even though all major implementations do. In some scenarios, they may instead throw `std::bad_alloc`, which is why I initially used a catch-all `...` for robustness.

However, after double-checking, I’ve confirmed that all three major standard library implementations (libc++, GCC, and MSVC-STL) consistently throw `std::length_error` when a modifier would exceed `max_size()`. Accordingly, I’ve updated the code to replace the catch-all handlers with `const std::length_error&` where appropriate.


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


More information about the libcxx-commits mailing list