mirror of
https://github.com/RPCS3/hidapi.git
synced 2026-07-21 00:45:21 -04:00
Fix string copy logic in Mac backend
This makes sure that a valid range is given to the string copy & that the returned string always has a valid null-terminator.
This commit is contained in:
@@ -263,16 +263,25 @@ static int32_t get_max_report_length(IOHIDDeviceRef device)
|
||||
|
||||
static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t *buf, size_t len)
|
||||
{
|
||||
CFStringRef str = IOHIDDeviceGetProperty(device, prop);
|
||||
CFStringRef str;
|
||||
|
||||
if (!len)
|
||||
return 0;
|
||||
|
||||
buf[0] = 0x0000;
|
||||
str = IOHIDDeviceGetProperty(device, prop);
|
||||
|
||||
buf[0] = 0;
|
||||
|
||||
if (str) {
|
||||
len --;
|
||||
|
||||
CFIndex str_len = CFStringGetLength(str);
|
||||
CFRange range;
|
||||
range.location = 0;
|
||||
range.length = len;
|
||||
range.length = (str_len > len)? len: str_len;
|
||||
CFIndex used_buf_len;
|
||||
CFStringGetBytes(str,
|
||||
CFIndex chars_copied;
|
||||
chars_copied = CFStringGetBytes(str,
|
||||
range,
|
||||
kCFStringEncodingUTF32LE,
|
||||
(char)'?',
|
||||
@@ -280,8 +289,9 @@ static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t
|
||||
(UInt8*)buf,
|
||||
len,
|
||||
&used_buf_len);
|
||||
buf[len-1] = 0x00000000;
|
||||
return used_buf_len;
|
||||
|
||||
buf[chars_copied] = 0;
|
||||
return chars_copied;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
@@ -290,16 +300,24 @@ static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t
|
||||
|
||||
static int get_string_property_utf8(IOHIDDeviceRef device, CFStringRef prop, char *buf, size_t len)
|
||||
{
|
||||
CFStringRef str = IOHIDDeviceGetProperty(device, prop);
|
||||
CFStringRef str;
|
||||
if (!len)
|
||||
return 0;
|
||||
|
||||
buf[0] = 0x0000;
|
||||
str = IOHIDDeviceGetProperty(device, prop);
|
||||
|
||||
buf[0] = 0;
|
||||
|
||||
if (str) {
|
||||
len--;
|
||||
|
||||
CFIndex str_len = CFStringGetLength(str);
|
||||
CFRange range;
|
||||
range.location = 0;
|
||||
range.length = len;
|
||||
range.length = (str_len > len)? len: str_len;
|
||||
CFIndex used_buf_len;
|
||||
CFStringGetBytes(str,
|
||||
CFIndex chars_copied;
|
||||
chars_copied = CFStringGetBytes(str,
|
||||
range,
|
||||
kCFStringEncodingUTF8,
|
||||
(char)'?',
|
||||
@@ -307,12 +325,12 @@ static int get_string_property_utf8(IOHIDDeviceRef device, CFStringRef prop, cha
|
||||
(UInt8*)buf,
|
||||
len,
|
||||
&used_buf_len);
|
||||
buf[len-1] = 0x00000000;
|
||||
|
||||
buf[chars_copied] = 0;
|
||||
return used_buf_len;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user