Skip to contentSkip to conversation

media: i2c: upd64031a: add error handling for I2C read

PATCH In review Wenyuan Li 2 participants 2 messages Mar 31 – May 5 Media (V4L/DVB)

lore
Share & tools
lore

Wenyuan Li

author
#0 lore raw

PATCH 1/1 media: i2c: upd64031a: add error handling for I2C read

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

---
 drivers/media/i2c/upd64031a.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/media/i2c/upd64031a.c +10 -1
index a178af46e695..ec9179cf9b69 100644
@@ -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];
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · Wenyuan Li

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

I'm not picking this up: it's not worth the effort, and this read is used for debugging only. Failures won't hurt anything.

Ditto for your upd64083 patch.

Regards,

Hans

Quoted 31 lines
---
 drivers/media/i2c/upd64031a.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/media/i2c/upd64031a.c +10 -1
index a178af46e695..ec9179cf9b69 100644
@@ -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];
 }