Bug 19512 – Exception during scope(exit) of an exception yields undefined behavior
Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-12-24T23:58:20Z
Last change time
2023-04-25T14:05:56Z
Assigned to
No Owner
Creator
Eyal
Comments
Comment #0 by eyal — 2018-12-24T23:58:20Z
An exception during scope(exit) handler of an already-propagating exception does not get handled correctly. Under some circumstances, it triggers missing destructor bugs too.
Example program:
// use puts, not writeln, because puts is nothrow. any code that may throw prevents some of the bug from reproducing
import core.stdc.stdio: puts;
struct C {
this(int x) {}
~this() { puts("C.~this()"); }
}
struct D {
auto c() { return C(1); }
}
D d;
void foo() {
with(d.c) {
puts("This code is reached");
// The exception here seems to long-jump back to the original exception propagation
try { throw new Exception("Exc"); }
catch(Exception e) { puts("This handler is never reached"); }
// Iff ONLY nothrow things are done below here, then d.c's dtor does not run at all!
// THIS CODE IS NOT REACHED:
puts("This code is NOT reached, and C's destructor never runs!");
}
}
void main() {
scope(exit) { foo(); }
throw new Throwable("Bomb");
}
Comment #1 by razvan.nitu1305 — 2023-04-25T14:05:56Z
Using the latest version git master, I am now getting:
This code is reached
This handler is never reached
This code is NOT reached, and C's destructor never runs!
C.~this()
object.Throwable@(0): Bomb
This looks like the correct behavior, so it seems this has been fixed.