Bug 21617 – dmd -boundscheck=off segfault when accessing an array's index with a function

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2021-02-07T18:10:28Z
Last change time
2024-12-13T19:14:29Z
Keywords
backend
Assigned to
No Owner
Creator
João Lourenço
Moved to GitHub: dmd#19866 →

Comments

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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19866 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB