https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118224
Bug ID: 118224
Summary: [15 Regression] Incorrect optimization with calloc
when the size exceeds SIZE_MAX
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: luigighiron at gmail dot com
Target Milestone: ---
The following program should always succeed:
#include<assert.h>
#include<stdint.h>
#include<stdlib.h>
int main(void){
assert(!calloc(SIZE_MAX,2));
}
> The calloc function returns either a pointer to the allocated space or a null
> pointer if the space cannot be allocated or if the product nmemb * size would
> wraparound size_t.
Section 7.24.3.1 "The aligned_alloc function" Paragraph 3 ISO/IEC 9899:2024
When compiling with -O or higher levels of optimization in GCC 15, this
triggers the assert even though calloc(SIZE_MAX,2) has to return null. Without
optimizations or when compiling with an earlier compiler this code does not
trigger the assertion.