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:
Vitali Lovich
2011-11-22 17:55:18 -08:00
committed by Alan Ott
parent c26a0c13f2
commit f4e138ac7a
+30 -12
View File
@@ -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;
}