Thread overview
[Issue 23739] Can't return by ref from opApply iteration
Feb 25, 2023
timon.gehr@gmx.ch
Feb 25, 2023
timon.gehr@gmx.ch
Feb 25, 2023
kinke
Feb 27, 2023
Iain Buclaw
Feb 27, 2023
timon.gehr@gmx.ch
Feb 27, 2023
timon.gehr@gmx.ch
Feb 27, 2023
timon.gehr@gmx.ch
February 25, 2023
https://issues.dlang.org/show_bug.cgi?id=23739

timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe

--- Comment #1 from timon.gehr@gmx.ch ---
This bug can cause memory corruption in @safe code:

---
struct S{
    int* x;
    int opApply(scope int delegate(ref int* x)@safe dg)@safe{
        return dg(x);
    }
}

ref int* foo(ref S s)@safe{
    foreach(x;s)
        return x;
    assert(0);
}

void main()@safe{
    auto s=new S;
    *foo(*s)=2; // segfault
}
---

--
February 25, 2023
https://issues.dlang.org/show_bug.cgi?id=23739

timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |major

--
February 25, 2023
https://issues.dlang.org/show_bug.cgi?id=23739

kinke <kinke@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kinke@gmx.net

--- Comment #2 from kinke <kinke@gmx.net> ---
The 1st testcase just requires a `foreach (ref x; s)`, which I think is
consistent.

The 2nd one never sets `S.x` before dereferencing it, so adding `ref` there still causes a segfault.

--
February 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23739

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P2

--
February 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23739

timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from timon.gehr@gmx.ch ---
(In reply to kinke from comment #2)
> The 1st testcase just requires a `foreach (ref x; s)`, which I think is
> consistent.
> 
> The 2nd one never sets `S.x` before dereferencing it, so adding `ref` there still causes a segfault.

Oops. Indeed I screwed up when trying to create a reduced test case... My bad. Thanks!

--
February 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23739

timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=23751

--
February 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23739

--- Comment #4 from timon.gehr@gmx.ch ---
(In reply to kinke from comment #2)
> The 1st testcase just requires a `foreach (ref x; s)`, which I think is
> consistent.
> 
> The 2nd one never sets `S.x` before dereferencing it, so adding `ref` there still causes a segfault.

Oops. Indeed I screwed up when trying to create a reduced test case... My bad. Thanks!(In reply to timon.gehr from comment #3)
> (In reply to kinke from comment #2)
> > The 1st testcase just requires a `foreach (ref x; s)`, which I think is
> > consistent.
> > 
> > The 2nd one never sets `S.x` before dereferencing it, so adding `ref` there still causes a segfault.
> 
> Oops. Indeed I screwed up when trying to create a reduced test case... My bad. Thanks!

See Issue 23751.

--