Skip to content

Commit 901512e

Browse files
amullins83signal11
authored andcommitted
windows: Set maximum buffer for USB strings to 0xFFF wide characters
Limit the size of the buffer passed to hid_get_manufacturer_string(), hid_get_product_string(), and hid_get_serial_number_string() because calling the underlying win32 functions with larger buffers causes them to fail.
1 parent ae15b65 commit 901512e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

windows/hid.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ typedef LONG NTSTATUS;
3636
#define _wcsdup wcsdup
3737
#endif
3838

39+
/* The maximum number of characters that can be passed into the
40+
HidD_Get*String() functions without it failing.*/
41+
#define MAX_STRING_WCHARS 0xFFF
42+
3943
/*#define HIDAPI_USE_DDK*/
4044

4145
#ifdef __cplusplus
@@ -62,6 +66,9 @@ extern "C" {
6266

6367
#include "hidapi.h"
6468

69+
#undef MIN
70+
#define MIN(x,y) ((x) < (y)? (x): (y))
71+
6572
#ifdef _MSC_VER
6673
/* Thanks Microsoft, but I know how to use strncpy(). */
6774
#pragma warning(disable:4996)
@@ -811,7 +818,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev
811818
{
812819
BOOL res;
813820

814-
res = HidD_GetManufacturerString(dev->device_handle, string, sizeof(wchar_t) * maxlen);
821+
res = HidD_GetManufacturerString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS));
815822
if (!res) {
816823
register_error(dev, "HidD_GetManufacturerString");
817824
return -1;
@@ -824,7 +831,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wch
824831
{
825832
BOOL res;
826833

827-
res = HidD_GetProductString(dev->device_handle, string, sizeof(wchar_t) * maxlen);
834+
res = HidD_GetProductString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS));
828835
if (!res) {
829836
register_error(dev, "HidD_GetProductString");
830837
return -1;
@@ -837,7 +844,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *de
837844
{
838845
BOOL res;
839846

840-
res = HidD_GetSerialNumberString(dev->device_handle, string, sizeof(wchar_t) * maxlen);
847+
res = HidD_GetSerialNumberString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS));
841848
if (!res) {
842849
register_error(dev, "HidD_GetSerialNumberString");
843850
return -1;
@@ -850,7 +857,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int
850857
{
851858
BOOL res;
852859

853-
res = HidD_GetIndexedString(dev->device_handle, string_index, string, sizeof(wchar_t) * maxlen);
860+
res = HidD_GetIndexedString(dev->device_handle, string_index, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS));
854861
if (!res) {
855862
register_error(dev, "HidD_GetIndexedString");
856863
return -1;

0 commit comments

Comments
 (0)