Bug 14643 – Safety violation with final switch and void initializer

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2015-06-02T18:08:55Z
Last change time
2023-01-29T12:19:35Z
Keywords
safe
Assigned to
No Owner
Creator
Dicebot
See also
https://issues.dlang.org/show_bug.cgi?id=11051

Comments

Comment #0 by public — 2015-06-02T18:08:55Z
This compiles: ``` enum E { A, B } int* foo() @safe { E value = void; static int a, b; final switch (value) { case E.A: return &a; case E.B: return &b; } } void main() @safe { *foo() = 43; } ``` Throws core.exception.SwitchError in normal mode but crashes in release mode by trying to write via garbage pointer.
Comment #1 by public — 2015-06-02T18:10:21Z
correction : foo() needs to be called twice to start getting garbage on stack
Comment #2 by nick — 2023-01-21T16:47:32Z
Note that an enum E is allowed to take values larger than E.max using binary operations: enum E { a, b} pragma(msg, E.max); // E.b which is 1 E value = E.b << E.b; // 2 So that violates `final switch` anyway, even in @safe code, and casting can (see https://issues.dlang.org/show_bug.cgi?id=11051#c7). Though void initialization is worse because the bug might not even be reproducible. Sadly void initialization is not banned in @safe.
Comment #3 by nick — 2023-01-21T16:53:26Z
> the bug might not even be reproducible Oops, the original bug also violates safety anyway. The above comment combined with the original example means that any enum value outside its min and max properties can produce an uninitialized pointer in safe code, void initialization is not required. The solution seems to be to fix issue 11051.
Comment #4 by nick — 2023-01-29T12:19:35Z
*** This issue has been marked as a duplicate of issue 11051 ***