[vlc-commits] freetype: Fix copying char buffer of CStringRef on darwin

Marvin Scholz git at videolan.org
Mon Jun 19 14:33:17 CEST 2017


vlc | branch: master | Marvin Scholz <epirat07 at gmail.com> | Sat Jun 17 16:57:34 2017 +0200| [83535f027312639d844095c0783a0669e054728b] | committer: Marvin Scholz

freetype: Fix copying char buffer of CStringRef on darwin

To convert a CStringRef to a char buffer, CFStringGetCStringPtr was
used, but the documentation states this can fail in some
circumstances and it does fail on 10.7 for example.

This commit fixes this by adding a helper function, which falls back to
CFStringGetCString, as suggested in the CFStringGetCStringPtr docs.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=83535f027312639d844095c0783a0669e054728b
---

 modules/text_renderer/freetype/fonts/darwin.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/modules/text_renderer/freetype/fonts/darwin.c b/modules/text_renderer/freetype/fonts/darwin.c
index 5225028d6f..c23ac8a388 100644
--- a/modules/text_renderer/freetype/fonts/darwin.c
+++ b/modules/text_renderer/freetype/fonts/darwin.c
@@ -42,15 +42,32 @@
 char* getPathForFontDescription(CTFontDescriptorRef fontDescriptor);
 void addNewFontToFamily(filter_t *p_filter, CTFontDescriptorRef iter, char *path, vlc_family_t *family);
 
+static char* getCStringCopyForCFStringRef(CFStringRef cfstring, CFStringEncoding encoding)
+{
+    // Try to get pointer directly
+    const char *cptr = CFStringGetCStringPtr(cfstring, encoding);
+    if (cptr) {
+        return strdup(cptr);
+    }
+
+    // If it fails, use CFStringGetCString
+    CFIndex len = CFStringGetLength(cfstring);
+    CFIndex size = CFStringGetMaximumSizeForEncoding(len, encoding);
+    char *buffer = calloc(len + 1, sizeof(char));
+
+    if (CFStringGetCString(cfstring, buffer, size, encoding)) {
+        return buffer;
+    } else {
+        free(buffer);
+        return NULL;
+    }
+}
+
 char* getPathForFontDescription(CTFontDescriptorRef fontDescriptor)
 {
     CFURLRef url = CTFontDescriptorCopyAttribute(fontDescriptor, kCTFontURLAttribute);
     CFStringRef path = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
-    char *cpath = (char *)CFStringGetCStringPtr(path, kCFStringEncodingUTF8);
-    char *retPath = NULL;
-    if (cpath) {
-        retPath = strdup(cpath);
-    }
+    char *retPath = getCStringCopyForCFStringRef(path, kCFStringEncodingUTF8);
     CFRelease(path);
     CFRelease(url);
     return retPath;



More information about the vlc-commits mailing list