D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21664 - Unsafe global initializer causes undefined behavior in @safe code
Summary: Unsafe global initializer causes undefined behavior in @safe code
Status: RESOLVED DUPLICATE of issue 19646
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: safe
Depends on:
Blocks:
 
Reported: 2021-02-26 02:08 UTC by Paul Backus
Modified: 2023-02-16 06:05 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Paul Backus 2021-02-26 02:08:34 UTC
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 Paul Backus 2023-02-16 06:05:18 UTC

*** This issue has been marked as a duplicate of issue 19646 ***