Le lun. 5 août 2024 à 11:27, LIU Hao <lh_mo...@126.com> a écrit :
>
> 在 2024-08-05 01:40, Pali Rohár 写道:
> > It looks like that casting whole function pointer to LPVOID type prior
> > casting to FARPROC mutes this warning.
> >
> > Now I remember that in past I have already used casting return value
> > from GetProcAddress() to LPVOID and after that to the correct function
> > pointer type which muted gcc warnings.
>
> No, cast to `INT_PTR` instead. Casting between function pointers and object 
> pointers is undefined
> behavior in C; but it's required by POSIX, and is conditionally supported in 
> C++.

I've casted the return of GetProcAddress in winpthreads through (void
*) to avoid the -Wcast-function-type warning too.
Note that POSIX 2008 only states that the return of dlsym, which is a
(void *), can be converted back to a function pointer, *not* that any
function pointer can be converted back and forth to (void *).

GCC provides a generic function pointer type: void(*)(void), but
neither MSVC nor clang support it.

C99 in Annex J.5.7 Function pointer casts states:
1 A pointer to an object or to void may be cast to a pointer to a
function, allowing data to be invoked as a function (6.5.4).
2 A pointer to a function may be cast to a pointer to an object or to
void, allowing a function to be inspected or modified (for example, by
a debugger) (6.5.4)

and MSVC, clang, and GCC implement this extension.

The C99 standard even defines (u)intptr_t for object pointers only...
but all major compilers seem to support casting a function pointer to
a (u)intptr_t too.

It's a mess!
-- Antonin


_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to