From nobody Fri Dec 19 17:52:13 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A1CB42A82 for ; Wed, 12 Mar 2025 14:37:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741790273; cv=none; b=kTCXhJJZGMEcSVOgmJx+hRF6PH5yhqLYKO+DO7XfPVsrRBszUsmLlaSF3i1UJMnJedRbWK+z4jvBW95q+aCAMAdNlbIiHefB0QS4fxwS4tOa8xJt+oEIOt/n6fgDi6eeN1WcSFdZk9bKos+0B+u151aXBDWR5Bd4sanE5nz6w8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741790273; c=relaxed/simple; bh=V0ZaSj+Spn2inI0JqfzyJrjh+Rsa6AKhAMrFK/kqgB8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ty5Yk5QOl0o78aRGHgseM+cIKelBqxYifjU34ikirT4JO6yeBHSpqB0Z9GeKtOuTf4N9Fj21weDcFKEcBbzyzGKS3yvWIX+pcJoVkwMw2aR2Admuk1p4Da+ZA44Ofc/H0gcWYfj7/SiQcYK+l8ppb9E/3JC+2QyApyM0PMLAFkc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kr8gc1gp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=QZIzu+3/; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kr8gc1gp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="QZIzu+3/" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1741790270; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8s+kgyHb+rH7bNPws5A6RoZEJxeIxOWBkNJP/5UXKEw=; b=kr8gc1gp+egonbM23oXjLEMAhf23ig5fpVYT1pC5fSCNtp39pz6hKnGCCxxstner7gIQmx r6rYrHSH+509uGsiXzeUg0W6vU4YPTchFZTkpLrDQEARPVU80JOFw4jGaXI6G0Jl0voZwV 4egrMQFQNjhOtmzssJYuYG0U4yPh2uw7I1CzsDdi2m4Qje0yRgKgXPll5pdEKHpRsYUYcq pAFwi34uPLGZn3BQ/vD7KkOFxdlBNCeSTPbpgstwVxZHHl6/cngcWOTYEdp+kLv5OfYbOC lHec+svLtBT1qGaYoEsO76XaDiUNEqdzoWOBCUHCFNCDU8znVaW6P3rVhzHQUg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1741790270; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8s+kgyHb+rH7bNPws5A6RoZEJxeIxOWBkNJP/5UXKEw=; b=QZIzu+3/Jf48X6I3zUQZBxgxgYeOhOKyPUn07NAA5lvZ6XJr1NNWeAFqfLUE9vCZx4TW6P KCrRnB7FVu35JTAA== To: Ingo Molnar , Dave Hansen , Borislav Petkov Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v2 01/20] tools/x86/kcpuid: Fix error handling Date: Wed, 12 Mar 2025 15:37:18 +0100 Message-ID: <20250312143738.458507-2-darwi@linutronix.de> In-Reply-To: <20250312143738.458507-1-darwi@linutronix.de> References: <20250312143738.458507-1-darwi@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Error handling in kcpuid is unreliable. On malloc() failures, the code prints an error then just goes on. The error messages are also printed to standard output instead of standard error. Use err() and errx() from to direct all error messages to standard error and automatically exit the program. Use err() to include the errno information, and errx() otherwise. Use warnx() for warnings. While at it, alphabetically reorder the header includes. Fixes: c6b2f240bf8d ("tools/x86: Add a kcpuid tool to show raw CPU features= ") Reported-by: Remington Brasga Closes: https://lkml.kernel.org/r/20240926223557.2048-1-rbrasga@uci.edu Signed-off-by: Ahmed S. Darwish --- tools/arch/x86/kcpuid/kcpuid.c | 47 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c index 1b25c0a95d3f..abfeecce5aa8 100644 --- a/tools/arch/x86/kcpuid/kcpuid.c +++ b/tools/arch/x86/kcpuid/kcpuid.c @@ -1,11 +1,12 @@ // SPDX-License-Identifier: GPL-2.0 #define _GNU_SOURCE =20 -#include +#include +#include #include +#include #include #include -#include =20 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define min(a, b) (((a) < (b)) ? (a) : (b)) @@ -145,14 +146,14 @@ static bool cpuid_store(struct cpuid_range *range, u3= 2 f, int subleaf, if (!func->leafs) { func->leafs =3D malloc(sizeof(struct subleaf)); if (!func->leafs) - perror("malloc func leaf"); + err(EXIT_FAILURE, NULL); =20 func->nr =3D 1; } else { s =3D func->nr; func->leafs =3D realloc(func->leafs, (s + 1) * sizeof(*leaf)); if (!func->leafs) - perror("realloc f->leafs"); + err(EXIT_FAILURE, NULL); =20 func->nr++; } @@ -211,7 +212,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax) =20 range =3D malloc(sizeof(struct cpuid_range)); if (!range) - perror("malloc range"); + err(EXIT_FAILURE, NULL); =20 if (input_eax & 0x80000000) range->is_ext =3D true; @@ -220,7 +221,7 @@ struct cpuid_range *setup_cpuid_range(u32 input_eax) =20 range->funcs =3D malloc(sizeof(struct cpuid_func) * idx_func); if (!range->funcs) - perror("malloc range->funcs"); + err(EXIT_FAILURE, NULL); =20 range->nr =3D idx_func; memset(range->funcs, 0, sizeof(struct cpuid_func) * idx_func); @@ -395,8 +396,8 @@ static int parse_line(char *line) return 0; =20 err_exit: - printf("Warning: wrong line format:\n"); - printf("\tline[%d]: %s\n", flines, line); + warnx("Wrong line format:\n" + "\tline[%d]: %s", flines, line); return -1; } =20 @@ -418,10 +419,8 @@ static void parse_text(void) file =3D fopen("./cpuid.csv", "r"); } =20 - if (!file) { - printf("Fail to open '%s'\n", filename); - return; - } + if (!file) + err(EXIT_FAILURE, "%s", filename); =20 while (1) { ret =3D getline(&line, &len, file); @@ -530,7 +529,7 @@ static inline struct cpuid_func *index_to_func(u32 inde= x) func_idx =3D index & 0xffff; =20 if ((func_idx + 1) > (u32)range->nr) { - printf("ERR: invalid input index (0x%x)\n", index); + warnx("Invalid input index (0x%x)", index); return NULL; } return &range->funcs[func_idx]; @@ -562,7 +561,7 @@ static void show_info(void) return; } =20 - printf("ERR: invalid input subleaf (0x%x)\n", user_sub); + warnx("Invalid input subleaf (0x%x)", user_sub); } =20 show_func(func); @@ -593,15 +592,15 @@ static void setup_platform_cpuid(void) =20 static void usage(void) { - printf("kcpuid [-abdfhr] [-l leaf] [-s subleaf]\n" - "\t-a|--all Show both bit flags and complex bit fields info\= n" - "\t-b|--bitflags Show boolean flags only\n" - "\t-d|--detail Show details of the flag/fields (default)\n" - "\t-f|--flags Specify the cpuid csv file\n" - "\t-h|--help Show usage info\n" - "\t-l|--leaf=3Dindex Specify the leaf you want to check\n" - "\t-r|--raw Show raw cpuid data\n" - "\t-s|--subleaf=3Dsub Specify the subleaf you want to check\n" + warnx("kcpuid [-abdfhr] [-l leaf] [-s subleaf]\n" + "\t-a|--all Show both bit flags and complex bit fields = info\n" + "\t-b|--bitflags Show boolean flags only\n" + "\t-d|--detail Show details of the flag/fields (default)\n" + "\t-f|--flags Specify the cpuid csv file\n" + "\t-h|--help Show usage info\n" + "\t-l|--leaf=3Dindex Specify the leaf you want to check\n" + "\t-r|--raw Show raw cpuid data\n" + "\t-s|--subleaf=3Dsub Specify the subleaf you want to check" ); } =20 @@ -652,7 +651,7 @@ static int parse_options(int argc, char *argv[]) user_sub =3D strtoul(optarg, NULL, 0); break; default: - printf("%s: Invalid option '%c'\n", argv[0], optopt); + warnx("Invalid option '%c'", optopt); return -1; } =20 --=20 2.48.1