All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cmd: led: reject unknown LED state instead of silently returning success
@ 2026-07-08 14:32 Naveen Kumar Chaudhary
  2026-07-08 16:00 ` Quentin Schulz
  0 siblings, 1 reply; 5+ messages in thread
From: Naveen Kumar Chaudhary @ 2026-07-08 14:32 UTC (permalink / raw)
  To: trini; +Cc: u-boot

get_led_cmd() searches state_label[] for a matching name and returns
-1 on no match, but its declared return type is enum led_state_t.
All enumerators of that enum are non-negative, so the compiler is
free to pick an unsigned underlying type (and does so with
-fshort-enums, which several U-Boot targets enable). In that case
-1 is reinterpreted as a large positive value and any subsequent
comparison against -1 silently misbehaves.

In do_led() the returned value is stored into an enum led_state_t
and dispatched through a switch that has no default arm and no case
for the sentinel. When the user supplies an unknown state name the
switch falls through, ret was already set to 0 by led_get_by_label(),
and the command reports success without doing anything.

Return LEDST_COUNT from get_led_cmd() on no match. LEDST_COUNT is
already a valid enumerator, so this avoids all questions about the
enum's underlying representation. In do_led(), when a state argument
was actually supplied (argc > 2) and the lookup produced LEDST_COUNT,
reject it with a diagnostic and CMD_RET_USAGE; the existing
LEDST_COUNT switch arm continues to handle the "no state argument"
case where it means "show current state".

Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
---
 cmd/led.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/cmd/led.c b/cmd/led.c
index 296c07b3b38..d547276e480 100644
--- a/cmd/led.c
+++ b/cmd/led.c
@@ -27,7 +27,7 @@ enum led_state_t get_led_cmd(char *var)
 			return i;
 	}
 
-	return -1;
+	return LEDST_COUNT;
 }
 
 static int show_led_state(struct udevice *dev)
@@ -84,6 +84,10 @@ int do_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		return list_leds();
 
 	cmd = argc > 2 ? get_led_cmd(argv[2]) : LEDST_COUNT;
+	if (argc > 2 && cmd == LEDST_COUNT) {
+		printf("Unknown LED state '%s'\n", argv[2]);
+		return CMD_RET_USAGE;
+	}
 	if (cmd == LEDST_BLINK) {
 		if (argc < 4)
 			return CMD_RET_USAGE;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-07-22 17:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-08 14:32 [PATCH] cmd: led: reject unknown LED state instead of silently returning success Naveen Kumar Chaudhary
2026-07-08 16:00 ` Quentin Schulz
2026-07-10 15:16   ` Naveen Kumar Chaudhary
2026-07-10 15:34     ` Quentin Schulz
2026-07-22 17:50       ` Quentin Schulz via U-Boot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.