Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc7.2: argument range exceeds maximum object size 9..7 [-Werror=alloc-size-larger-than=]

Tags:

c

calloc

gcc7

The program contains code like follows:

int size;
...
int *pi = (int*)calloc(size, sizeof(int));
...

Here is the error message when compiled with gcc7.2:

error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]

When I change
int *pi = (int*)calloc(size, sizeof(int)); to
int *pi = (int*)calloc((unsigned int)size, sizeof(int));

The error disappeared.

However, in the program, there are many malloc and calloc used like my original version.

Why there is only one error detected by gcc?

like image 487
Yuan Wen Avatar asked Jan 30 '23 04:01

Yuan Wen


1 Answers

I recently had the same problem on my GCC 9.1 build, and I found this discussion on the GCC Bugzilla:

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

As mentioned in the link discussion, I was able to suppress the warning by checking the size parameter against PTRDIFF_MAX.

like image 53
Kurt Avatar answered May 11 '23 01:05

Kurt