www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12857] New: Don't allow declaring system function inside

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 puremagic.com
          Reporter: k.hara.pg 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!
}

--
Jun 04 2014