> From: Martin Dorey <[email protected]>
> CC: "[email protected]" <[email protected]>
> Date: Fri, 9 Jun 2023 06:32:28 +0000
> msip_labels:
>
> #include <stdint.h>
>
> intptr_t _get_osfhandle(int);
> typedef void* HANDLE;
>
> HANDLE fn() {
> return (HANDLE)_get_osfhandle(0);
> }
> martind@sirius:~/tmp/svensson-2023-06-08$ gcc -c -Wbad-function-cast make.c
> make.c: In function ‘fn’:
> make.c:7:10: warning: cast from function call of type ‘intptr_t {aka long
> int}’ to non-matching type ‘void *’ [-Wbad-function-cast]
> return (HANDLE)_get_osfhandle(0);
> ^
> martind@sirius:~/tmp/svensson-2023-06-08$
>
> That's gcc-6.3, but it's much the same in every version I tested from gcc-4.4
> to gcc-10. A random version's man page,
> https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Warning-Options.html, explains
> the flag as to:
>
> > Warn when a function call is cast to a non-matching type. For example, warn
> > if a call to a function returning an integer type is cast to a pointer type.
>
> I fear they mean exactly what they say there. So, while you might call it a
> mistake, I don't think it's an accident.
> https://stackoverflow.com/a/35308713/18096 has some explanation of why it
> might sometimes be useful... and how it could be sidestepped by a change to
> Gnu Make, like our new friend wished for.
Yes, it sounds like -Wbad-function-cast will always emit a warning in
such cases, and one cannot use it with the likes of _get_osfhandle,
which _require_ such casts.
You can, of course, work around it; the following compiles without a
warning:
#include <stdint.h>
intptr_t _get_osfhandle(int);
typedef void* HANDLE;
HANDLE fn() {
intptr_t handle = _get_osfhandle(0);
return (HANDLE)handle;
}