[Issue 23445] Can leak scope variable through delegate context

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 1 10:56:14 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=23445

Tejas_Garhewal <scienticman at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |scienticman at gmail.com

--- Comment #3 from Tejas_Garhewal <scienticman at gmail.com> ---
The following code (It's timon's except it calls a couple more intermediate
functions):

```d
import std.stdio:writeln;

int global;
int* foo(scope int* p)@safe{
    auto dg=(return scope int* q)@safe return scope{
        return p;
    };
    return dg(&global);
}

auto qux()@safe{
    int x=10;
    int* p=&x;
    int* q=foo(p);
    return q;
}

void func(int[6] a) @safe{
    int[6] b = a.dup;
    int[6] c = a.dup;
    writeln(a, b, c);
}

void main()@safe{
    import std.stdio;
    auto p=qux();
    version(SMASH_STACK) writeln("smashing stack");
    func([1,2,3,4,5,6]);
    func([0,8,2,0,9,7]);
    writeln(*p);
    func([0,8,2,9,6,1]);
    writeln(*p);
}
```

When executing via normal dmd, the output is:

smashing stack
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]
[0, 8, 2, 0, 9, 7][0, 8, 2, 0, 9, 7][0, 8, 2, 0, 9, 7]
-960565922
[0, 8, 2, 9, 6, 1][0, 8, 2, 9, 6, 1][0, 8, 2, 9, 6, 1]
-960565848                                              <----- Notice the value
changing if read after invoking another function


When executed via dmd-beta, the output is:

smashing stack
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]
[0, 8, 2, 0, 9, 7][0, 8, 2, 0, 9, 7][0, 8, 2, 0, 9, 7]
-1630675618
[0, 8, 2, 9, 6, 1][0, 8, 2, 9, 6, 1][0, 8, 2, 9, 6, 1]
-1630675544                                           <-------- Ditto

When executed via ldc, the output is:

smashing stack
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]
[0, 8, 2, 0, 9, 7][0, 8, 2, 0, 9, 7][0, 8, 2, 0, 9, 7]
21942
[0, 8, 2, 9, 6, 1][0, 8, 2, 9, 6, 1][0, 8, 2, 9, 6, 1]
21942                                                <------- Here it doesn't 

ldc-beta output:

smashing stack
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]
[0, 8, 2, 0, 9, 7][0, 8, 2, 0, 9, 7][0, 8, 2, 0, 9, 7]
22061
[0, 8, 2, 9, 6, 1][0, 8, 2, 9, 6, 1][0, 8, 2, 9, 6, 1]
22061                                                <------- Neither over here


Surely this is not expected behaviour?

--


More information about the Digitalmars-d-bugs mailing list