https://issues.dlang.org/show_bug.cgi?id=12857

          Issue ID: 12857
           Summary: Don't allow declaring @system function inside @safe
                    block
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: major
          Priority: P1
         Component: DMD
          Assignee: [email protected]
          Reporter: [email protected]

Because it would break memory safety. See example code:

auto func(int n) @safe {
    static int* ptr;
    if (!ptr)
        ptr = new int(n);

    static void foo() @system {
        ptr = cast(int*)1;  // stomp memory
    }
    return &foo;
}

void main() {
    auto fp = func(1);
    func(1);
    fp();       // break 'ptr' in @safe code
    func(1);    // crash!
}

--

Reply via email to