[Issue 21617] New: dmd -boundscheck=off segfault when accessing an array's index with a function
    d-bugmail at puremagic.com 
    d-bugmail at puremagic.com
       
    Sun Feb  7 18:10:28 UTC 2021
    
    
  
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 at puremagic.com
          Reporter: jlourenco5691 at 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
}
--
    
    
More information about the Digitalmars-d-bugs
mailing list