diff mbox

[U-Boot,1/2] m68k: fix an undefined behavior warning of M5253DEMO board

Message ID 1405994239-31834-2-git-send-email-yamada.m@jp.panasonic.com
State Accepted
Headers show

Commit Message

Masahiro Yamada July 22, 2014, 1:57 a.m. UTC
The latest GCC is so clever that it reports more warnings
than old ones did:

 ------------------------------>8------------------------------

  board/freescale/m5253demo/flash.c: In function 'flash_get_offsets':
  board/freescale/m5253demo/flash.c:65:23: warning: iteration 2047u
  invokes undefined behavior [-Waggressive-loop-optimizations]
      info->start[k + 1] = info->start[k] + CONFIG_SYS_SST_SECTSZ;
                         ^
  board/freescale/m5253demo/flash.c:64:3: note: containing loop
     for (k = 0, j = 0; j < CONFIG_SYS_SST_SECT; j++, k++) {
     ^

 ------------------------------8<------------------------------

The cause of the warning is like this:

The for statement iterates 2048 times in flash_get_offsets() func.
(Notice CONFIG_SYS_SST_SECT is defined as 2048)

The last iteration does
  info->start[2048] = info->start[2047] + CONFIG_SYS_SST_SECTSZ;
causing an undefined behavior.

(Please note the array size of info->start is 2048.
CONFIG_SYS_MAX_FLASH_SECT is defined as 2048 for this board.)

This commit fixes that so as not to overrun the info->start array.

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
Cc: Jason Jin <Jason.jin@freescale.com>
---

 board/freescale/m5253demo/flash.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Tom Rini July 22, 2014, 7:24 p.m. UTC | #1
On Tue, Jul 22, 2014 at 10:57:18AM +0900, Masahiro Yamada wrote:

> The latest GCC is so clever that it reports more warnings
> than old ones did:
> 
>  ------------------------------>8------------------------------
> 
>   board/freescale/m5253demo/flash.c: In function 'flash_get_offsets':
>   board/freescale/m5253demo/flash.c:65:23: warning: iteration 2047u
>   invokes undefined behavior [-Waggressive-loop-optimizations]
>       info->start[k + 1] = info->start[k] + CONFIG_SYS_SST_SECTSZ;
>                          ^
>   board/freescale/m5253demo/flash.c:64:3: note: containing loop
>      for (k = 0, j = 0; j < CONFIG_SYS_SST_SECT; j++, k++) {
>      ^
> 
>  ------------------------------8<------------------------------
> 
> The cause of the warning is like this:
> 
> The for statement iterates 2048 times in flash_get_offsets() func.
> (Notice CONFIG_SYS_SST_SECT is defined as 2048)
> 
> The last iteration does
>   info->start[2048] = info->start[2047] + CONFIG_SYS_SST_SECTSZ;
> causing an undefined behavior.
> 
> (Please note the array size of info->start is 2048.
> CONFIG_SYS_MAX_FLASH_SECT is defined as 2048 for this board.)
> 
> This commit fixes that so as not to overrun the info->start array.
> 
> Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
> Cc: Jason Jin <Jason.jin@freescale.com>

Applied to u-boot/master, thanks!
diff mbox

Patch

diff --git a/board/freescale/m5253demo/flash.c b/board/freescale/m5253demo/flash.c
index 16bba59..071701d 100644
--- a/board/freescale/m5253demo/flash.c
+++ b/board/freescale/m5253demo/flash.c
@@ -56,14 +56,16 @@  ulong flash_init(void)
 
 int flash_get_offsets(ulong base, flash_info_t * info)
 {
-	int j, k;
+	int i;
 
 	if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
 
 		info->start[0] = base;
-		for (k = 0, j = 0; j < CONFIG_SYS_SST_SECT; j++, k++) {
-			info->start[k + 1] = info->start[k] + CONFIG_SYS_SST_SECTSZ;
-			info->protect[k] = 0;
+		info->protect[0] = 0;
+		for (i = 1; i < CONFIG_SYS_SST_SECT; i++) {
+			info->start[i] = info->start[i - 1]
+						+ CONFIG_SYS_SST_SECTSZ;
+			info->protect[i] = 0;
 		}
 	}