You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While casting function pointers is allowed in C, the function must
ultimately be called through a pointer with the same type signature as
the function itself. Type signature mismatches, even decaying T* to
void* is undefined behavior.
UBSan flags this with -fsanitize=function. The easiest way I found to
repro this was:
CC=clang-18 CXX=clang++-18 \
CFLAGS="-fsanitize=function -fno-sanitize-recover=function" \
CXXFLAGS="-fsanitize=function -fno-sanitize-recover=function" \
cmake -GNinja -B build -DCPUINFO_BUILD_BENCHMARKS=OFF
ninja -C build
./build/cpu-info
That gives the following error:
[...]/src/linux/multiline.c:85:11: runtime error: call to function parse_line through pointer to incorrect function type 'bool (*)(const char *, const char *, void *, unsigned long)'
cpuinfo.c: note: parse_line defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior [...]/src/linux/multiline.c:85:11
The fix is fairly straightforward: just keep the function at the type
signature the expected, and cast void* instead the function instead.
0 commit comments