@@ -49,15 +49,22 @@ const char* ffCfStrGetString(CFTypeRef cf, FFstrbuf* result)
4949    if  (CFGetTypeID (cf ) ==  CFStringGetTypeID ())
5050    {
5151        CFStringRef  cfStr  =  (CFStringRef )cf ;
52-         uint32_t  length  =  (uint32_t )CFStringGetLength (cfStr );
53-         //CFString stores UTF16 characters, therefore may require larger buffer to convert to UTF8 string 
54-         ffStrbufEnsureFree (result , length  *  2 );
55-         if  (!CFStringGetCString (cfStr , result -> chars , result -> allocated , kCFStringEncodingUTF8 ))
52+ 
53+         const  char *  cstr  =  CFStringGetCStringPtr (cfStr , kCFStringEncodingUTF8 );
54+         if  (cstr )
55+         {
56+             ffStrbufSetS (result , cstr );
57+             return  NULL ;
58+         }
59+         else 
5660        {
57-             ffStrbufEnsureFree (result , length  *  4 );
61+             uint32_t  length  =  CFStringGetLength (cfStr );
62+             uint32_t  maxLength  =  (uint32_t ) CFStringGetMaximumSizeForEncoding (length , kCFStringEncodingUTF8 );
63+             ffStrbufEnsureFixedLengthFree (result , maxLength );
5864            if (!CFStringGetCString (cfStr , result -> chars , result -> allocated , kCFStringEncodingUTF8 ))
5965                return  "CFStringGetCString() failed" ;
6066        }
67+ 
6168        // CFStringGetCString ensures the buffer is NUL terminated 
6269        // https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring 
6370        result -> length  =  (uint32_t ) strnlen (result -> chars , (uint32_t )result -> allocated );
0 commit comments