[U-Boot] [PATCH 2/2] fsl: sys_eeprom: Fix 'may be used uninitialized' warning

Anton Vorontsov avorontsov at ru.mvista.com
Tue Sep 1 20:06:13 CEST 2009


On Tue, Sep 01, 2009 at 09:38:14PM +0400, Anton Vorontsov wrote:
[...]
> > >static int prog_eeprom(void)
> > >{
> > >-	int ret, i;
> > >+	int uninitialized_var(ret);
> > >+	int i;
> > 
> > why don't we init ret = 0;  seems like we should be doing that since
> > we might not enter the for loop
> 
> No, we always enter the for loop:
> 
> for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
> 
> sizeof(e) always > 0 because:
> 
> #if !defined(CONFIG_SYS_I2C_EEPROM_CCID) && !defined(CONFIG_SYS_I2C_EEPROM_NXID)
> #error "Please define either CONFIG_SYS_I2C_EEPROM_CCID or CONFIG_SYS_I2C_EEPROM_NXID"
> #endif
> 
> static struct __attribute__ ((__packed__)) eeprom {
> #ifdef CONFIG_SYS_I2C_EEPROM_CCID
>         u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'CCID' */
> ...
> #endif
> #ifdef CONFIG_SYS_I2C_EEPROM_NXID
>         u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'NXID' */
> ...
> #endif
> } e;

Another option is to turn the for loop into do {} while, so
we can avoid unitialized_var() usage. Something like that:

diff --git a/board/freescale/common/sys_eeprom.c b/board/freescale/common/sys_eeprom.c
index c0fff68..a4e0980 100644
--- a/board/freescale/common/sys_eeprom.c
+++ b/board/freescale/common/sys_eeprom.c
@@ -204,8 +204,8 @@ static void update_crc(void)
  */
 static int prog_eeprom(void)
 {
-	int ret, i;
-	void *p;
+	int ret;
+	int i = 0;
 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
 	unsigned int bus;
 #endif
@@ -224,13 +224,14 @@ static int prog_eeprom(void)
 	i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
 #endif
 
-	for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
+	do {
 		ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
-			p, min((sizeof(e) - i), 8));
+			(void *)&e + i, min((sizeof(e) - i), 8));
 		if (ret)
 			break;
 		udelay(5000);	/* 5ms write cycle timing */
-	}
+		i += 8;
+	} while (i < sizeof(e));
 
 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
 	i2c_set_bus_num(bus);


More information about the U-Boot mailing list