Skip to content

libobs/util: Move the platform string conversions into platform-conversions.c - #1610

Merged
zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/platform-conversions-move
Oct 21, 2026
Merged

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/platform-conversions-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Refs #262 (slice 1, conversions).

What and why

Behavior-neutral extraction before the Rust port, as the testing policy asks for a partial-file 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. Slices 2 to 6 already took everything else out of platform.c.h, so the move is a rename of an unchanged file (100% similarity). There are no static helpers. No Rust, no signature, header or behavior change.

  • Build: the file stays in the libobs.platform unity group and libobs-platform.pch.h, under its new name.
  • Oracle: rust/obs-c-oracle/oracle/platform.c includes it under the new name (the functions were already renamed oracle_* there), so the oracle still runs the real C.
  • Ifdef ratchet: the moved code has no host conditionals.

Earlier slice 1 work is b7216309-jpg's: #263 fixed the mbs conversions' (size_t)-1 and too-small-buffer cases, which this test pins, and #267 ported the Windows branch of util/utf8.c that the UTF-8 conversions call.

New characterization test test_platform_conversions (tests.d/ fragment, plus the Cargo C-test manifest). test_platform has a few round trips and the #263 cases; this test covers every function on its own. wchar_t is 32 bits on Linux and macOS and 16 bits on Windows, so the expected units are chosen with WCHAR_MAX, and the cases where the Windows API (MultiByteToWideChar / WideCharToMultiByte) and libobs's own decoder differ are pinned per side. The group setup sets LC_ALL to "C"; the UTF-8-locale cases switch LC_CTYPE and back, and skip where no UTF-8 locale is installed. Cases:

  • ASCII, and 2-, 3- and 4-byte UTF-8, a surrogate pair where wchar_t is 16 bits, and the U+FFFF / U+10000 edges, both ways;
  • len 0 (up to the NUL) against an explicit len, which also converts an embedded NUL;
  • the NULL-dst size query (which ignores dst_size), dst_size 0 and 1, an exact-size dst and a too-small one, with guards after the buffer;
  • empty and NULL input;
  • invalid UTF-8 (forbidden and lone continuation bytes, truncated sequences, a len that cuts one) and lone surrogates: off Windows they fail the whole string, on Windows they become U+FFFD;
  • a leading UTF-8 BOM: kept as U+FEFF off Windows, dropped on Windows;
  • the _ptr forms: one bmalloc per call, checked with bnum_allocs and freed with bfree, the intermediate wide buffer of os_utf8_to_mbs_ptr and os_mbs_to_utf8_ptr freed, an allocated empty string on failure, *pstr set to NULL for NULL input;
  • the mbs functions in the C locale (ASCII, U+4E2D unconvertible) and a UTF-8 locale (2- and 4-byte characters, invalid input, a cut that never splits a character).

Characterized, not endorsed:

  • the mbs functions ignore len, so os_mbs_to_utf8_ptr does too, while os_utf8_to_mbs_ptr honors it;
  • a dst too small for the UTF-8 conversions gives 0 and an empty string, where the mbs ones cut the text to fit;
  • off Windows, the size query accepts an encoded surrogate (ED A0 80) that the conversion then refuses (1, then 0);
  • off Windows, the decoder accepts overlong 3-byte forms (E0 80 AF is /), values above U+10FFFF and 5- and 6-byte sequences, and the encoder writes values above U+10FFFF in 4 to 6 bytes.

RED → GREEN

  • No RED commit: this is a move plus a characterization test, which passes on the moved code. The move is byte-for-byte (platform.c.h on master = platform-conversions.c.h). The test also passes against the v0.1.0-beta.20261018 release's libobs.so.30, both the C one and the obs-studio-rust/ one.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON). Each runs the cmocka tests against libobs and against libobs-with-rust:
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 350/350 ¹ 350/350 ¹ IDENTICAL (1766)
Rust (ENABLE_RUST_LIBOBS=ON) 350/350 ¹ 350/350 ¹ IDENTICAL (1766)

¹ Headless (private Xvfb, private D-Bus, fresh HOME). The full -j6 run had no X display, so the four tests that need one skipped (test_output_delay_restart, test_output_interleave_e2e, test_output_interleave_restart, test_screencast_portal_teardown) and test_v4l2_busy_signal failed at obs_startup; test_audio_controls (two runs) and test_pulse_monitor_reset (one run) also failed once under -j6. All of them pass when rerun one at a time under Xvfb in all four.

  • test_platform_conversions: 18/18 in all four, three repeated runs in each.
  • soldr cargo test -p obs-util: 742 passed (Tier 1 and the Tier 3 tests over the oracle, which compiles the renamed file).
  • ci/local_gate.py --lane lint: passed (52 s). --lane cross (Windows and macOS clippy/Dylint, the oracle compiled for both): passed (86 s).
  • Real app: not applicable (no behavior change).

Windows/macOS: this test is not behind MSVC or a host check, so it builds and runs on Windows and macOS wherever their Tier 2 lanes run. The Windows expectations (U+FFFD for invalid input, the dropped BOM, a too-small buffer giving 0) follow #267's characterization of the same API calls; this PR could not run them locally.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not applicable (no port here)

…rsions.c

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
@rdoupe
rdoupe requested a review from zackees as a code owner October 21, 2026 08:10
@zackees
zackees enabled auto-merge October 21, 2026 08:11
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit 819a325 Oct 21, 2026
25 checks passed
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