Bug 4232 – Local struct member variable can be returned by ref
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2010-05-24T23:21:00Z
Last change time
2015-01-11T05:44:21Z
Keywords
wrong-code
Assigned to
nobody
Creator
rsinfu
Comments
Comment #0 by rsinfu — 2010-05-24T23:21:52Z
Using struct members bypasses the compiler check for escaping reference to local variables.
This code should not compile:
--------------------
struct S { int a; }
ref int test()
{
S s;
s.a = 42;
return s.a;
}
void main()
{
auto p = &test(); // reference to the local variable
assert(test() == 42); // assertion fails with -O
}
--------------------
Moreover, test() in the above example returnes a corrupted (or uninitialized) value when optimization is on. This causes unittests for std.functional.toDelegate() to segfault.
Comment #1 by k.hara.pg — 2015-01-11T05:44:21Z
*** This issue has been marked as a duplicate of issue 13902 ***