]> granicus.if.org Git - libass/commitdiff
coretext: fix conversion from CFStringRef to utf8 buffer
authorStefano Pigozzi <stefano.pigozzi@gmail.com>
Fri, 6 Dec 2013 17:36:24 +0000 (18:36 +0100)
committerGrigori Goronzy <greg@chown.ath.cx>
Fri, 10 Jul 2015 08:42:40 +0000 (10:42 +0200)
The code incorrectly assumed that the utf8 characters could always be
represented with only one byte. This commit queries CFStringRef instances for
the actual amount of bytes needed.

libass/ass_coretext.c

index ad054437f6d9e1e589bed0f5ea904bcbb681ced0..bf2e30140bc2e4ef58cc8cd870a07840ca52abc4 100644 (file)
 
 static char *cfstr2buf(CFStringRef string)
 {
-    const char *buf_ptr = CFStringGetCStringPtr(string, kCFStringEncodingUTF8);
+    const int encoding = kCFStringEncodingUTF8;
+    const char *buf_ptr = CFStringGetCStringPtr(string, encoding);
     if (buf_ptr) {
         return strdup(buf_ptr);
     } else {
-        size_t buf_len = CFStringGetLength(string) + 1;
+        size_t len = CFStringGetLength(string);
+        CFIndex buf_len = CFStringGetMaximumSizeForEncoding(len, encoding);
         char *buf = malloc(buf_len);
-        CFStringGetCString(string, buf, buf_len, kCFStringEncodingUTF8);
+        CFStringGetCString(string, buf, buf_len, encoding);
         return buf;
     }
 }