This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/87996] New: [8/9 Regression] "size of array is negative" error when SIZE_MAX/2 < sizeof(array) <= SIZE_MAX


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87996

            Bug ID: 87996
           Summary: [8/9 Regression] "size of array is negative" error
                    when SIZE_MAX/2 < sizeof(array) <= SIZE_MAX
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jozef.l at mittosystems dot com
  Target Milestone: ---

Created attachment 44992
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44992&action=edit
tester.ii

In GCC 7.3, declaring an array of size SIZE_MAX results in an error because 
the size of the array is too large.

tester.c:
  #include <stdint.h>
  char array[SIZE_MAX];

gcc tester.c
> tester.c:3:6: error: size of array 'array' is too large
>  char array[SIZE_MAX];

g++ tester.c
> tester.c:3:19: error: size of array 'array' is too large
>  char array[SIZE_MAX];

In GCC 8.1 and current trunk, the error message when compiling C code is
the same, but for C++, the size of the array is reported to be negative.

gcc tester.c
> tester.c:3:6: error: size of array ‘array’ is too large
>   3 | char array[SIZE_MAX];

g++ tester.c
> tester.c:3:19: error: size of array ‘array’ is negative
>   3 | char array[SIZE_MAX];

For G++ I tried with -std=c++{98,11,14} and the behaviour was the same.
For SIZE_MAX/2, compilation completes succesfully, and the same errors occur
for (SIZE_MAX/2+1)

According to cppreference (https://en.cppreference.com/w/cpp/language/array)
the array size initializer (i.e. the value within []), is either an integral
constant expression (until C++14), or a constant expression of type size_t.

So in none of these cases should the size of the array be considered to be
negative as SIZE_MAX will always fit in a integral constant expression, or
size_t, so should never wrap to a negative value.

I've attached the pre-processed source applicable for 64-bit size_t.

Observed with x86_64-pc-linux-gnu and msp430-elf

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]