Skip to content

Commit 8cb442f

Browse files
committed
Fix invalid function pointer cast in cpuinfo.c
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.
1 parent 8a1772a commit 8cb442f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/x86/linux/cpuinfo.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ struct proc_cpuinfo_parser_state {
8383
static bool parse_line(
8484
const char* line_start,
8585
const char* line_end,
86-
struct proc_cpuinfo_parser_state state[restrict static 1],
86+
void* context,
8787
uint64_t line_number) {
88+
struct proc_cpuinfo_parser_state* restrict state = context;
8889
/* Empty line. Skip. */
8990
if (line_start == line_end) {
9091
return true;
@@ -215,5 +216,5 @@ bool cpuinfo_x86_linux_parse_proc_cpuinfo(
215216
.processors = processors,
216217
};
217218
return cpuinfo_linux_parse_multiline_file(
218-
"/proc/cpuinfo", BUFFER_SIZE, (cpuinfo_line_callback)parse_line, &state);
219+
"/proc/cpuinfo", BUFFER_SIZE, parse_line, &state);
219220
}

0 commit comments

Comments
 (0)