[Issue 23445] Can leak scope variable through delegate context
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Nov 1 00:33:26 UTC 2022
https://issues.dlang.org/show_bug.cgi?id=23445
Walter Bright <bugzilla at digitalmars.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
CC| |bugzilla at digitalmars.com
Resolution|--- |INVALID
--- Comment #1 from Walter Bright <bugzilla at digitalmars.com> ---
Reducing to:
int global;
int* foo(scope int* p)@safe{
auto dg=(return scope int* q)@safe return scope{
return p;
};
return dg(&global); <== returns a pointer to p
}
This is not a problem, because the compiler recognizes the escape of p and
allocates p in a closure on the heap. This heap allocate is done whenever a
nested function is turned into a delegate.
For nested functions not turned into a delegate, p is placed in a heap
allocated closure, and an error is produced for `return dg(&global);`
--
More information about the Digitalmars-d-bugs
mailing list