Skip to contentSkip to conversation

i2c: busses: fix uninit-value in pci1xxxx_i2c_xfer

PATCH In review Suraj Sonawane 2 participants 3 messages Oct 22, 2024 – Oct 26, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: busses: fix uninit-value in pci1xxxx_i2c_xfer

Fix an issue reported by the smatch static analysis tool:
drivers/i2c/busses/i2c-mchp-pci1xxxx.c:1030 pci1xxxx_i2c_xfer() error:
uninitialized symbol 'retval'.

The error occurs because retval may be used without being set if the transfer loop does not execute (e.g., when num is 0). This could cause the function to return an undefined value, leading to unpredictable behavior.

Initialize retval to 0 before the transfer loop to ensure that the function returns a valid value even if no transfers are processed. This change also preserves proper error handling within the loop.

---
 drivers/i2c/busses/i2c-mchp-pci1xxxx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-mchp-pci1xxxx.c +1 -1
index 5ef136c3e..4dfa11650 100644
@@ -994,7 +994,7 @@ static int pci1xxxx_i2c_xfer(struct i2c_adapter *adap,
 {
 	struct pci1xxxx_i2c *i2c = i2c_get_adapdata(adap);
 	u8 slaveaddr;
-	int retval;
+	int retval = 0;
 	u32 i;
 
 	i2c->i2c_xfer_in_progress = true;
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hi Suraj,

Thanks for your patch, but...

Quoted 8 lines · Suraj Sonawane

Fix an issue reported by the smatch static analysis tool:
drivers/i2c/busses/i2c-mchp-pci1xxxx.c:1030 pci1xxxx_i2c_xfer() error:
uninitialized symbol 'retval'.

The error occurs because retval may be used without being set if the transfer loop does not execute (e.g., when num is 0). This could cause the function to return an undefined value, leading to unpredictable behavior.

... if num is '0', then we would never reach here, check __i2c_transfer().

Thanks,
Andi

Quoted 5 lines

Initialize retval to 0 before the transfer loop to ensure that the function returns a valid value even if no transfers are processed. This change also preserves proper error handling within the loop.

#2 lore raw
Quoted 14 lines · Andi Shyti

Hi Suraj,

Thanks for your patch, but...

Quoted 8 lines · Suraj Sonawane

Fix an issue reported by the smatch static analysis tool:
drivers/i2c/busses/i2c-mchp-pci1xxxx.c:1030 pci1xxxx_i2c_xfer() error:
uninitialized symbol 'retval'.

The error occurs because retval may be used without being set if the transfer loop does not execute (e.g., when num is 0). This could cause the function to return an undefined value, leading to unpredictable behavior.

Quoted 11 lines

... if num is '0', then we would never reach here, check __i2c_transfer().

Thanks,
Andi

Quoted 5 lines

Initialize retval to 0 before the transfer loop to ensure that the function returns a valid value even if no transfers are processed. This change also preserves proper error handling within the loop.

Thank you for reviewing and clarifying.

Best regards,
Suraj Sonawane