Comment #0 by jlourenco5691 — 2021-02-07T18:10:28Z
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
}
Comment #1 by ag0aep6g — 2021-09-09T10:51:37Z
Reduced:
----
int[1] a13 = [13];
int[1] a42 = [42];
int[] slice;
size_t foo()
{
slice = a42[];
return 0;
}
int main()
{
slice = a13[];
return slice[foo()];
}
----
With `-boundscheck=off`, the program returns 13. With bounds checking, it returns 42.
Looks like `-boundscheck` affects the order of evaluation.
Comment #2 by robert.schadek — 2024-12-13T19:14:29Z