Skip to content

Commit 9d80992

Browse files
Fix CPU_SET dynamic allocation and leak (#205)
The initial implementation had a number of issues: - The allocation of the CPU_SET should be checked for a NULL return. - The CPU_*_S macros should be used when working with dynamic sets. - The CPU_SET needs to be cleared via CPU_ZERO_S before use. - Dynamic CPU_SETs need to be freed after use. - The __riscv_hwprobe syscall is expecting a set *size* not a *count*.
1 parent ef63460 commit 9d80992

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Diff for: src/riscv/linux/riscv-hw.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ void cpuinfo_riscv_linux_decode_vendor_uarch_from_hwprobe(
2121
*uarch = cpuinfo_uarch_unknown;
2222

2323
/* Create a CPU set with this processor flagged. */
24-
const size_t cpu_set_size = processor + 1;
25-
cpu_set_t* cpu_set = CPU_ALLOC(cpu_set_size);
26-
CPU_SET(processor, cpu_set);
24+
const size_t cpu_count = processor + 1;
25+
cpu_set_t* cpu_set = CPU_ALLOC(cpu_count);
26+
if (cpu_set == NULL) {
27+
cpuinfo_log_warning("failed to allocate space for cpu_set");
28+
return;
29+
}
30+
31+
const size_t cpu_set_size = CPU_ALLOC_SIZE(cpu_count);
32+
CPU_ZERO_S(cpu_set_size, cpu_set);
33+
CPU_SET_S(processor, cpu_set_size, cpu_set);
2734

2835
/* Request all available information from hwprobe. */
2936
int ret = __riscv_hwprobe(pairs, pairs_count,
3037
cpu_set_size, (unsigned long*)cpu_set,
3138
0 /* flags */);
3239
if (ret < 0) {
3340
cpuinfo_log_warning("failed to get hwprobe information, err: %d", ret);
34-
return;
41+
goto cleanup;
3542
}
3643

3744
/**
@@ -59,4 +66,7 @@ void cpuinfo_riscv_linux_decode_vendor_uarch_from_hwprobe(
5966
}
6067
cpuinfo_riscv_decode_vendor_uarch(vendor_id, arch_id, imp_id,
6168
vendor, uarch);
69+
70+
cleanup:
71+
CPU_FREE(cpu_set);
6272
}

0 commit comments

Comments
 (0)