[libcxx-commits] [libcxx] 07443e9 - [libc++][test] Improve ThrowingT to Accurately Throw after throw_after > 1 Use (#114077)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 5 08:24:11 PST 2024


Author: Peng Liu
Date: 2024-11-05T11:24:05-05:00
New Revision: 07443e9776f97090a776cba0288d65e90b6f1af4

URL: https://github.com/llvm/llvm-project/commit/07443e9776f97090a776cba0288d65e90b6f1af4
DIFF: https://github.com/llvm/llvm-project/commit/07443e9776f97090a776cba0288d65e90b6f1af4.diff

LOG: [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.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
index c9c1bac2fb4a0c..e2b0d691889c6c 100644
--- a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
@@ -49,13 +49,14 @@ struct ThrowingT {
     --throw_after_n;
   }
 
-  ThrowingT(const ThrowingT&) {
+  ThrowingT(const ThrowingT& rhs) : throw_after_n_(rhs.throw_after_n_) {
     if (throw_after_n_ == nullptr || *throw_after_n_ == 0)
       throw 1;
     --*throw_after_n_;
   }
 
-  ThrowingT& operator=(const ThrowingT&) {
+  ThrowingT& operator=(const ThrowingT& rhs) {
+    throw_after_n_ = rhs.throw_after_n_;
     if (throw_after_n_ == nullptr || *throw_after_n_ == 0)
       throw 1;
     --*throw_after_n_;


        


More information about the libcxx-commits mailing list