www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24162] New: Another example of why safe is broken

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

          Issue ID: 24162
           Summary: Another example of why  safe is broken
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: tomerfiliba gmail.com

have a look at this snippet - all  safe, uses GC and no pointers:
https://run.dlang.io/is/KGbaEd

```
 safe:
import std.stdio: writeln;

void f(ref int x, ref int[] arr) {
    x += 1;                  // this happens on arr[2]
    arr ~= new int[900000];  // array is relocated
    x += 1;                  // this now modifies random memory and will not be
reflected on arr
}

void main() {
    auto arr = new int[10];
    f(arr[2], arr);
    writeln(arr[0..10]);
}
```

it passes two mutable references to the array, which means the array can be
relocated while a reference exists

--
Sep 25 2023