From 71775e36190b248c1ae08c36a4316f33c83bcccd Mon Sep 17 00:00:00 2001 From: gxalpha Date: Fri, 27 Sep 2024 20:51:13 +0200 Subject: [PATCH] libobs: Assure large enough buffer in dstr_from_cfstring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- libobs/obs-cocoa.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libobs/obs-cocoa.m b/libobs/obs-cocoa.m index 4019aed089394a..0bc4ffe1ebb3e4 100644 --- a/libobs/obs-cocoa.m +++ b/libobs/obs-cocoa.m @@ -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))