libobs: Assure large enough buffer in dstr_from_cfstring

Per the documentation of CFStringGetCString, the buffer provided must be
large enough not just for the string itself, but also for a NUL
terminator. This space for the NUL terminator is currently ignored, and
we just get lucky that CFStringGetMaximumSizeForEncoding often
dramatically overestimates the space required. However, it is possible
to actually hit the maximum with the string itself (for example by using
strings that contain exclusively Chinese characters such as "我"), in
which case the conversion fails. Adding the extra byte for the NUL
terminator fixes this.
At this point, we can also safely assert that our max_size is larger
than zero, silencing a clang analyzer warning that now is no longer
valid.
This commit is contained in:
gxalpha 2024-09-27 20:51:13 +02:00 committed by Ryan Foster
parent 42670ab4e3
commit 71775e3619

View File

@ -166,7 +166,9 @@ void log_system_info(void)
static bool dstr_from_cfstring(struct dstr *str, CFStringRef ref)
{
CFIndex length = CFStringGetLength(ref);
CFIndex max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
CFIndex max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
assert(max_size > 0);
dstr_reserve(str, max_size);
if (!CFStringGetCString(ref, str->array, max_size, kCFStringEncodingUTF8))