Skip to contentSkip to conversation

led: fix next coverity scan error

PATCH In review Heiko Schocher 3 participants 3 messages Feb 26, 2025 – Mar 5, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 led: fix next coverity scan error

Tom reported the following covervity scan error:

*** CID 542488: Control flow issues (NO_EFFECT) /drivers/led/led-uclass.c: 277 in led_get_function_name()
271 return uc_plat->label;
272

273             /* Now try to detect function label name */
274             func = dev_read_string(dev, "function");
275             cp = dev_read_u32(dev, "color", &color);
276             // prevent coverity scan error CID 541279: (TAINTED_SCALAR)
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 542488: Control flow issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true.

"color < 0U".
277 if (color < LED_COLOR_ID_WHITE || color >= LED_COLOR_ID_MAX)
278 cp = -EINVAL;
279

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

Fix it.

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

Changes in v2:
- Work in comment from Quentin and Tom
remove changes in dts/upstream/include/dt-bindings/leds/common.h Add therefore a comment in the uclass check, that we only check the upper bound.
- added Quentin to cc

 drivers/led/led-uclass.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/led/led-uclass.c +6 -2
index 22f61d12d38..edcdeee1e9a 100644
@@ -273,8 +273,12 @@ 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)
+	/*
+	 *  prevent coverity scan error CID 541279: (TAINTED_SCALAR)
+	 *  only check the upper bound. No need to check the lower bound
+	 *  as color is from type u32 and never can be lower than 0.
+	 */
+	if (color >= LED_COLOR_ID_MAX)
 		cp = -EINVAL;
 
 	if (cp == 0 || func) {
Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#1 lore raw

Hi Heiko,

Quoted 17 lines · Heiko Schocher

Tom reported the following covervity scan error:

*** CID 542488: Control flow issues (NO_EFFECT) /drivers/led/led-uclass.c: 277 in led_get_function_name()
271 return uc_plat->label;
272

273             /* Now try to detect function label name */
274             func = dev_read_string(dev, "function");
275             cp = dev_read_u32(dev, "color", &color);
276             // prevent coverity scan error CID 541279: (TAINTED_SCALAR)
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 542488: Control flow issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true.

"color < 0U".
277 if (color < LED_COLOR_ID_WHITE || color >= LED_COLOR_ID_MAX)
278 cp = -EINVAL;
279

Quoted 7 lines

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

Fix it.

Thanks!
Quentin

#2 lore raw
In reply to Heiko Schocher
Quoted 10 lines · Heiko Schocher

Tom reported the following covervity scan error:

*** CID 542488: Control flow issues (NO_EFFECT) /drivers/led/led-uclass.c: 277 in led_get_function_name()
271 return uc_plat->label;
272

273             /* Now try to detect function label name */
274             func = dev_read_string(dev, "function");
275             cp = dev_read_u32(dev, "color", &color);
276             // prevent coverity scan error CID 541279: (TAINTED_SCALAR)
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 542488: Control flow issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true.

"color < 0U".
277 if (color < LED_COLOR_ID_WHITE || color >= LED_COLOR_ID_MAX)
278 cp = -EINVAL;
279

[...]

Applied to u-boot/master, thanks!

Signature · 1 line
Tom