Bug 8538 – scope parameters escaping via closure

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-08-11T12:23:47Z
Last change time
2024-12-13T18:00:58Z
Keywords
pull, safe
Assigned to
No Owner
Creator
Adam D. Ruppe
See also
https://issues.dlang.org/show_bug.cgi?id=22095, https://issues.dlang.org/show_bug.cgi?id=20956
Moved to GitHub: dmd#18459 →

Comments

Comment #0 by destructionator — 2012-08-11T12:23:47Z
=== void delegate() test(Object[] objs...) { return { foreach(obj; objs) assert(obj); }; } void delegate() foo() { return test(new Object, new Object); } void main() { auto ok = test(new Object, new Object); ok(); // no problem auto it = foo(); // but if we build the array elsewhere it(); // this triggers a failure } === My guess is the arguments are on the enclosing stack, so the ok() call is fine, because the test call is still there. But, the foo call leaves that [new Object, new Object] on the stack, which is now potentially invalid. Other local variables are copied in this situation for a closure, but it looks like the variadic arg case was overlooked. Marked as minor because it is very easy to work around and this combination of features is super rare anyway.
Comment #1 by dkorpel — 2022-04-03T15:23:08Z
Bumping the priority, because this is a memory safety issue not caught by @safe / dip1000: ``` @safe: void delegate() @safe test(Object[] objs...) { return () {assert(objs[0]); assert(objs[1]);}; } void delegate() @safe foo() { return test(new Object, new Object); } void main() { auto it = foo(); // but if we build the array elsewhere it(); // this triggers a failure } ```
Comment #2 by bugzilla — 2022-07-28T04:56:22Z
A simpler version of the same problem: --- @safe int delegate() test(scope int* p) { return () { return *p; }; } --- `p` is placed in a dynamically allocated closure, which then escapes via the returned delegate. The fix is to not allow scope variables to be put in a dynamic closure.
Comment #3 by bugzilla — 2022-07-28T05:20:40Z
Furthermore, a variable cannot be inferred as `scope` if it is referenced in a dynamic closure.
Comment #4 by dlang-bot — 2022-08-12T01:58:48Z
@WalterBright created dlang/dmd pull request #14363 "fix Issue 8538 - scope parameters escaping via closure" fixing this issue: - fix Issue 8538 - scope parameters escaping via closure https://github.com/dlang/dmd/pull/14363
Comment #5 by bugzilla — 2022-08-12T04:55:53Z
*** Issue 22095 has been marked as a duplicate of this issue. ***
Comment #6 by robert.schadek — 2024-12-13T18:00:58Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18459 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB