diff --git a/src/lower_init.c b/src/lower_init.c index 156d63c8cc..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 */ @@ -11100,6 +11103,32 @@ do_assignment:; free_destructible_entity_descr(dip->destructible_entity_descr); dip->destructible_entity_descr = NULL; } /* if */ + } else if ((options & LDIO_THROW) && + (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 + 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 + 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, + 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..60d7871b75 --- /dev/null +++ b/tests/tests/regressions/throw-aggregate-subobject-cleanup.sft.cpp @@ -0,0 +1,95 @@ +//type:rp +//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). +// 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. Temporaries with +// destructors in the initializer must still be destroyed (once). + +int live, tlive, 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) { ++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 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; + tlive = 0; + try { f(); } catch (...) {} + if (live != 0 || tlive != 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([] { 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))}; }); + // 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); + 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; +}