Clang optimization results in inconsistent XORG_CHECK_MALLOC_ZERO behavior
Hello!
I encountered an issue when attempting to build libXt-1.3.0 using clang version 17. The build process failed due to the use of XORG_CHECK_MALLOC_ZERO
in xorg-macros
to determine whether to define MALLOC_0_RETURNS_NULL
or not. The project is being compiled with the -O2
optimization level. As is known, clang optimizes malloc
calls at optimization levels -O1
and above.
To illustrate this inconsistency, consider the following code snippet from ./configure
:
#include <stdlib.h>
int
main(void)
{
char *m0, *r0, *c0, *p;
m0 = malloc(0);
p = malloc(10);
r0 = realloc(p, 0);
c0 = calloc(0, 10);
exit((m0 == NULL || r0 == NULL || c0 == NULL) ? 0 : 1);
return 0;
}
When compiling with optimization level of -O1
or higher, GCC exits with 0, whereas Clang exits with 1 (compiling with -O0
results in an exit code of 0).(In [https://gcc.godbolt.org/z/ofoPsMvsf], you can see that the key lines are optimized to nothing, which causes the inconsistency.)
Could you please address this issue within the script? For snippet above, a potential solution would be to add __attribute__((optnone))
before the main
function declaration. However, I'm unsure how to implement this fix in the xorg-macros.m4.in
.
Thank you for your consideration!