[libcxx-commits] [libcxx] [libc++] Add exception guard for vector<bool>::__init_with_sentinel (PR #115491)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 28 09:06:33 PST 2024


================
@@ -0,0 +1,87 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef EXCEPTION_TEST_HELPERS_H
+#define EXCEPTION_TEST_HELPERS_H
+
+#include "count_new.h"
+
+template <class T>
+struct throwing_allocator {
+  using value_type      = T;
+  using is_always_equal = std::false_type;
----------------
winner245 wrote:

The above test has been replaced by the following test:

```cpp
try { // Throw in vector(vector&&, const allocator_type&) from type during element-wise move
  std::vector<throwing_t, test_allocator<throwing_t> > vec(test_allocator<throwing_t>(1));
  int throw_after = 10;
  throwing_t v(throw_after);
  vec.insert(vec.end(), 6, v);
  std::vector<throwing_t, test_allocator<throwing_t> > vec2(std::move(vec), test_allocator<throwing_t>(2));
} catch (int) {
}
check_new_delete_called();
```

This test now uses  `test_allocator<T>`, which checks equality between the allocator instances  based on the `data_` field. In the above test, the data fields 1 and 2 make the two allocators unequal, thus ensuring element-wise move. 

The original test use a stateless allocator type `Allocator` (now renamed to `throwing_allocator`), which is not suitable for the above purpose. Also, the fact that a stateless allocator type has a false `is_always_equal` value sounds a bit counter-intuitive to me. That's why I removed the member type in my later PR. Now `locator_traits<throwing_allocator>::is_always_equal` becomes true and this is consistence with its `operator==` which always returns true. 

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


More information about the libcxx-commits mailing list