]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
tools/x86/kcpuid: Exit the program on invalid parameters
authorAhmed S. Darwish <darwi@linutronix.de>
Mon, 24 Mar 2025 14:20:23 +0000 (15:20 +0100)
committerIngo Molnar <mingo@kernel.org>
Tue, 25 Mar 2025 08:53:44 +0000 (09:53 +0100)
If the user passed an invalid CPUID index value through --leaf=index,
kcpuid prints a warning, does nothing, then exits successfully.
Transform the warning to an error, and exit the program with a proper
error code.

Similarly, if the user passed an invalid subleaf, kcpuid prints a
warning, dumps the whole leaf, then exits successfully.  Print a clear
error message regarding the invalid subleaf and exit the program with the
proper error code.

Note, moving the "Invalid input index" message from index_to_func() to
show_info() localizes error message handling to the latter, where it
should be.  It also allows index_to_func() to be refactored at further
commits.

Note, since after this commit and its parent kcpuid does not just "move
on" on failures, remove the NULL parameter check plus silent exit at
show_func() and show_leaf().

Signed-off-by: Ahmed S. Darwish <darwi@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Link: https://lore.kernel.org/r/20250324142042.29010-3-darwi@linutronix.de
tools/arch/x86/kcpuid/kcpuid.c

index 40a9e59c2fd568c9dc2b85d24a20696bda99aed2..25b10feeb7209373c1e2edf435b2a645b5f3c46b 100644 (file)
@@ -481,9 +481,6 @@ static void decode_bits(u32 value, struct reg_desc *rdesc, enum cpuid_reg reg)
 
 static void show_leaf(struct subleaf *leaf)
 {
-       if (!leaf)
-               return;
-
        if (show_raw) {
                leaf_print_raw(leaf);
        } else {
@@ -505,9 +502,6 @@ static void show_func(struct cpuid_func *func)
 {
        int i;
 
-       if (!func)
-               return;
-
        for (i = 0; i < func->nr; i++)
                show_leaf(&func->leafs[i]);
 }
@@ -528,10 +522,9 @@ static inline struct cpuid_func *index_to_func(u32 index)
        range = (index & 0x80000000) ? leafs_ext : leafs_basic;
        func_idx = index & 0xffff;
 
-       if ((func_idx + 1) > (u32)range->nr) {
-               warnx("Invalid input index (0x%x)", index);
+       if ((func_idx + 1) > (u32)range->nr)
                return NULL;
-       }
+
        return &range->funcs[func_idx];
 }
 
@@ -550,18 +543,19 @@ static void show_info(void)
                /* Only show specific leaf/subleaf info */
                func = index_to_func(user_index);
                if (!func)
-                       return;
+                       errx(EXIT_FAILURE, "Invalid input leaf (0x%x)", user_index);
 
                /* Dump the raw data also */
                show_raw = true;
 
                if (user_sub != 0xFFFFFFFF) {
-                       if (user_sub + 1 <= (u32)func->nr) {
-                               show_leaf(&func->leafs[user_sub]);
-                               return;
+                       if (user_sub + 1 > (u32)func->nr) {
+                               errx(EXIT_FAILURE, "Leaf 0x%x has no valid subleaf = 0x%x",
+                                    user_index, user_sub);
                        }
 
-                       warnx("Invalid input subleaf (0x%x)", user_sub);
+                       show_leaf(&func->leafs[user_sub]);
+                       return;
                }
 
                show_func(func);