Bug 21664 – Unsafe global initializer causes undefined behavior in @safe code
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-02-26T02:08:34Z
Last change time
2023-02-16T06:05:18Z
Keywords
safe
Assigned to
No Owner
Creator
Paul Backus
Comments
Comment #0 by snarwin+bugzilla — 2021-02-26T02:08:34Z
As of DMD 2.095.0, the following program compiles and exhibits undefined behavior at runtime:
---
int* p = cast(int*) 0xDEADBEEF;
void main() @safe
{
int n = *p;
}
---
To prevent this, the compiler must forbid access to global variables in @safe code unless it can prove that they have safe values. [1]
[1] https://dlang.org/spec/function.html#safe-values
Comment #1 by snarwin+bugzilla — 2023-02-16T06:05:18Z
*** This issue has been marked as a duplicate of issue 19646 ***