[Issue 22837] New: [dip1000] checkConstructorEscape quits after first non-pointer

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 2 16:55:03 UTC 2022


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

          Issue ID: 22837
           Summary: [dip1000] checkConstructorEscape quits after first
                    non-pointer
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: safe
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: dkorpel at live.nl

dmd checks escaping pointers of a constructor by fake assigning arguments to
`this`, but it stops at the first non pointer argument.
```
bool checkConstructorEscape() {
// ...
    /* Attempt to assign each `return` arg to the `this` reference
     */
    foreach (const i; 0 .. n)
    {
        Expression arg = (*ce.arguments)[i];
        if (!arg.type.hasPointers())
            return false; // < EARLY EXIT
```

This means you can escape scope pointers like this:
```
@safe:
struct S 
{
    int* p;

    this(int dummy, return scope int* p) 
    {
        this.p = p;
    }
}

int* escape()
{
    int x;
    auto s = S(0, &x);
    return s.p;
}
```

--


More information about the Digitalmars-d-bugs mailing list