www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24370] New: static array values in static AA initialise to

https://issues.dlang.org/show_bug.cgi?id=24370

          Issue ID: 24370
           Summary: static array values in static AA initialise to dynamic
                    arrays
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: zxinsworld gmail.com

Creating a module-level `immutable` associative array with a static array as
its value seemingly causes it to create a dyanmic array, which it then
interprets as a static array, causing memory corruption if the static array's
length is longer than a dynamic array object.
Affects dmd 2.106.X 0–2.107.0.

Steps to reproduce...
Here's a minimal test example:
```d
immutable uint[3][string] test = [
        "oneTwoThree": [1,2,3],
        "fourFiveSix": [4,5,6],
        "sevenEightNine": [7,8,9],
];
void main(){
        import std.stdio: writeln;
        writeln(test);
}
```
When I ran it on run.dlang.io with `dmd-beta` (2.106.X), I got:
```
["sevenEightNine":[3, 0, 3620282384], "fourFiveSix":[3, 0, 3620282448],
"oneTwoThree":[3, 0, 3620282512]]
```
Uh oh! I expected:
```
["sevenEightNine":[7, 8, 9], "fourFiveSix":[4, 5, 6], "oneTwoThree":[1, 2, 3]]
```

Let's add a few more items to our static array:
```d
immutable uint[18][string] test = [
        "aaa": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18],
];
void main()  safe{
        import std.stdio: writeln;
        writeln(test);
}
```
Now I got:
```
["aaa":[18, 0, 1472507920, 21935, 0, 0, 0, 0, 3844923289, 3235698430,
1472508016, 21935, 0, 0, 0, 0, 0, 0]]
```

The very long numbers are obviously pointers, so their value is
non-deterministic.

--
Feb 04