westes / flex

The Fast Lexical Analyzer - scanner generator for lexing in C and C++
Other
3.55k stars 529 forks source link

Flex fails to compile on UBI8 image with LLVM15+ #578

Open interistaadastra opened 1 year ago

interistaadastra commented 1 year ago

When compiling flex using LLVM 15 or 16 (built from source, although I'm guessing an install from dnf would present the same issue) on the docker image for UBI8, flex fails to compile due to the following error:

misc.c:664:14: warning: call to undeclared function 'reallocarray'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        new_array = reallocarray(array, (size_t) size, element_size);
                    ^
misc.c:664:12: error: incompatible integer to pointer conversion assigning to 'void *' from 'int' [-Wint-conversion]
        new_array = reallocarray(array, (size_t) size, element_size);

The use of reallocarray itself is not the problem, rather it's the fact that the function is undeclared and thus the compiler assumes the return type is int. The problem appears to be here (flexdef.h):

#ifndef HAVE_REALLOCARRAY
void *reallocarray(void *, size_t, size_t);
#endif

If you look at the other uses of HAVE_REALLOCARRAY:

#if HAVE_REALLOCARRAY
    /* reallocarray has built-in overflow detection */
    mem = reallocarray(NULL, (size_t) size, element_size);
#else

this usage is wrong. I believe it should be

#if HAVE_REALLOCARRAY
void *reallocarray(void *, size_t, size_t);
#endif

In fact making this change allows flex to compile once more.

westes commented 1 year ago

Please submit a pull request with your proposed change.