Jump to page: 1 2
Thread overview
[Issue 23170] Array literal passed to map in lambda, then returned from nested function, is memory corrupted
Jun 07, 2022
FeepingCreature
Jun 07, 2022
FeepingCreature
Jun 07, 2022
FeepingCreature
Jun 07, 2022
Mario Kroeplin
Jun 07, 2022
Iain Buclaw
Jun 07, 2022
Iain Buclaw
Jun 07, 2022
Iain Buclaw
Jun 07, 2022
Iain Buclaw
Jun 07, 2022
Dlang Bot
Jun 08, 2022
Dlang Bot
Jul 09, 2022
Dlang Bot
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

--- Comment #1 from FeepingCreature <default_357-line@yahoo.de> ---
(happens with void main() @safe as well)

--
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

--- Comment #2 from FeepingCreature <default_357-line@yahoo.de> ---
Even weirder reproduction!

void main()
{
    test(({ return badAlias([1, 2, 3]); })());
}

void test(int[] array)
{
    assert(array[0] == 1);
    assert(array[1] == 2);
    assert(array[2] == 3);
}

alias badAlias = (int[] array) => id(array);

int[] id(int[] array) { return array; }

--
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

--- Comment #3 from FeepingCreature <default_357-line@yahoo.de> ---
It seems like DMD thinks it can stack allocate the array literal?

--
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

Mario Kroeplin <kroeplin.d@googlemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |industry
                 CC|                            |kroeplin.d@googlemail.com

--
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw@gdcproject.org

--- Comment #4 from Iain Buclaw <ibuclaw@gdcproject.org> ---
(In reply to FeepingCreature from comment #3)
> It seems like DMD thinks it can stack allocate the array literal?
Yes.

--
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

--- Comment #5 from Iain Buclaw <ibuclaw@gdcproject.org> ---
(In reply to FeepingCreature from comment #0)
> Consider this code:
> 
> import std.algorithm;
> import std.conv;
> import std.range;
> 
> void main()
> {
>     auto result = ({ return [1, 2, 3].badMap; })().array;
> 
>     assert(result == [1, 2, 3], result.to!string);
> }
> 
> alias badMap = range => range.map!(a => a);
This is the original, but without imports (could use as an extra test).

---
template map(fun...)
{
    auto map(int[] r)
    {
        static struct MapResult(alias fun)
        {
            this(int[] input) { _input = input; }
            auto length() { return _input.length; }
            bool empty() { return !_input.length; }
            auto ref front() { return fun(_input[0]); }
            void popFront() { _input = _input[1 .. $]; }
            private int[] _input;
        }
        return MapResult!(fun)(r);
    }
}

auto array(Range)(Range r)
{
    const length = r.length;
    if (length == 0) return null;
    int[] result;
    result.length = 3;
    size_t i;
    foreach (e; r)
    {
        result[i] = e;
        ++i;
    }
    assert(i == length);
    return (() @trusted => cast(int[]) result)();
}

void main()
{
    auto result = [1, 2, 3].badMap.dgify()().array;
    assert(result == [1, 2, 3]);
}

alias badMap = range => range.map!(a => a);
alias dgify = (lazy value) => &value;

--
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

Iain Buclaw <ibuclaw@gdcproject.org> changed:

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

--- Comment #6 from Iain Buclaw <ibuclaw@gdcproject.org> ---
Probably introduced by https://github.com/dlang/dmd/pull/11039

--
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

--- Comment #7 from Iain Buclaw <ibuclaw@gdcproject.org> ---
Fixed in master by https://github.com/dlang/dmd/pull/14089

--
June 07, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #8 from Dlang Bot <dlang-bot@dlang.rocks> ---
@dkorpel created dlang/dmd pull request #14193 "Fix Issue 23170 - Array literal passed to map in lambda, then returneā€¦" fixing this issue:

- Fix Issue 23170 - Array literal passed to map in lambda, then returned from nested function, is memory corrupted

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

--
June 08, 2022
https://issues.dlang.org/show_bug.cgi?id=23170

Dlang Bot <dlang-bot@dlang.rocks> changed:

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

--- Comment #9 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #14193 "Fix Issue 23170 - Array literal passed to map in lambda, then returneā€¦" was merged into stable:

- f192ae3d24d73c566754c5eb2e14dcdd1dbe5549 by dkorpel:
  Fix Issue 23170 - Array literal passed to map in lambda, then returned from
nested function, is memory corrupted

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

--
« First   ‹ Prev
1 2