[vlc-devel] [PATCH 1/3] freetype: Fix copying char buffer of CStringRef on darwin

Marvin Scholz epirat07 at gmail.com
Sat Jun 17 17:08:29 CEST 2017


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.
---
 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;
-- 
2.11.0 (Apple Git-81)



More information about the vlc-devel mailing list