From 171a521b91add62c19343100df62b15ecd8f3216 Mon Sep 17 00:00:00 2001 From: "Mark A. Tsuchida" Date: Sat, 21 Dec 2013 13:01:28 -0800 Subject: [PATCH] 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. --- windows/hid.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/windows/hid.c b/windows/hid.c index 179f88e..b355677 100755 --- a/windows/hid.c +++ b/windows/hid.c @@ -662,6 +662,7 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char * int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds) { DWORD bytes_read = 0; + size_t copy_len = 0; BOOL res; /* Copy the handle for convenience. */ @@ -709,14 +710,13 @@ int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char number (0x0) on the beginning of the report anyway. To make this work like the other platforms, and to make it work more like the HID spec, we'll skip over this byte. */ - size_t copy_len; bytes_read--; copy_len = length > bytes_read ? bytes_read : length; memcpy(data, dev->read_buf+1, copy_len); } else { /* Copy the whole buffer, report number and all. */ - size_t copy_len = length > bytes_read ? bytes_read : length; + copy_len = length > bytes_read ? bytes_read : length; memcpy(data, dev->read_buf, copy_len); } } @@ -727,7 +727,7 @@ int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char return -1; } - return bytes_read; + return copy_len; } int HID_API_EXPORT HID_API_CALL hid_read(hid_device *dev, unsigned char *data, size_t length)