From nobody Thu Dec 18 16:15:42 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 8197F25DAF0 for ; Mon, 24 Mar 2025 14:20: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=1742826053; cv=none; b=Ed9AuyaeumMOOfyE/0DFzf8IdY16IiHXAkiIoNRLW/Eh6hR/HY9AadVkznWth+5o+K0KnSue0gFshM5LB7HXsAD/Qbtpghs1+xXnY2ehbCJxT9kc9wAkMLWvH5I5xTOTqi7iCFG2Hh2ibWiv0Rl2mWEeLawaLW5LIOsVwcC9tv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742826053; c=relaxed/simple; bh=V0ZaSj+Spn2inI0JqfzyJrjh+Rsa6AKhAMrFK/kqgB8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uZy/AAB57bHoMWf1ErteKN10FQ7lALQ4a2nL34sI7es/rHhLIqF1kHmOGMdsZSgy2AL0KY6mu4uRUgS/JRbOqCQvGbYyFlWeuF7R62CUsO30YhBaRXZhd3xMGHSgxn8DIGcxPSD83KnoT3bMdrAz6gRhPjBR/gwrN7g7r7aU4JE= 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=ME4kKdDU; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=J7R2oRxh; 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="ME4kKdDU"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="J7R2oRxh" From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742826049; 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=ME4kKdDUw4sZV2otsK9csaw+nb7dDw+ZbKRPyHvDcgfiR0GfXihp5qbBhdKJW6ZdSYJ/IL SjPLJVF5g4qZhtaxdb6k+0yX5UlM7Qt0IxXkOSzF9VRuTybQTEu8pGkaPoowVtiFwY1Sr5 FOsQpkY2o4+dEOlPu5oCrFuj9XWJQJLdFbrM2NGV7qu31fb5MIYb22RkEKNx9pv7GzYDfu alPu2lY9ZkTF7JenKmfrr+yqfK+x9By41XbiR37nyFePVyff6suVLrcwJd+eH/NUEAdsDo W52/QVMIIS8KCr2iO5NG6LgKuJpaPjS62RjkGt6a7hDZ8U6hYIeEYf2tqDtA2w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742826049; 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=J7R2oRxhrt188w+d801H8s8zlLM4ilUBR8NqIByzKew6RU2DTNLRtU4sETyWAj6n7oi891 VnSHXnv3B1087vDg== To: Ingo Molnar , Borislav Petkov , Dave Hansen Cc: Thomas Gleixner , Andrew Cooper , "H. Peter Anvin" , John Ogness , x86@kernel.org, x86-cpuid@lists.linux.dev, LKML , "Ahmed S. Darwish" Subject: [PATCH v3 01/20] tools/x86/kcpuid: Fix error handling Date: Mon, 24 Mar 2025 15:20:22 +0100 Message-ID: <20250324142042.29010-2-darwi@linutronix.de> In-Reply-To: <20250324142042.29010-1-darwi@linutronix.de> References: <20250324142042.29010-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