Media (macOS): try fixing error CFStringGetCString() failed

This commit is contained in:
李通洲
2024-11-12 21:52:12 +08:00
parent 748b7df0d7
commit e618303343
2 changed files with 15 additions and 5 deletions
+12 -5
View File
@@ -49,15 +49,22 @@ const char* ffCfStrGetString(CFTypeRef cf, FFstrbuf* result)
if (CFGetTypeID(cf) == CFStringGetTypeID())
{
CFStringRef cfStr = (CFStringRef)cf;
uint32_t length = (uint32_t)CFStringGetLength(cfStr);
//CFString stores UTF16 characters, therefore may require larger buffer to convert to UTF8 string
ffStrbufEnsureFree(result, length * 2);
if (!CFStringGetCString(cfStr, result->chars, result->allocated, kCFStringEncodingUTF8))
const char* cstr = CFStringGetCStringPtr(cfStr, kCFStringEncodingUTF8);
if (cstr)
{
ffStrbufEnsureFree(result, length * 4);
ffStrbufSetS(result, cstr);
return NULL;
}
else
{
uint32_t length = CFStringGetLength(cfStr);
uint32_t maxLength = (uint32_t) CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
ffStrbufEnsureFixedLengthFree(result, maxLength);
if(!CFStringGetCString(cfStr, result->chars, result->allocated, kCFStringEncodingUTF8))
return "CFStringGetCString() failed";
}
// CFStringGetCString ensures the buffer is NUL terminated
// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring
result->length = (uint32_t) strnlen(result->chars, (uint32_t)result->allocated);
+3
View File
@@ -381,6 +381,9 @@ int main(void)
ffStrbufEnsureFixedLengthFree(&strbuf, 10);
VERIFY(strbuf.length == 0);
VERIFY(strbuf.allocated == 11);
ffStrbufEnsureFixedLengthFree(&strbuf, 12);
VERIFY(strbuf.length == 0);
VERIFY(strbuf.allocated == 13);
ffStrbufDestroy(&strbuf);
//ffStrbufEnsureFixedLengthFree / empty buffer with zero free length