[PATCH] i2c: smbus: Fixed uninitialized variable msgbuf in i2c_smbus_xfer_emulated

From: cuiguoqi
Date: Fri Dec 15 2023 - 01:35:38 EST


From: cuiguoqi <cuiguoqi@xxxxxxxxxx>

When the slave device is disconnected during i2c transmission, the communication is interrupted because the ACK cannot be received.
However, if the uninitialized msgbuf1 is used by i2c_smbus_xfer_emulated, abnormal random values will be reported.
The test proves that the exception communication using SMBUS after initialization is more reliable.

Signed-off-by: cuiguoqi <cuiguoqi@xxxxxxxxxx>
---
drivers/i2c/i2c-core-smbus.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c
index e3b96fc..f7b0980d 100644
--- a/drivers/i2c/i2c-core-smbus.c
+++ b/drivers/i2c/i2c-core-smbus.c
@@ -351,6 +351,8 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
&& size != I2C_SMBUS_I2C_BLOCK_DATA);

+ memset(msgbuf0, 0, sizeof(msgbuf0));
+ memset(msgbuf1, 0, sizeof(msgbuf1));
msgbuf0[0] = command;
switch (size) {
case I2C_SMBUS_QUICK:
--
2.7.4