Bug 22298 – [DIP1000] Nested function's scope parameters can be assigned to variables in enclosing function

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-09-10T21:16:45Z
Last change time
2022-01-14T16:49:40Z
Keywords
pull, safe, Vision
Assigned to
No Owner
Creator
giacomo.ratto

Comments

Comment #0 by giacomo.ratto — 2021-09-10T21:16:45Z
This makes possible to get a pointer to a local variable within an expired stack frame: --- void g(scope void delegate(scope int*) @safe cb) @safe { int x = 42; cb(&x); } void main() @safe { int* p; void f(scope int* i) @safe { p = i; } g(&f); // address of x has escaped g assert(*p == 42); } ---
Comment #1 by dkorpel — 2022-01-12T16:28:15Z
The problem is that `VarDeclaration.enclosesLifetimeOf(VarDeclaration v)` in declaration.d considers parameters to always have a longer lifetime than local variables, without considering if the parameter and local variable actually belong to the same function.
Comment #2 by dlang-bot — 2022-01-13T19:43:41Z
@dkorpel created dlang/dmd pull request #13530 "Fix issue 22298 - Nested function's scope parameters can be assigned to variables in enclosing function" fixing this issue: - Fix issue 22298 - Nested function's scope parameters can be assigned to variables in enclosing function https://github.com/dlang/dmd/pull/13530
Comment #3 by dlang-bot — 2022-01-14T16:49:40Z
dlang/dmd pull request #13530 "Fix issue 22298 - Nested function's scope parameters can be assigned to variables in enclosing function" was merged into master: - 87226872ebb9d218d21d14350f22748783ac9176 by dkorpel: Fix issue 22298 - Nested function's scope parameters can be assigned to variables in enclosing function https://github.com/dlang/dmd/pull/13530