Repository navigation
libobs/util: Treat unconvertible text as a failed mbs conversion - #263
Merged
zackees merged 2 commits intoOct 11, 2026
Merged
Conversation
mbstowcs and wcstombs return (size_t)-1 for text the current locale cannot convert: any non-ASCII byte or invalid UTF-8 under glibc's C locale, invalid UTF-8 under a UTF-8 locale, or a character such as U+4E2D in the C locale on every platform. os_mbs_to_wcs and os_wcs_to_mbs take that as a length: - without dst they return (size_t)-1, so the _ptr functions ask for bmalloc((SIZE_MAX + 1) * size), that is bmalloc(0), which crashes. os_mbs_to_utf8_ptr reaches it from X11 window titles in linux-capture (xcomposite-input.c), where Qt has set the user's locale. - with dst they write the NUL at dst[SIZE_MAX]. They also pass dst_size to mbstowcs/wcstombs, which fill all of it when the text does not fit, and the NUL then lands one past the end. The tests expect 0 and an empty string for unconvertible text, and text cut to dst_size - 1 elements in a buffer too small for it, with a guard element after the buffer. Both fail until the next commit (on Windows and Linux: `-1 != 0` and `4 != 3`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
os_mbs_to_wcs and os_wcs_to_mbs now return 0, with an empty string in dst, when mbstowcs/wcstombs return (size_t)-1, instead of using it as a length. The _ptr functions then allocate one element and return 0, so os_mbs_to_utf8_ptr and os_utf8_to_mbs_ptr fail cleanly; xcomposite's callers already treat 0 as failure. The conversion into dst is limited to dst_size - 1 elements, so the terminating NUL always stays in the buffer. Text that fits is unchanged. The oracle's copy of os_mbs_to_wcs (oracle/platform_conv_host.c) gets the same change, so it keeps matching platform.c. Co-Authored-By: Claude Opus 5.5 <[email protected]>
6 of 7 tasks
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 21, 2026
…rsions.c (obs-rust#1610) Behavior-neutral extraction before the Rust port: os_mbs_to_wcs, os_utf8_to_wcs, os_wcs_to_mbs, os_wcs_to_utf8, their _ptr forms, os_utf8_to_mbs_ptr and os_mbs_to_utf8_ptr move verbatim from libobs/util/platform.c.h into libobs/util/platform-conversions.c.h. They were all that was left in platform.c.h after slices 2 to 6, so the file is renamed unchanged (100% similarity). It stays in the libobs.platform unity group and precompiled header, and the oracle compiles it under the new name. No host conditionals, no signature or header change. test_platform_conversions characterizes every function on its own: ASCII, 2-, 3- and 4-byte UTF-8 (a surrogate pair where wchar_t is 16 bits), invalid UTF-8 and lone surrogates, len 0 against an explicit len, the NULL-dst size query, dst_size 0 and 1, an exact and a too-small dst, empty and NULL input, the _ptr allocation and free (bnum_allocs), and the mbs functions in the C locale and a UTF-8 locale, set by the test. Characterized, not endorsed: the mbs functions ignore len; a dst too small for the UTF-8 conversions gives an empty string where the mbs ones cut the text; off Windows the size query accepts an encoded surrogate that the conversion then refuses, and the decoder accepts overlong 3-byte forms, values above U+10FFFF and 5- and 6-byte sequences. Windows replaces invalid input with U+FFFD and drops a leading UTF-8 BOM. Earlier slice 1 work is b7216309-jpg's: obs-rust#263 fixed the mbs conversions' (size_t)-1 and too-small-buffer cases, which the test pins, and obs-rust#267 ported the Windows branch of util/utf8.c that the UTF-8 conversions call. Refs obs-rust#262
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The first step of slice 1 (conversions) of #262. Found while characterizing
platform.c: the C bugs get fixed first, and the port then copies the fixed behavior, as with AV1 (#103 before #105).Bug 1: unconvertible text is taken as a length
os_mbs_to_wcsandos_wcs_to_mbsnever check for(size_t)-1, whichmbstowcs/wcstombsreturn for text the current locale can't convert. Probed on both platforms:mbstowcs(NULL, "café" in UTF-8)mbstowcs(NULL, "bad \xff byte")wcstombs(NULL, L"cé中")Consequences:
dst: they return(size_t)-1, so the_ptrfunctions callbmalloc((SIZE_MAX + 1) * size), that isbmalloc(0), whichbcrashes.os_mbs_to_utf8_ptris called on X11 window titles inplugins/linux-capture/xcomposite-input.cwhen only the legacyWM_NAMEis set. Qt applies the user's locale, so a Latin-1 title under a UTF-8 locale crashes OBS.os_wcs_to_mbs_ptrandos_utf8_to_mbs_ptrcrash on text the locale can't represent, such as CJK in the "C" locale.dst: they write the NUL atdst[SIZE_MAX].Bug 2: off-by-one write with a small buffer
With
dst, the conversion is called withdst_sizeas its limit.mbstowcs/wcstombsfill all of it when the text doesn't fit, anddst[out_len] = 0then writes one element past the end. There are no callers of the fixed-buffer forms in the tree today, but these are exported. The UTF-8 variants are safe:utf8_to_wcharrefuses output that doesn't fit.Commits
test/cmocka/test_platform.c:test_mbs_unconvertible_is_empty:os_wcs_to_mbs(with and without a buffer),os_wcs_to_mbs_ptrandos_utf8_to_mbs_ptr;C.UTF-8,en_US.UTF-8or.UTF8, skipped with a message if none exists) throughos_mbs_to_wcs,os_mbs_to_wcs_ptrandos_mbs_to_utf8_ptr.All must give 0 and an empty string.
test_mbs_buffer_too_small_is_cut: a guard element after a 4-element buffer stays untouched, and the text is cut to 3 elements.Each test saves and restores
LC_CTYPE.(size_t)-1gives 0 and an empty string, and the conversion intodstis limited todst_size - 1elements. Text that fits converts as before. The oracle's copy ofos_mbs_to_wcs(oracle/platform_conv_host.c) gets the same change, so it stays identical toplatform.c(bug: Oracle stand-ins in test_stubs.c can diverge from libobs #145).Testing
The two new tests were built against the real
platform.c(MSVC on Windows, gcc 15 on Ubuntu 26.04):-1 != 0and4 != 3);Also run:
cargo test --workspaceon Windows and Linux, and clippy on Linux. CI runs the fulltest_platform.🤖 Generated with Claude Code