Skip to content

Commit e618303

Browse files
committed
Media (macOS): try fixing error CFStringGetCString() failed
1 parent 748b7df commit e618303

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/util/apple/cf_helpers.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

tests/strbuf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ int main(void)
381381
ffStrbufEnsureFixedLengthFree(&strbuf, 10);
382382
VERIFY(strbuf.length == 0);
383383
VERIFY(strbuf.allocated == 11);
384+
ffStrbufEnsureFixedLengthFree(&strbuf, 12);
385+
VERIFY(strbuf.length == 0);
386+
VERIFY(strbuf.allocated == 13);
384387
ffStrbufDestroy(&strbuf);
385388

386389
//ffStrbufEnsureFixedLengthFree / empty buffer with zero free length

0 commit comments

Comments
 (0)