Issue 178388
Summary wcast-function-type warning may be too strict for conversions between pointer and integer types of same width
Labels new issue
Assignees
Reporter jiangxuezhi
    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.

Consider the following code:

#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, LLVM 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.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to