Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_d_newarraymTX can be with more dimensions than in the type #17213

Open
dlangBugzillaToGithub opened this issue Apr 19, 2024 · 0 comments
Open
Labels

Comments

@dlangBugzillaToGithub
Copy link

Tim reported this on 2024-04-19T20:23:01Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=24514

Description

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
```
@thewilsonator thewilsonator added Druntime Specific to druntime and removed OS:Linux Arch:x86_64 Issues specific to x86_64 labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants