diff mbox

[U-Boot] imx: ventana: Avoid undefined behaviour

Message ID 1408693595-13977-1-git-send-email-thierry.reding@gmail.com
State Awaiting Upstream
Delegated to: Stefano Babic
Headers show

Commit Message

Thierry Reding Aug. 22, 2014, 7:46 a.m. UTC
From: Thierry Reding <treding@nvidia.com>

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.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 board/gateworks/gw_ventana/gw_ventana.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Tim Harvey Aug. 22, 2014, 6:55 p.m. UTC | #1
On Fri, Aug 22, 2014 at 12:46 AM, Thierry Reding
<thierry.reding@gmail.com> wrote:
> From: Thierry Reding <treding@nvidia.com>
>
> 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.
>
> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---
>  board/gateworks/gw_ventana/gw_ventana.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/board/gateworks/gw_ventana/gw_ventana.c b/board/gateworks/gw_ventana/gw_ventana.c
> index 9d2651f0cbf1..01212367c396 100644
> --- a/board/gateworks/gw_ventana/gw_ventana.c
> +++ b/board/gateworks/gw_ventana/gw_ventana.c
> @@ -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);
>         }
> --
> 2.0.4
>

Thanks for catching and fixing this Thierry!

Acked-by: Tim Harvey <tharvey@gateworks.com>

Tim
Stefano Babic Sept. 9, 2014, 2:27 p.m. UTC | #2
On 22/08/2014 09:46, Thierry Reding wrote:
> From: Thierry Reding <treding@nvidia.com>
> 
> 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.
> 
> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---

Applied to u-boot-imx, thanks !

Best regards,
Stefano Babic
diff mbox

Patch

diff --git a/board/gateworks/gw_ventana/gw_ventana.c b/board/gateworks/gw_ventana/gw_ventana.c
index 9d2651f0cbf1..01212367c396 100644
--- a/board/gateworks/gw_ventana/gw_ventana.c
+++ b/board/gateworks/gw_ventana/gw_ventana.c
@@ -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);
 	}