[PATCH v1] led: fix coverity scan error

Heiko Schocher hs at denx.de
Wed Feb 12 10:10:55 CET 2025


Tom reported the following covervity scan error:

*** CID 541279:    (TAINTED_SCALAR)
/drivers/led/led-uclass.c: 284 in led_get_function_name()
278                     if (!ret) {
279                             snprintf(uc_plat->name, LED_MAX_NAME_SIZE,
280                                      "%s:%s-%d",
281                                      cp ? "" : led_colors[color],
282                                      func ? func : "", enumerator);
283                     } else {
>>>     CID 541279:    (TAINTED_SCALAR)
>>>     Using tainted variable "color" as an index into an array
"led_colors".

see:
https://lists.denx.de/pipermail/u-boot/2025-February/580250.html

Fix it.

Signed-off-by: Heiko Schocher <hs at denx.de>

---
Azure build:
https://dev.azure.com/hs0298/hs/_build/results?buildId=170&view=results

 drivers/led/led-uclass.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
index 27ef890ed0a..22f61d12d38 100644
--- a/drivers/led/led-uclass.c
+++ b/drivers/led/led-uclass.c
@@ -273,6 +273,10 @@ static const char *led_get_function_name(struct udevice *dev)
 	/* Now try to detect function label name */
 	func = dev_read_string(dev, "function");
 	cp = dev_read_u32(dev, "color", &color);
+	// prevent coverity scan error CID 541279: (TAINTED_SCALAR)
+	if (color < LED_COLOR_ID_WHITE || color >= LED_COLOR_ID_MAX)
+		cp = -EINVAL;
+
 	if (cp == 0 || func) {
 		ret = dev_read_u32(dev, "function-enumerator", &enumerator);
 		if (!ret) {
-- 
2.20.1



More information about the U-Boot mailing list