[Issue 24442] [DIP1000] struct member slice cannot point to other struct member
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Mar 19 20:37:45 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=24442
Dennis <dkorpel at live.nl> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
CC| |dkorpel at live.nl
Resolution|--- |INVALID
--- Comment #1 from Dennis <dkorpel at live.nl> ---
Interior pointers aren't safe, the error is correct. Without it, you can escape
stack pointers.
```
@safe:
struct S {
int[10] array;
int[] slice;
void foo() @trusted {
slice = array[];
}
}
void main()
{
int[] f()
{
S s;
s.foo();
return s.slice;
}
auto x = f(); // dangling pointer
x[0] = 0xAAAA;
f(); // stomp stack
assert(x[0] == 0xAAAA); // fails
}
```
Even when foo becomes `scope`:
```
@safe:
struct S {
int[10] array;
int[] slice;
void foo() scope @trusted {
slice = array[];
}
}
void main()
{
S s;
int[] f(return scope S s)
{
s.foo();
return s.slice;
}
auto x = f(s); // dangling pointer
x[0] = 0xAAAA;
f(s); // stomp stack
assert(x[0] == 0xAAAA); // fails
}
```
--
More information about the Digitalmars-d-bugs
mailing list