www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21675] New: Unsafe aggregate field initializer causes


          Issue ID: 21675
           Summary: Unsafe aggregate field initializer causes undefined
                    behavior in  safe code
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

As of DMD 2.095.0, the following program compiles and exhibits undefined
behavior at runtime:

struct S
    int* p = cast(int*) 0xDEADBEEF;

 safe void main()
    S s;
    int n = *s.p;

To prevent this, the compiler must either forbid default initialization of
types with unsafe `.init` values, or enforce that all default initializers are
safe values. [1]

Related: issue 21664.

[1] https://dlang.org/spec/function.html#safe-values

Mar 02 2021