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

[亡魂溺海] 提交于 2019-12-24 01:44:11

问题


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?


回答1:


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.




回答2:


The warning depends on the range that GCC thinks size has. At that particular point in the program it is deemed to be in that (exceedingly large) range. At the other malloc/calloc callsites perhaps it wasn't so large.

It would largely depend on how size is calculated at different points in the program. Of course, making sure that it is actually initialised before any use is the first step.



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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!