Bug 20347 – Initialization of globals not checked for @safe, round 2

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-11-02T09:39:39Z
Last change time
2023-04-18T14:10:46Z
Keywords
safe
Assigned to
No Owner
Creator
ag0aep6g

Comments

Comment #0 by ag0aep6g — 2019-11-02T09:39:39Z
This was found by dkorpel while working on a DIP: https://github.com/dlang/DIPs/blob/7b109744db7fd0cfea9904354613a50e7dbdad08/DIPs/DIP1NNN-DK.md#existing-holes-in-safe The fix for issue 19646 outlaws this code: ---- @safe: const x = 42; int* y = cast(int*) &x; /* Error: cast from const(int)* to int* not allowed in safe code */ void main() { *y = 7; } ---- But the following two slight variations still pass. 1) Applying `@safe` individually: ---- @safe const x = 42; @safe int* y = cast(int*) &x; /* Should be rejected. */ @safe void main() { *y = 7; } ---- 2) Calling an @system function in the initializer: ---- @system int* f() { return cast(int*) &x; } @safe: const x = 42; int* y = f(); /* Should be rejected. */ void main() { *y = 7; } ----
Comment #1 by razvan.nitu1305 — 2023-04-18T09:04:24Z
Both cases seem to have been fixed. For 1) I get: test.d(2): Error: cast from `const(int)*` to `int*` not allowed in safe code For 2) I get: test.d(4): Error: `@safe` variable `y` cannot be initialized by calling `@system` function `f` Closing as WORKSFORME.
Comment #2 by dkorpel — 2023-04-18T14:10:46Z
Yes, this was fixed by https://github.com/dlang/dmd/pull/14483 I forgot to link the issue