Skip to content

Commit

Permalink
libobs: Assure large enough buffer in dstr_from_cfstring
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gxalpha authored and RytoEX committed Oct 4, 2024
1 parent 42670ab commit 71775e3
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion libobs/obs-cocoa.m
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 71775e3

Please sign in to comment.