https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123858
Bug ID: 123858
Summary: wcast-function-type warning may be too strict for
conversions between pointer and integer types of same
width
Product: gcc
Version: 14.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: jiangxuezhi2 at huawei dot com
Target Milestone: ---
The -Wcast-function-type warning rightly guards against potentially unsafe
function pointer casts. It currently considers cases where pointer types or
integer types are cast to one another, but it does not seem to account for
situations where a pointer type and an integer type have the same width, which
could be safe in some low-level or system programming contexts.
Example:
#include <stdio.h>
#include <stdint.h>
void* func() {
return NULL;
}
int main() {
uintptr_t (*d)() = (uintptr_t (*)())func;
return 0;
}
On platforms where void* and uintptr_t have the same size (e.g., typical 32‑bit
or 64‑bit systems), this cast does not cause any loss of information.
Currently, GCC emits a -Wcast-function-type warning here, although the
conversion is effectively safe from a size perspective.
Could the warning be refined to also consider type widths? For example, when a
function returning a pointer type is cast to a function returning an integer
type (or vice versa), and both types have the same sizeof, the warning might be
suppressed or downgraded.