Skip to contentSkip to conversation

imx: ventana: Avoid undefined behaviour

PATCH New Thierry Reding 3 participants 3 messages Aug 22, 2014 – Sep 9, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 imx: ventana: Avoid undefined behaviour

From: Thierry Reding <[email protected]>

The leds array within struct ventana has space for 3 elements, but the setup_board_gpio() function tries to set up 4 GPIOs for LEDs. Recent versions of GCC complain about that:

	board/gateworks/gw_ventana/gw_ventana.c: In function 'setup_board_gpio':
	board/gateworks/gw_ventana/gw_ventana.c:987:27: warning: iteration 3u invokes undefined behavior [-Waggressive-loop-optimizations]
	   if (gpio_cfg[board].leds[i])
				   ^
	board/gateworks/gw_ventana/gw_ventana.c:986:2: note: containing loop
	  for (i = 0; i < 4; i++) {
	  ^

Fix this by making the upper bound of the loop match the array size.

---
 board/gateworks/gw_ventana/gw_ventana.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
board/gateworks/gw_ventana/gw_ventana.c +1 -1
index 9d2651f0cbf1..01212367c396 100644
@@ -983,7 +983,7 @@ static void setup_board_gpio(int board)
 	gpio_direction_output(gpio_cfg[board].pcie_rst, 0);
 
 	/* turn off (active-high) user LED's */
-	for (i = 0; i < 4; i++) {
+	for (i = 0; i < ARRAY_SIZE(gpio_cfg[board].leds); i++) {
 		if (gpio_cfg[board].leds[i])
 			gpio_direction_output(gpio_cfg[board].leds[i], 1);
 	}
Signature · 1 line
2.0.4
#1 lore raw

PATCH 1/1 imx: ventana: Avoid undefined behaviour

Quoted 35 lines · Thierry Reding

From: Thierry Reding <[email protected]>

The leds array within struct ventana has space for 3 elements, but the setup_board_gpio() function tries to set up 4 GPIOs for LEDs. Recent versions of GCC complain about that:

        board/gateworks/gw_ventana/gw_ventana.c: In function 'setup_board_gpio':
        board/gateworks/gw_ventana/gw_ventana.c:987:27: warning: iteration 3u invokes undefined behavior [-Waggressive-loop-optimizations]
           if (gpio_cfg[board].leds[i])
                                   ^
        board/gateworks/gw_ventana/gw_ventana.c:986:2: note: containing loop
          for (i = 0; i < 4; i++) {
          ^

Fix this by making the upper bound of the loop match the array size.

---
 board/gateworks/gw_ventana/gw_ventana.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
board/gateworks/gw_ventana/gw_ventana.c +1 -2
index 9d2651f0cbf1..01212367c396 100644
@@ -983,7 +983,7 @@ static void setup_board_gpio(int board)
        gpio_direction_output(gpio_cfg[board].pcie_rst, 0);
 
        /* turn off (active-high) user LED's */
-       for (i = 0; i < 4; i++) {
+       for (i = 0; i < ARRAY_SIZE(gpio_cfg[board].leds); i++) {
                if (gpio_cfg[board].leds[i])
                        gpio_direction_output(gpio_cfg[board].leds[i], 1);
        }
--
Quoted 2 lines

2.0.4

Thanks for catching and fixing this Thierry!

Tim

#2 lore raw

PATCH 1/1 imx: ventana: Avoid undefined behaviour

In reply to Thierry Reding
Quoted 14 lines · Thierry Reding

From: Thierry Reding <[email protected]>

The leds array within struct ventana has space for 3 elements, but the setup_board_gpio() function tries to set up 4 GPIOs for LEDs. Recent versions of GCC complain about that:

	board/gateworks/gw_ventana/gw_ventana.c: In function 'setup_board_gpio':
	board/gateworks/gw_ventana/gw_ventana.c:987:27: warning: iteration 3u invokes undefined behavior [-Waggressive-loop-optimizations]
	   if (gpio_cfg[board].leds[i])
				   ^
	board/gateworks/gw_ventana/gw_ventana.c:986:2: note: containing loop
	  for (i = 0; i < 4; i++) {
	  ^
Quoted 4 lines

Fix this by making the upper bound of the loop match the array size.

---

Applied to u-boot-imx, thanks !

Best regards,
Stefano Babic

Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic at denx.de
=====================================================================