[libcxx-commits] [libcxx] [libc++] Validate exception throwing for vector mutators on max_size violation (PR #131953)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jun 24 10:08:38 PDT 2025
================
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: no-exceptions
+
+// <vector>
+// vector<bool>
+
+// template <class... Args>
+// reference emplace_back(Args&&... args); // reference in C++17
+
+#include <cassert>
+#include <stdexcept>
+#include <vector>
+
+#include "test_allocator.h"
+
+int main(int, char**) {
+ std::vector<bool, limited_allocator<bool, 10> > v;
+ v.resize(v.max_size(), true);
+ try {
+ v.emplace_back(true);
+ assert(false);
+ } catch (const std::length_error&) {
+ assert(v.size() == v.max_size());
+ for (std::size_t i = 0; i != v.size(); ++i)
+ assert(v[i] == true);
+ }
----------------
ldionne wrote:
```suggestion
{ // Try adding an element in a vector that is already at the allocator's maximum capacity.
std::vector<bool, limited_allocator<bool, 10> > v;
v.resize(v.max_size(), true);
try {
v.emplace_back(true);
assert(false);
} catch (const std::length_error&) {
assert(v.size() == v.max_size());
for (std::size_t i = 0; i != v.size(); ++i)
assert(v[i] == true);
}
}
```
https://github.com/llvm/llvm-project/pull/131953
More information about the libcxx-commits
mailing list