Skip to content

Commit 7620bc7

Browse files
authored
Guard against zero length buffers in hid_write (#279)
1 parent fc8fdd2 commit 7620bc7

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

libusb/hid.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,9 +1043,15 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
10431043
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
10441044
{
10451045
int res;
1046-
int report_number = data[0];
1046+
int report_number;
10471047
int skipped_report_id = 0;
10481048

1049+
if (!data || (length ==0)) {
1050+
return -1;
1051+
}
1052+
1053+
report_number = data[0];
1054+
10491055
if (report_number == 0x0) {
10501056
data++;
10511057
length--;

linux/hid.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,12 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
963963
{
964964
int bytes_written;
965965

966+
if (!data || (length == 0)) {
967+
errno = EINVAL;
968+
register_device_error(dev, strerror(errno));
969+
return -1;
970+
}
971+
966972
bytes_written = write(dev->device_handle, data, length);
967973

968974
register_device_error(dev, (bytes_written == -1)? strerror(errno): NULL);

mac/hid.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,13 @@ static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char
898898
const unsigned char *data_to_send = data;
899899
CFIndex length_to_send = length;
900900
IOReturn res;
901-
const unsigned char report_id = data[0];
901+
unsigned char report_id;
902+
903+
if (!data || (length == 0)) {
904+
return -1;
905+
}
906+
907+
report_id = data[0];
902908

903909
if (report_id == 0x0) {
904910
/* Not using numbered Reports.

windows/hid.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *
659659

660660
unsigned char *buf;
661661

662+
if (!data || (length==0)) {
663+
register_error(dev, "Zero length buffer");
664+
return function_result;
665+
}
666+
662667
/* Make sure the right number of bytes are passed to WriteFile. Windows
663668
expects the number of bytes which are in the _longest_ report (plus
664669
one for the report number) bytes even if the data is a report

0 commit comments

Comments
 (0)