Bug 24514 – _d_newarraymTX can be with more dimensions than in the type

Status
NEW
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2024-04-19T20:23:01Z
Last change time
2024-12-07T13:43:26Z
Assigned to
No Owner
Creator
Tim
Moved to GitHub: dmd#17213 →

Comments

Comment #0 by tim.dlang — 2024-04-19T20:23:01Z
Function _d_newarraymTX is internally used for expressions like `new int[][](2, 3)`. The function is @trusted, but it can be called with more dimensions at runtime than the type has: ``` import std.stdio; void main() @safe { auto a = _d_newarraymTX!(int*[][], int)([1, 1, 42]); writeln(a); // prints [[42]] } ``` The function internally allocates an array with the dimensions `[1][1][42]`, but then returns it as type `int*[][]`. The length of the inner array is now interpreted as a pointer. See also the discussion in https://github.com/dlang/dmd/pull/16400 A solution could be to change the function signature to this: ``` Tarr _d_newarraymTX(Tarr : U[], size_t N, U)(size_t[N] dims, bool isShared=false) @trusted ```
Comment #1 by robert.schadek — 2024-12-07T13:43:26Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17213 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB