www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21617] New: dmd -boundscheck=off segfault when accessing an

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

          Issue ID: 21617
           Summary: dmd -boundscheck=off segfault when accessing an
                    array's index with a function
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: jlourenco5691 gmail.com

void main()
{
    int[] foos;

    auto foo = ()
    {
        foos ~= 0;
        return foos.length - 1;
    };

    auto f = foos[foo()]; // segfault
}

This code breaks when compiled with dmd and -boundscheck=off.
Compiling with ldc and -boundscheck=off works.
Compiling with either dmd or ldc and -boundscheck=on works.

A workaround is to store the returned value of foo into an auxiliary variable.

void main()
{
    int[] foos;

    auto foo = ()
    {
        foos ~= 0;
        return foos.length - 1;
    };

    auto aux = foo();
    auto f = foos[aux]; // ok
}

--
Feb 07