In upd64031a_read(), i2c_master_recv() is called without checking
its return value. If the I2C read fails, the function proceeds to
return invalid data from the buffer.
Add proper error checking:
- Check if all 2 bytes were read successfully
- Log the error with %pe format
- Return 0xff (the same as invalid register) on error
Signed-off-by: Wenyuan Li <2063309626@qq.com>
---
drivers/media/i2c/upd64031a.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/drivers/media/i2c/upd64031a.c b/drivers/media/i2c/upd64031a.c
index a178af46e695..ec9179cf9b69 100644
--- a/drivers/media/i2c/upd64031a.c
+++ b/drivers/media/i2c/upd64031a.c
@@ -73,10 +73,19 @@ static u8 upd64031a_read(struct v4l2_subdev *sd, u8 reg)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
u8 buf[2];
+ int ret;
if (reg >= sizeof(buf))
return 0xff;
- i2c_master_recv(client, buf, 2);
+
+ ret = i2c_master_recv(client, buf, 2);
+ if (ret != sizeof(buf)) {
+ int err = ret < 0 ? ret : -EIO;
+
+ v4l2_err(sd, "I2C read failed: %pe\n", ERR_PTR(err));
+ return 0xff;
+ }
+
return buf[reg];
}
--
2.43.0