Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit 171a521

Browse files
marktsuchidasignal11
authored andcommitted
windows: Limit hid_read() return value to buffer length
hid_read() and hid_read_timeout() now return the number of bytes copied, instead of the number of bytes returned by GetOverlappekdResult() (which is the maximum report size for the device). This limits the return value to the requested length (buffer size), matching the behavior on other platforms.
1 parent 99d0219 commit 171a521

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

windows/hid.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *
662662
int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
663663
{
664664
DWORD bytes_read = 0;
665+
size_t copy_len = 0;
665666
BOOL res;
666667

667668
/* Copy the handle for convenience. */
@@ -709,14 +710,13 @@ int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char
709710
number (0x0) on the beginning of the report anyway. To make this
710711
work like the other platforms, and to make it work more like the
711712
HID spec, we'll skip over this byte. */
712-
size_t copy_len;
713713
bytes_read--;
714714
copy_len = length > bytes_read ? bytes_read : length;
715715
memcpy(data, dev->read_buf+1, copy_len);
716716
}
717717
else {
718718
/* Copy the whole buffer, report number and all. */
719-
size_t copy_len = length > bytes_read ? bytes_read : length;
719+
copy_len = length > bytes_read ? bytes_read : length;
720720
memcpy(data, dev->read_buf, copy_len);
721721
}
722722
}
@@ -727,7 +727,7 @@ int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char
727727
return -1;
728728
}
729729

730-
return bytes_read;
730+
return copy_len;
731731
}
732732

733733
int HID_API_EXPORT HID_API_CALL hid_read(hid_device *dev, unsigned char *data, size_t length)

0 commit comments

Comments
 (0)