[Issue 12857] New: Don't allow declaring @system function inside @safe block

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jun 4 20:33:55 PDT 2014


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: nobody at puremagic.com
          Reporter: k.hara.pg at gmail.com

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!
}

--


More information about the Digitalmars-d-bugs mailing list