Skip to content

libobs/util: Treat unconvertible text as a failed mbs conversion - #263

Merged
zackees merged 2 commits into
obs-rust:masterfrom
b7216309-jpg:fix/platform-mbs-conversions
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
b7216309-jpg:fix/platform-mbs-conversions

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

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_wcs and os_wcs_to_mbs never check for (size_t)-1, which mbstowcs/wcstombs return for text the current locale can't convert. Probed on both platforms:

Linux "C" Linux C.UTF-8 Windows "C" Windows .UTF8
mbstowcs(NULL, "café" in UTF-8) -1 4 5 (bytes map 1:1) 4
mbstowcs(NULL, "bad \xff byte") -1 -1 10 -1
wcstombs(NULL, L"cé中") -1 6 -1 6

Consequences:

  • Without dst: they return (size_t)-1, so the _ptr functions call bmalloc((SIZE_MAX + 1) * size), that is bmalloc(0), which bcrashes.
    • Linux: os_mbs_to_utf8_ptr is called on X11 window titles in plugins/linux-capture/xcomposite-input.c when only the legacy WM_NAME is set. Qt applies the user's locale, so a Latin-1 title under a UTF-8 locale crashes OBS.
    • Windows: os_wcs_to_mbs_ptr and os_utf8_to_mbs_ptr crash on text the locale can't represent, such as CJK in the "C" locale.
  • With dst: they write the NUL at dst[SIZE_MAX].

Bug 2: off-by-one write with a small buffer

With dst, the conversion is called with dst_size as its limit. mbstowcs/wcstombs fill all of it when the text doesn't fit, and dst[out_len] = 0 then 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_wchar refuses output that doesn't fit.

Commits

  1. Tests, in test/cmocka/test_platform.c:
    • test_mbs_unconvertible_is_empty:

      • U+4E2D in the "C" locale through os_wcs_to_mbs (with and without a buffer), os_wcs_to_mbs_ptr and os_utf8_to_mbs_ptr;
      • invalid UTF-8 under a UTF-8 locale (C.UTF-8, en_US.UTF-8 or .UTF8, skipped with a message if none exists) through os_mbs_to_wcs, os_mbs_to_wcs_ptr and os_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.

  2. Fix: (size_t)-1 gives 0 and an empty string, and the conversion into dst is limited to dst_size - 1 elements. Text that fits converts as before. The oracle's copy of os_mbs_to_wcs (oracle/platform_conv_host.c) gets the same change, so it stays identical to platform.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):

  • on master, both fail on both systems (-1 != 0 and 4 != 3);
  • with the fix, both pass on both systems.

Also run: cargo test --workspace on Windows and Linux, and clippy on Linux. CI runs the full test_platform.

🤖 Generated with Claude Code

b7216309-jpg and others added 2 commits October 11, 2026 22:39
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]>
@b7216309-jpg
b7216309-jpg requested a review from zackees as a code owner October 11, 2026 20:40
@zackees
zackees merged commit 6e9d3ca into obs-rust:master Oct 11, 2026
23 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants