Skip to content

Commit

Permalink
fix Issue 14238 - DIP25: escape checks can be circumvented with delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Nov 1, 2016
1 parent eb4d6fe commit 7c3e47a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/escape.d
Expand Up @@ -742,6 +742,17 @@ private void escapeByRef(Expression e, VarDeclarations* byref, VarDeclarations *
Parameter p = Parameter.getNth(tf.parameters, i - j);
if ((p.storageClass & (STCout | STCref)) && (p.storageClass & STCreturn))
arg.accept(this);
else if ((p.storageClass & STCscope) && (p.storageClass & STCreturn))
{
if (arg.op == TOKdelegate)
{
DelegateExp de = cast(DelegateExp)arg;
if (de.func.isNested())
byexp.push(de);
}
else
escapeByValue(arg, byref, byvalue, byexp);
}
}
}
}
Expand All @@ -752,6 +763,11 @@ private void escapeByRef(Expression e, VarDeclarations* byref, VarDeclarations *
if (dve.var.storage_class & STCreturn || tf.isreturn)
dve.e1.accept(this);
}
// If it's a delegate, check it too
if (e.e1.op == TOKvar && t1.ty == Tdelegate)
{
escapeByValue(e.e1, byref, byvalue, byexp);
}
}
else
byexp.push(e);
Expand All @@ -762,3 +778,4 @@ private void escapeByRef(Expression e, VarDeclarations* byref, VarDeclarations *
e.accept(v);
}


30 changes: 30 additions & 0 deletions test/fail_compilation/test14238.d
@@ -0,0 +1,30 @@
/* REQUIRED_ARGS: -dip25
PERMUTE_ARGS:
TEST_OUTPUT:
---
fail_compilation/test14238.d(21): Error: scope variable fn may not be returned
fail_compilation/test14238.d(29): Error: escaping reference to stack allocated value returned by &baz
---
*/
// https://issues.dlang.org/show_bug.cgi?id=14238

@safe:

alias Fn = ref int delegate();

ref int foo(return scope Fn fn)
{
return fn(); // Ok
}

ref int foo2(scope Fn fn) {
return fn(); // Error
}

ref int bar() {
int x;
ref int baz() {
return x;
}
return foo(&baz);
}

0 comments on commit 7c3e47a

Please sign in to comment.