From ae72c18c57e67ee06940eebce3b1952a3c8236df Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:39 +0000 Subject: [PATCH 1/4] Do not destroy subobjects of a thrown aggregate twice [GH #553] When an aggregate whose initialization has cleanups for partially constructed subobjects is thrown (e.g., "throw H{E(1)};"), those cleanups were still active when the exception was thrown, so the subobjects were destroyed during unwinding and again when the exception object was destroyed. Remove them from the cleanup state once the exception object is completely initialized. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 22 +++++++ ...-553-throw-aggregate-subobject-cleanup.txt | 10 +++ .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../throw-aggregate-subobject-cleanup.sft.cpp | 63 +++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 src/pending_changes/gh-553-throw-aggregate-subobject-cleanup.txt create mode 100644 tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.2.txt create mode 100644 tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp diff --git a/src/lower_init.c b/src/lower_init.c index 156d63c8cc..73dfc6d064 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -11100,6 +11100,28 @@ do_assignment:; free_destructible_entity_descr(dip->destructible_entity_descr); dip->destructible_entity_descr = NULL; } /* if */ + } else if ((options & LDIO_THROW) && + (dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate || + dip->kind == (a_dynamic_init_kind)dik_lambda) && + latest_initialization_on_entry != + eff_context->latest_initialization) { + /* A thrown aggregate (which has no destructor entry; the runtime + destroys the exception object) has been completely initialized. + Remove the cleanups for its partially constructed subobjects from + the cleanup state, so that they are not run (in addition to the + destruction of the exception object) when it is thrown. */ + a_boolean some_cloned, some_ordered; + adjust_cleanup_state_for_aggregate_init( + eff_context->latest_initialization, + latest_initialization_on_entry, + eff_insert_location, + &some_cloned, + &some_ordered); + if (exceptions_enabled) { + insert_code_to_indicate_cleanup_state(curr_context->curr_cleanup_state, + eff_insert_location, + /*unreachable=*/FALSE); + } /* if */ } /* if */ /* If the dynamic init defines a lifetime that surrounds the initialization, pop the context for that lifetime. */ diff --git a/src/pending_changes/gh-553-throw-aggregate-subobject-cleanup.txt b/src/pending_changes/gh-553-throw-aggregate-subobject-cleanup.txt new file mode 100644 index 0000000000..05a0b36533 --- /dev/null +++ b/src/pending_changes/gh-553-throw-aggregate-subobject-cleanup.txt @@ -0,0 +1,10 @@ +[GH #553] +Subobjects of a thrown aggregate destroyed twice + +When an aggregate with class members that have destructors was thrown, e.g., + + struct H { E a, b; }; + throw H{E(7)}; + +the members were destroyed during stack unwinding and again when the +exception object was destroyed. diff --git a/tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.1.txt b/tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.1.txt new file mode 100644 index 0000000000..d72d18ed14 --- /dev/null +++ b/tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++14 --exceptions Test_name.c diff --git a/tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.2.txt b/tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.throw-aggregate-subobject-cleanup.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp b/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp new file mode 100644 index 0000000000..078f64a71c --- /dev/null +++ b/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp @@ -0,0 +1,63 @@ +//type:rp +//options_all:--c++14 --exceptions + +// Throwing an aggregate whose initialization has cleanups for partially +// constructed subobjects (e.g., "throw H{E(1)}" with two class members). +// Once the exception object is completely initialized, those cleanups must +// no longer be active: before the fix, they were still active when the +// exception was thrown, so the subobjects were destroyed during unwinding +// and again when the exception object was destroyed. + +int live, errors; + +struct E { + int v; + E(int x = -1) : v(x) { ++live; } + E(const E &o) : v(o.v) { ++live; } + ~E() { if (--live < 0) ++errors; } +}; +struct Thrower { + Thrower() { throw 0; } + ~Thrower() { ++errors; } +}; +struct T { int v; T(int x) : v(x) {} ~T() {} }; +static int tv(const T &t) { return t.v; } + +struct H2 { E a, b; }; +struct H4 { E a[2]; }; +struct H5 { E a[2]; E b; }; +struct H6 { E a[2][2]; }; +struct L { E a; E b; int i; }; +struct X { E a; Thrower t; }; + +template static void run(F f) { + live = 0; + try { f(); } catch (...) {} + if (live != 0) ++errors; +} + +int main() { + run([] { throw H2{E(7)}; }); + run([] { throw H2{E(7), E(8)}; }); + run([] { throw H4{{E(7)}}; }); + run([] { throw H5{{E(7)}}; }); + run([] { throw H5{{E(1), E(2)}, E(3)}; }); + run([] { throw H6{{{E(7)}}}; }); + run([] { throw L{E(1), E(2), tv(T(3))}; }); + run([] { throw H2{E(tv(T(7)))}; }); + run([] { E x(1), y(2); throw [x, y] { return x.v + y.v; }; }); + // An enclosing local must still be destroyed (once) during unwinding. + run([] { E outer(1); throw H2{E(7)}; }); + // A subobject initializer that throws: the subobjects constructed so far + // must be destroyed (once), and no exception object is thrown. + run([] { throw X{E(1)}; }); + // The cleanup state after a caught exception must be intact. + run([] { + E outer(1); + try { throw H4{{E(2)}}; } catch (const H4 &h) { + if (h.a[0].v != 2 || h.a[1].v != -1) ++errors; + } + throw H2{E(3)}; + }); + return errors != 0; +} From e67974964748e302415946b5c3da422943187c86 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 09:39:01 +0000 Subject: [PATCH 2/4] Test temporaries in thrown aggregates; assert one context [GH #553] Make the T temporaries in the throw-aggregate-subobject-cleanup test count their constructions and destructions, and add cases with destructible temporaries before, between, and after the members with cleanups (also in nested aggregates, a lambda init-capture, a throwing member initializer, and with an enclosing local). They are destroyed once, at the end of the throw expression. A thrown aggregate or lambda has no init_expr_lifetime of its own (only constructor initializations get one, for default arguments), so the cleanup state adjustment for it runs in the effective context. Assert that. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 7 ++++- .../throw-aggregate-subobject-cleanup.sft.cpp | 30 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index 73dfc6d064..2bba25533f 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -11109,8 +11109,13 @@ do_assignment:; destroys the exception object) has been completely initialized. Remove the cleanups for its partially constructed subobjects from the cleanup state, so that they are not run (in addition to the - destruction of the exception object) when it is thrown. */ + destruction of the exception object) when it is thrown. The + aggregate has no init_expr_lifetime of its own (only constructor + initializations get one, for default arguments), so its temporaries + are in the current context, which is also the effective one. */ a_boolean some_cloned, some_ordered; + check_assertion(init_expr_lifetime == NULL && + curr_context == eff_context); adjust_cleanup_state_for_aggregate_init( eff_context->latest_initialization, latest_initialization_on_entry, diff --git a/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp b/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp index 078f64a71c..430dc3c9f3 100644 --- a/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp +++ b/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp @@ -6,9 +6,10 @@ // Once the exception object is completely initialized, those cleanups must // no longer be active: before the fix, they were still active when the // exception was thrown, so the subobjects were destroyed during unwinding -// and again when the exception object was destroyed. +// and again when the exception object was destroyed. Temporaries with +// destructors in the initializer must still be destroyed (once). -int live, errors; +int live, tlive, errors; struct E { int v; @@ -20,20 +21,31 @@ struct Thrower { Thrower() { throw 0; } ~Thrower() { ++errors; } }; -struct T { int v; T(int x) : v(x) {} ~T() {} }; +struct T { + int v; + T(int x) : v(x) { ++tlive; } + T(const T &o) : v(o.v) { ++tlive; } + ~T() { if (--tlive < 0) ++errors; } +}; static int tv(const T &t) { return t.v; } +static E te(const T &t) { return E(t.v); } struct H2 { E a, b; }; struct H4 { E a[2]; }; struct H5 { E a[2]; E b; }; struct H6 { E a[2][2]; }; struct L { E a; E b; int i; }; +struct L2 { int i; E a; E b; }; +struct L3 { E a; int i; E b; }; +struct M { L l; E c; }; struct X { E a; Thrower t; }; +struct X2 { E a; int i; Thrower t; }; template static void run(F f) { live = 0; + tlive = 0; try { f(); } catch (...) {} - if (live != 0) ++errors; + if (live != 0 || tlive != 0) ++errors; } int main() { @@ -45,12 +57,22 @@ int main() { run([] { throw H6{{{E(7)}}}; }); run([] { throw L{E(1), E(2), tv(T(3))}; }); run([] { throw H2{E(tv(T(7)))}; }); + run([] { throw L2{tv(T(3)), E(1), E(2)}; }); + run([] { throw L3{E(1), tv(T(3)), E(2)}; }); + run([] { throw H2{te(T(7)), te(T(8))}; }); + run([] { throw H2{E(1), te(T(8))}; }); + run([] { throw H5{{E(1), te(T(2))}, te(T(3))}; }); + run([] { throw M{{E(1), E(2), tv(T(3))}, te(T(4))}; }); + run([] { throw M{{E(1), te(T(2)), tv(T(3))}, E(4)}; }); run([] { E x(1), y(2); throw [x, y] { return x.v + y.v; }; }); + run([] { E x(1); throw [x, y = te(T(2))] { return x.v + y.v; }; }); // An enclosing local must still be destroyed (once) during unwinding. run([] { E outer(1); throw H2{E(7)}; }); + run([] { E outer(1); throw L3{E(1), tv(T(3)), te(T(4))}; }); // A subobject initializer that throws: the subobjects constructed so far // must be destroyed (once), and no exception object is thrown. run([] { throw X{E(1)}; }); + run([] { throw X2{E(1), tv(T(3))}; }); // The cleanup state after a caught exception must be intact. run([] { E outer(1); From 6517dc32958e9f7215e35e477a3af66651eabe80 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 12:39:39 +0000 Subject: [PATCH 3/4] Use dyn_init_is in the thrown-aggregate cleanup branch [GH #553] Address review comments: test the dynamic initialization kind with dyn_init_is instead of comparing against casted enumerators, hyphenate "partially-constructed", and keep the first argument of the adjust_cleanup_state_for_aggregate_init call on the same line as the function name. No functional change. --- src/lower_init.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index 2bba25533f..534ee40a05 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -11101,13 +11101,13 @@ do_assignment:; dip->destructible_entity_descr = NULL; } /* if */ } else if ((options & LDIO_THROW) && - (dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate || - dip->kind == (a_dynamic_init_kind)dik_lambda) && + (dyn_init_is(dip, dik_nonconstant_aggregate) || + dyn_init_is(dip, dik_lambda)) && latest_initialization_on_entry != eff_context->latest_initialization) { /* A thrown aggregate (which has no destructor entry; the runtime destroys the exception object) has been completely initialized. - Remove the cleanups for its partially constructed subobjects from + Remove the cleanups for its partially-constructed subobjects from the cleanup state, so that they are not run (in addition to the destruction of the exception object) when it is thrown. The aggregate has no init_expr_lifetime of its own (only constructor @@ -11116,8 +11116,7 @@ do_assignment:; a_boolean some_cloned, some_ordered; check_assertion(init_expr_lifetime == NULL && curr_context == eff_context); - adjust_cleanup_state_for_aggregate_init( - eff_context->latest_initialization, + adjust_cleanup_state_for_aggregate_init(eff_context->latest_initialization, latest_initialization_on_entry, eff_insert_location, &some_cloned, From c56402e2c7fc3cda797a539f22639724ae2b35e0 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 15:46:32 +0000 Subject: [PATCH 4/4] Keep cleanups across a constant lambda in a thrown object [GH #553] For a lambda whose captures are all constant, lower_dynamic_init took the constant path without recording the cleanup state on entry, so the completion processing for a thrown aggregate compared the current state with NULL and removed cleanups recorded before the lambda, e.g., those for preceding subobjects of an enclosing aggregate. Record the entry state on the constant path as well. Reported by CodeRabbit. The regression test now uses C++17 (for class template argument deduction of aggregates with a closure member) and covers a constant lambda that is thrown, or that is a member of a thrown aggregate. --- src/lower_init.c | 5 ++++- .../throw-aggregate-subobject-cleanup.sft.cpp | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index 534ee40a05..b35115b944 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -10979,7 +10979,10 @@ do_assignment:; eff_insert_location); } /* if */ if (!dip->variant.constant.non_constant) { - /* Analogous to dik_constant. */ + /* Analogous to dik_constant. Record the entry state anyway, so + that the completion processing below sees no change (rather + than removing cleanups recorded before this initialization). */ + latest_initialization_on_entry = eff_context->latest_initialization; lower_constant(dip->variant.constant.ptr); goto do_assignment; } /* if */ diff --git a/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp b/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp index 430dc3c9f3..60d7871b75 100644 --- a/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp +++ b/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp @@ -1,5 +1,5 @@ //type:rp -//options_all:--c++14 --exceptions +//options_all:--c++17 --exceptions // Throwing an aggregate whose initialization has cleanups for partially // constructed subobjects (e.g., "throw H{E(1)}" with two class members). @@ -40,6 +40,10 @@ struct L3 { E a; int i; E b; }; struct M { L l; E c; }; struct X { E a; Thrower t; }; struct X2 { E a; int i; Thrower t; }; +template struct LF { E a; F f; int i; }; +template LF(E, F, int) -> LF; +template struct XF { E a; F f; Thrower t; }; +template XF(E, F) -> XF; template static void run(F f) { live = 0; @@ -73,6 +77,12 @@ int main() { // must be destroyed (once), and no exception object is thrown. run([] { throw X{E(1)}; }); run([] { throw X2{E(1), tv(T(3))}; }); + // A lambda whose captures are all constant is initialized as a constant + // (without cleanups of its own); that must leave the cleanups of the + // enclosing initialization alone. + run([] { throw LF{E(1), [k = 1] { return k; }, 0}; }); + run([] { throw XF{E(1), [k = 1] { return k; }}; }); + run([] { L3{E(1), (throw [k = 1] { return k; }, 0), E(2)}; }); // The cleanup state after a caught exception must be intact. run([] { E outer(1);