llvm / llvm/llvm-project

wcast-function-type warning may be too strict for conversions between pointer and integer types of same width

Open
#178,388 7 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics undefined behaviour
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

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:

```c
#include
#include

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.