Skip to content

Commit 07443e9

Browse files
authored
[libc++][test] Improve ThrowingT to Accurately Throw after throw_after > 1 Use (#114077)
This PR fixes the `ThrowingT` class, which currently fails to raise exceptions after a specified number of copy construction operations. The class is intended to throw in a controlled manner based on a specified counter value `throw_after`. However, its current implementation of the copy constructor fails to achieve this goal. The problem arises because the copy constructor does not initialize the `throw_after_n_` member, leaving `throw_after_n_` to default to `nullptr` as defined by the in-class initializer. As a result, its copy constructor always checks against `nullptr`, causing an immediate exception rather than throwing after the specified number `throw_after` of uses. The fix is straightforward: simply initialize the `throw_after_n_` member in the member initializer list. This issue was previously uncovered because all exception tests for `std::vector` in `exceptions.pass.cpp` used a `throw_after` value of 1, which coincidentally aligned with the class's behavior.
1 parent 8dfd9ff commit 07443e9

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ struct ThrowingT {
4949
--throw_after_n;
5050
}
5151

52-
ThrowingT(const ThrowingT&) {
52+
ThrowingT(const ThrowingT& rhs) : throw_after_n_(rhs.throw_after_n_) {
5353
if (throw_after_n_ == nullptr || *throw_after_n_ == 0)
5454
throw 1;
5555
--*throw_after_n_;
5656
}
5757

58-
ThrowingT& operator=(const ThrowingT&) {
58+
ThrowingT& operator=(const ThrowingT& rhs) {
59+
throw_after_n_ = rhs.throw_after_n_;
5960
if (throw_after_n_ == nullptr || *throw_after_n_ == 0)
6061
throw 1;
6162
--*throw_after_n_;

0 commit comments

Comments
 (0)