From dc6c17067237d78ea9defa9cb6edd8eaa4ae64fa Mon Sep 17 00:00:00 2001 From: Alan Ott Date: Mon, 26 Nov 2012 13:59:33 -0500 Subject: [PATCH] mac: Fix string handling There were errors regarding the length of strings in conversion from CFString to wchar_t. Thanks to github user nikolajsheller for pointing this out in pull request #80. --- mac/hid.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/mac/hid.c b/mac/hid.c index 8cac198..7723bae 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -244,10 +244,14 @@ static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t (char)'?', FALSE, (UInt8*)buf, - len, + len * sizeof(wchar_t), &used_buf_len); - buf[chars_copied] = 0; + if (chars_copied == len) + buf[len] = 0; /* len is decremented above */ + else + buf[chars_copied] = 0; + return 0; } else @@ -271,7 +275,7 @@ static int get_string_property_utf8(IOHIDDeviceRef device, CFStringRef prop, cha CFIndex str_len = CFStringGetLength(str); CFRange range; range.location = 0; - range.length = (str_len > len)? len: str_len; + range.length = str_len; CFIndex used_buf_len; CFIndex chars_copied; chars_copied = CFStringGetBytes(str, @@ -283,7 +287,11 @@ static int get_string_property_utf8(IOHIDDeviceRef device, CFStringRef prop, cha len, &used_buf_len); - buf[chars_copied] = 0; + if (used_buf_len == len) + buf[len] = 0; /* len is decremented above */ + else + buf[used_buf_len] = 0; + return used_buf_len; } else