D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22837 - [dip1000] checkConstructorEscape quits after first non-pointer
Summary: [dip1000] checkConstructorEscape quits after first non-pointer
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull, safe
Depends on:
Blocks:
 
Reported: 2022-03-02 16:55 UTC by Dennis
Modified: 2022-03-04 14:39 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Dennis 2022-03-02 16:55:03 UTC
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;
}
```
Comment 1 Dlang Bot 2022-03-02 16:59:01 UTC
@dkorpel created dlang/dmd pull request #13744 "Fix issue 22837 - checkConstructorEscape quits after first non-pointer" fixing this issue:

- Fix issue 22837 - checkConstructorEscape quits after first non-pointer

https://github.com/dlang/dmd/pull/13744
Comment 2 Dlang Bot 2022-03-04 14:39:40 UTC
dlang/dmd pull request #13744 "Fix issue 22837 - checkConstructorEscape quits after first non-pointer" was merged into master:

- 54ecdf46f47e41999ff4720be5fa9ea9b0c43898 by Dennis Korpel:
  Fix issue 22837 - checkConstructorEscape quits after first non-pointer

https://github.com/dlang/dmd/pull/13744