Thread overview
[Issue 24377] Error: negative array dimension `3145728u * 1024u`[32bit]
Feb 10
kdevel
Feb 12
kdevel
February 10
https://issues.dlang.org/show_bug.cgi?id=24377

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
                 CC|                            |ibuclaw@gdcproject.org
           Severity|normal                      |regression

--- Comment #1 from Iain Buclaw <ibuclaw@gdcproject.org> ---
Behaviour changed in v2.105

--
February 10
https://issues.dlang.org/show_bug.cgi?id=24377

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=11612

--
February 10
https://issues.dlang.org/show_bug.cgi?id=24377

--- Comment #2 from Iain Buclaw <ibuclaw@gdcproject.org> ---
https://github.com/dlang/dmd/pull/15359

--
February 10
https://issues.dlang.org/show_bug.cgi?id=24377

--- Comment #3 from Iain Buclaw <ibuclaw@gdcproject.org> ---
You're allocating an array that takes up more than half the address space, so that is already pretty dubious.

Note: this is the case on 64-bit too if you request an array size bigger than `long.max`.


auto arr = new char [1u + long.max];
// Error: negative array dimension `1LU + 9223372036854775807LU`

--
February 10
https://issues.dlang.org/show_bug.cgi?id=24377

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--
February 10
https://issues.dlang.org/show_bug.cgi?id=24377

--- Comment #4 from kdevel <kdevel@vogtner.de> ---
(In reply to Iain Buclaw from comment #3)
> You're allocating an array that takes up more than half the address space, so that is already pretty dubious.
Up to about 3.5 GiB the allocation is legit in 32 bit mode. But even with a 2 GiB allocation the error shows up:

$ cat nad2g.d
import std.stdio;

void main()
{
   auto arr = new char [2u * 1024 * 1024 * 1024]; // <--- error
   writefln ("%X", arr.length);
}
$ dmd -m32 nad2g.d
nad2g.d(5): Error: negative array dimension `2097152u * 1024u`

--
February 11
https://issues.dlang.org/show_bug.cgi?id=24377

--- Comment #5 from Iain Buclaw <ibuclaw@gdcproject.org> ---
(In reply to kdevel from comment #4)
> (In reply to Iain Buclaw from comment #3)
> > You're allocating an array that takes up more than half the address space, so that is already pretty dubious.
> Up to about 3.5 GiB the allocation is legit in 32 bit mode. But even with a 2 GiB allocation the error shows up:
> 
> $ cat nad2g.d
> import std.stdio;
> 
> void main()
> {
>    auto arr = new char [2u * 1024 * 1024 * 1024]; // <--- error
>    writefln ("%X", arr.length);
> }
> $ dmd -m32 nad2g.d
> nad2g.d(5): Error: negative array dimension `2097152u * 1024u`
It is based on the error for static arrays.

```
Error: `char[cast(size_t)2147483648]` size 1 * 2147483648 exceeds 0x7fffffff
size limit for static array
```

The hard limit is the max supported static data size for the target.

GCC will even warn about doing such things (i.e: malloc/calloc).
```
warning: argument 1 value ‘2147483648’ exceeds maximum object size 2147483647
```

--
February 12
https://issues.dlang.org/show_bug.cgi?id=24377

--- Comment #6 from kdevel <kdevel@vogtner.de> ---
(In reply to Iain Buclaw from comment #5)
> > $ dmd -m32 nad2g.d nad2g.d(5): Error: negative array dimension `2097152u * 1024u`
> It is based on the error for static arrays.

What follows from that observation?

> ```
> Error: `char[cast(size_t)2147483648]` size 1 * 2147483648 exceeds 0x7fffffff
> size limit for static array
> ```
> 
> The hard limit is the max supported static data size for the target.

This issue is not about the static data size but about dynamic allocation.

> GCC will even warn about doing such things (i.e: malloc/calloc).
> ```
> warning: argument 1 value ‘2147483648’ exceeds maximum object size 2147483647
> ```

The GCC message is a warning not an error. And, what is more, a false positive one [1] [2]. If you check the return value of malloc/calloc you will find that it is not NULL. That the full 2^^32 address space is available to the user process has been discussed in [3].

[1] https://stackoverflow.com/questions/47450718/gcc7-2-argument-range-exceeds-maximum-object-size-9-7-werror-alloc-size-larg

[2] [GCC] Bug 85783 – alloc-size-larger-than fires incorrectly with new[] and
can't be disabled
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85783

[3] https://stackoverflow.com/questions/5079519/memory-limit-to-a-32-bit-process-running-on-a-64-bit-linux-os

--