Skip to content

Fix CPU_SET dynamic allocation and leak #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/riscv/linux/riscv-hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ void cpuinfo_riscv_linux_decode_vendor_uarch_from_hwprobe(
*uarch = cpuinfo_uarch_unknown;

/* Create a CPU set with this processor flagged. */
const size_t cpu_set_size = processor + 1;
cpu_set_t* cpu_set = CPU_ALLOC(cpu_set_size);
CPU_SET(processor, cpu_set);
const size_t cpu_count = processor + 1;
cpu_set_t* cpu_set = CPU_ALLOC(cpu_count);
if (cpu_set == NULL) {
cpuinfo_log_warning("failed to allocate space for cpu_set");
return;
}

const size_t cpu_set_size = CPU_ALLOC_SIZE(cpu_count);
CPU_ZERO_S(cpu_set_size, cpu_set);
CPU_SET_S(processor, cpu_set_size, cpu_set);

/* Request all available information from hwprobe. */
int ret = __riscv_hwprobe(pairs, pairs_count,
cpu_set_size, (unsigned long*)cpu_set,
0 /* flags */);
if (ret < 0) {
cpuinfo_log_warning("failed to get hwprobe information, err: %d", ret);
return;
goto cleanup;
}

/**
Expand Down Expand Up @@ -59,4 +66,7 @@ void cpuinfo_riscv_linux_decode_vendor_uarch_from_hwprobe(
}
cpuinfo_riscv_decode_vendor_uarch(vendor_id, arch_id, imp_id,
vendor, uarch);

cleanup:
CPU_FREE(cpu_set);
}