Skip to content

Commit 82f09a6

Browse files
MichaelZaidmanJiri Kosina
authored andcommitted
HID: ft260: improve error handling of ft260_hid_feature_report_get()
The ft260_hid_feature_report_get() checks if the return size matches the requested size. But the function can also fail with at least -ENOMEM. Add the < 0 checks. In ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer if there is an error. Fixes: 6a82582 ("HID: ft260: add usb hid to i2c host bridge driver") Signed-off-by: Tom Rix <[email protected]> Signed-off-by: Michael Zaidman <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 4fb1251 commit 82f09a6

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

drivers/hid/hid-ft260.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ static int ft260_hid_feature_report_get(struct hid_device *hdev,
249249

250250
ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT,
251251
HID_REQ_GET_REPORT);
252-
memcpy(data, buf, len);
252+
if (likely(ret == len))
253+
memcpy(data, buf, len);
254+
else if (ret >= 0)
255+
ret = -EIO;
253256
kfree(buf);
254257
return ret;
255258
}
@@ -298,7 +301,7 @@ static int ft260_xfer_status(struct ft260_device *dev)
298301

299302
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
300303
(u8 *)&report, sizeof(report));
301-
if (ret < 0) {
304+
if (unlikely(ret < 0)) {
302305
hid_err(hdev, "failed to retrieve status: %d\n", ret);
303306
return ret;
304307
}
@@ -724,10 +727,9 @@ static int ft260_get_system_config(struct hid_device *hdev,
724727

725728
ret = ft260_hid_feature_report_get(hdev, FT260_SYSTEM_SETTINGS,
726729
(u8 *)cfg, len);
727-
if (ret != len) {
730+
if (ret < 0) {
728731
hid_err(hdev, "failed to retrieve system status\n");
729-
if (ret >= 0)
730-
return -EIO;
732+
return ret;
731733
}
732734
return 0;
733735
}
@@ -780,8 +782,8 @@ static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,
780782
int ret;
781783

782784
ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
783-
if (ret != len && ret >= 0)
784-
return -EIO;
785+
if (ret < 0)
786+
return ret;
785787

786788
return scnprintf(buf, PAGE_SIZE, "%hi\n", *field);
787789
}
@@ -792,8 +794,8 @@ static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
792794
int ret;
793795

794796
ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
795-
if (ret != len && ret >= 0)
796-
return -EIO;
797+
if (ret < 0)
798+
return ret;
797799

798800
return scnprintf(buf, PAGE_SIZE, "%hi\n", le16_to_cpu(*field));
799801
}
@@ -944,10 +946,8 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
944946

945947
ret = ft260_hid_feature_report_get(hdev, FT260_CHIP_VERSION,
946948
(u8 *)&version, sizeof(version));
947-
if (ret != sizeof(version)) {
949+
if (ret < 0) {
948950
hid_err(hdev, "failed to retrieve chip version\n");
949-
if (ret >= 0)
950-
ret = -EIO;
951951
goto err_hid_close;
952952
}
953953

0 commit comments

Comments
 (0)