From: longlong yan <yanlonglong@kylinos.cn>
To: lenb@kernel.org
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
longlong yan <yanlonglong@kylinos.cn>
Subject: [PATCH] tools/power/x86/turbostat: add NULL check after calloc()
Date: Wed, 2 Sep 2026 14:59:22 +0800 [thread overview]
Message-ID: <20260902065922.1408-1-yanlonglong@kylinos.cn> (raw)
Three calloc() calls in turbostat lack NULL return checks, leading to
potential NULL pointer dereferences on allocation failure:
1. rapl_perf_init(): the allocated `domain_visited` is used later via
memset(domain_visited, 0, ...) without checking for NULL.
2. added_perf_counters_init_(): the allocated `domain_visited` is used
later via memset() and domain_visited[next_domain] without checking
for NULL.
3. pmt_add_counter(): the allocated `pcounter` is dereferenced
immediately via strncpy(pcounter->name, ...) without checking for
NULL.
Add NULL checks after each calloc(), using the same error handling
style already present in each function: err(-1, ...) for rapl_perf_init(),
errx(1, ...) for added_perf_counters_init_(), and return 1 for
pmt_add_counter().
Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
tools/power/x86/turbostat/turbostat.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 4ad7cb1df5c5..926dd1422fbe 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -8578,6 +8578,8 @@ void rapl_perf_init(void)
{
const unsigned int num_domains = get_rapl_num_domains();
bool *domain_visited = calloc(num_domains, sizeof(bool));
+ if (!domain_visited)
+ err(-1, "calloc domain_visited");
rapl_counter_info_perdomain = calloc(num_domains, sizeof(*rapl_counter_info_perdomain));
if (rapl_counter_info_perdomain == NULL)
@@ -9942,6 +9944,8 @@ int added_perf_counters_init_(struct perf_counter_info *pinfo)
const size_t max_num_domains = MAX(topo.max_cpu_num + 1, MAX(topo.max_core_id + 1, topo.max_package_id + 1));
domain_visited = calloc(max_num_domains, sizeof(*domain_visited));
+ if (!domain_visited)
+ errx(1, "%s: alloc %s", __func__, "domain_visited");
while (pinfo) {
switch (pinfo->scope) {
@@ -10333,6 +10337,8 @@ int pmt_add_counter(unsigned int guid, unsigned int seq, const char *name, enum
pcounter = pmt_find_counter(*pmt_root, name);
if (!pcounter) {
pcounter = calloc(1, sizeof(*pcounter));
+ if (!pcounter)
+ return 1;
new_counter = true;
}
--
2.43.0
reply other threads:[~2026-09-02 6:59 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260902065922.1408-1-yanlonglong@kylinos.cn \
--to=yanlonglong@kylinos.cn \
--cc=lenb@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox