Skip to contentSkip to conversation

rtc: pcf2127: fix uninitialized variable msg

PATCH New Biwen Li 4 participants 6 messages Jul 9, 2020 – Sep 23, 2020 Real time clock (RTC)

lore
Share & tools
lore

Biwen Li

author
#0 lore raw

PATCH 1/1 rtc: pcf2127: fix uninitialized variable msg

From: Biwen Li <[email protected]>

Fix uninitialized variable msg

---
 drivers/rtc/pcf2127.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/rtc/pcf2127.c +2 -1
index f695350..58c4ee9 100644
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2016 by NXP Semiconductors Inc.
  * Date & Time support for PCF2127 RTC
+ * Copyright 2020 NXP
  */
 
 /*	#define	DEBUG	*/
@@ -26,7 +27,7 @@ static int pcf2127_read_reg(struct udevice *dev, uint offset,
 			    u8 *buffer, int len)
 {
 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
-	struct i2c_msg msg;
+	struct i2c_msg msg = {0};
 	int ret;
 
 	/* Set the address of the start register to be read */
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/1 rtc: pcf2127: fix uninitialized variable msg

drivers/rtc/pcf2127.c static int pcf2127_read_reg(struct udevice *dev, uint offset, · line 33

Quoted 11 lines · Biwen Li

From: Biwen Li <[email protected]>

Fix uninitialized variable msg

drivers/rtc/pcf2127.c +1 -1
 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
-	struct i2c_msg msg;
+	struct i2c_msg msg = {0};
 	int ret;
 
 	/* Set the address of the start register to be read */

I assume it's the

        msg.flags |= I2C_M_RD;

line that is warned about (please include such info)? Isn't the right fix to replace that by

        msg.flags = I2C_M_RD;

?

Rasmus

#2 lore raw
Quoted 16 lines · Biwen Li

drivers/rtc/pcf2127.c static int pcf2127_read_reg(struct udevice *dev, uint offset, · line 33

Quoted 11 lines

From: Biwen Li <[email protected]>

Fix uninitialized variable msg

      struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
drivers/rtc/pcf2127.c +1 -1
-     struct i2c_msg msg;
+     struct i2c_msg msg = {0};
      int ret;
 
      /* Set the address of the start register to be read */

I assume it's the

Quoted 8 lines
        msg.flags |= I2C_M_RD;

line that is warned about (please include such info)? Isn't the right fix to replace that by

        msg.flags = I2C_M_RD;

?

Two lines("struct i2c msg;" and "msg.flags |= I2C_M_RD") are warned by build system. Initializing msg variable will be better.

Quoted 2 lines

Rasmus

#3 lore raw
Quoted 18 lines · Biwen Li
Quoted 18 lines · Biwen Li

drivers/rtc/pcf2127.c static int pcf2127_read_reg(struct udevice *dev, uint offset, · line 33

Quoted 11 lines

From: Biwen Li <[email protected]>

Fix uninitialized variable msg

      struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
drivers/rtc/pcf2127.c +1 -1
-     struct i2c_msg msg;
+     struct i2c_msg msg = {0};
      int ret;
 
      /* Set the address of the start register to be read */

I assume it's the

        msg.flags |= I2C_M_RD;
Quoted 7 lines
Quoted 6 lines

line that is warned about (please include such info)? Isn't the right fix to replace that by

        msg.flags = I2C_M_RD;

?

Two lines("struct i2c msg;" and "msg.flags |= I2C_M_RD") are warned by build system.

What? That doesn't make sense. Perhaps your compiler is just friendly enough to show you the declaration of the struct i2c_msg, but clearly declaring an automatic variable without initializing it is not, by itself, wrong. Otherwise "int ret;" in that very same function should be warned about, and 100000 other instances in any code base written in C.

Please show the exact output from the compiler and the compiler version.

Quoted 1 line

Initializing msg variable will be better.

No, understanding the cause of the warning and fixing that is clearly better.

Rasmus

#4 lore raw
Quoted 29 lines · Biwen Li
Quoted 18 lines
Quoted 18 lines · Biwen Li

drivers/rtc/pcf2127.c static int pcf2127_read_reg(struct udevice *dev, uint offset, · line 33

Quoted 11 lines

From: Biwen Li <[email protected]>

Fix uninitialized variable msg

      struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
drivers/rtc/pcf2127.c +1 -1
-     struct i2c_msg msg;
+     struct i2c_msg msg = {0};
      int ret;
 
      /* Set the address of the start register to be read */

I assume it's the

        msg.flags |= I2C_M_RD;
Quoted 7 lines
Quoted 6 lines

line that is warned about (please include such info)? Isn't the right fix to replace that by

        msg.flags = I2C_M_RD;

?

Two lines("struct i2c msg;" and "msg.flags |= I2C_M_RD") are warned by

build system.

Quoted 7 lines

What? That doesn't make sense. Perhaps your compiler is just friendly enough to show you the declaration of the struct i2c_msg, but clearly declaring an automatic variable without initializing it is not, by itself, wrong. Otherwise "int ret;" in that very same function should be warned about, and 100000 other instances in any code base written in C.

Please show the exact output from the compiler and the compiler version.

Sorry, acctually not build system. It's code analysis tool.
Such as:
Declaring variable msg without initializer. (struce i2c_msg msg;)
Uninitialized scalar variable (UNINIT), uninit_use: Using uninitialized value msg.flags.(msg.flags |= I2C_M_RD)

Quoted 6 lines
Quoted 2 lines

Initializing msg variable will be better.

No, understanding the cause of the warning and fixing that is clearly better.

Rasmus

#5 lore raw
Quoted 39 lines

-----Original Message-----
From: U-Boot <[email protected]> On Behalf Of Biwen Li (OSS)
Sent: Friday, July 10, 2020 7:24 AM
To: Rasmus Villemoes <[email protected]>; Biwen Li (OSS) <[email protected]>; Jagdish Gediya <[email protected]>; Priyanka Jain <[email protected]>
Cc: Jiafei Pan <[email protected]>; u-boot at lists.denx.de
Subject: RE: [EXT] Re: [PATCH] rtc: pcf2127: fix uninitialized variable msg

Quoted 30 lines · Biwen Li
Quoted 18 lines
Quoted 18 lines · Biwen Li

drivers/rtc/pcf2127.c static int pcf2127_read_reg(struct udevice *dev, uint offset, · line 33

Quoted 11 lines

From: Biwen Li <[email protected]>

Fix uninitialized variable msg

      struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
drivers/rtc/pcf2127.c +1 -1
-     struct i2c_msg msg;
+     struct i2c_msg msg = {0};
      int ret;
 
      /* Set the address of the start register to be read */

I assume it's the

        msg.flags |= I2C_M_RD;
Quoted 8 lines
Quoted 6 lines

line that is warned about (please include such info)? Isn't the right fix to replace that by

        msg.flags = I2C_M_RD;

?

Two lines("struct i2c msg;" and "msg.flags |= I2C_M_RD") are warned by

build system.

Quoted 8 lines
Quoted 7 lines

What? That doesn't make sense. Perhaps your compiler is just friendly enough to show you the declaration of the struct i2c_msg, but clearly declaring an automatic variable without initializing it is not, by itself, wrong. Otherwise "int ret;" in that very same function should be warned about, and 100000 other instances in any code base written in C.

Please show the exact output from the compiler and the compiler version.

Sorry, acctually not build system. It's code analysis tool.

Can you please update subject or description stating that you are fixing issue reported by static analysis tool coverity

Thanks
Priyanka

Quoted 10 lines

Such as:
Declaring variable msg without initializer. (struce i2c_msg msg;) Uninitialized scalar variable (UNINIT), uninit_use: Using uninitialized value msg.flags.(msg.flags |= I2C_M_RD)

Quoted 6 lines
Quoted 2 lines

Initializing msg variable will be better.

No, understanding the cause of the warning and fixing that is clearly better.

Rasmus