Skip to content

rust: Port util/dstr-libc.c to obs-util #1858

Closed as not planned
Closed as not planned

Description

@rdoupe

Unit 2 of #1846: libobs/util/dstr-libc.c.h (272 lines) into rust/obs-util. It is the last part of dstr that is still C; #43 ported dstr.c. The file holds the dstr helpers that call the C library. All 15 functions are EXPORT in util/dstr.h:

  • wstrcmpi, wstrcmp_n, wstrcmpi_n, wstrstri, wcsdepad: the wide-string compares, search and depad (towupper);
  • dstr_from_mbs, dstr_to_mbs, dstr_to_wcs, dstr_from_wcs: the conversions (os_mbs_to_utf8_ptr, os_utf8_to_wcs_ptr, wchar_to_utf8);
  • dstr_to_upper, dstr_to_lower (towupper, towlower);
  • dstr_printf, dstr_catf, dstr_vprintf, dstr_vcatf (vsnprintf).

The first 11 become Rust. dstr-libc.c.h leaves the production build whole. The four printf functions stay C (see Decisions).

Phase and position

Phase 3 (#43, the remaining leaf utilities). It is unit 2 of #1846 and does not share a file with an open port. dstr.c is already Rust in obs-util::dstr, and so is utf8.c (wchar_to_utf8). The conversions call util/platform-conversions.c.h, which is unit 1 of #1846 and stays C here.

Dependencies and existing tests

  • The C library: towupper and towlower (wstrcmpi, wstrcmpi_n, wstrstri, the case maps), mbstowcs (through os_mbs_to_utf8_ptr) and vsnprintf. Their results depend on the libc and the current LC_CTYPE locale. In glibc's "C" locale towupper(U+00E9) is U+00E9; in "C.UTF-8" it is U+00C9. The port has to call the same functions to keep that behaviour.
  • struct dstr is public, and its layout is the contract (existing layout test). Buffers come from the bmalloc family, so C can free or resize them.
  • wchar_t is a signed 32-bit unit on Linux and macOS and an unsigned 16-bit unit on Windows. The compares order units by that type.
  • test_dstr covers only dstr_to_upper/dstr_to_lower on ASCII (test_dstr_case, non-Windows) and five dstr_printf/dstr_catf calls. The characterization adds cases to test_dstr, with no new test executable. The symbols are EXPORT, so test_dstr calls them through libobs and sees the swap.

Behaviour of the C today, characterized and kept (not endorsed):

  • dstr_from_mbs moves the converted buffer in and leaves capacity 0.
  • dstr_to_mbs converts multibyte to UTF-8, not the other way round, and stops at the first NUL.
  • dstr_to_upper and dstr_to_lower truncate at an embedded NUL. They free a string that is not valid UTF-8, because dstr_to_wcs gives L"" and dstr_from_wcs(L"") frees.
  • wcsdepad moves the leading padding out together with the NUL, then overwrites the trailing padding with NULs one unit at a time. Units past those keep their old values.
  • The printf family frees the dstr when the result's first byte is NUL, even for "%c" of 0. When vsnprintf fails (on glibc, %ls of a non-ASCII character in the "C" locale), it assumes 4095 bytes, sizes the buffer to 4096 and takes the strlen of what glibc wrote.

Plan

One PR, as #1846 asks, with separate commits:

  1. Characterization: cases for every function in test_dstr (no Rust). They cover NULL and empty input, formats and exact capacities, 4095, 4096 and 100000-byte results, embedded NULs, invalid UTF-8 and surrogates, the case maps in a UTF-8 LC_CTYPE (skipped where none exists), and on glibc the "C" locale and the vsnprintf failure path. They pass against OBS_LIBOBS_BACKEND=C, libobs-c-reference and the hybrid.
  2. RED: Tier 1 tests against a stub safe core (todo!()), a module of the existing obs-util harness.
  3. GREEN: the safe core obs_util::dstr_libc, generic over the wchar_t unit, and #[no_mangle] shims in ffi/dstr_libc.rs with the same 11 symbols and C signatures. The include moves into libobs.ported-util, and the libobs.util-libc group, which held only it, goes away. There is an export fragment for Windows (rust-exports.d/obs-util-dstr_libc.txt). The exported-symbol count stays at 1766. ci/test_ported_sources.py checks that the production build excludes the C on Linux, Windows and macOS.
  4. Tier 3: proptests against the C oracle (oracle/dstr_libc.c) on arbitrary wide units and bytes (non-UTF-8, embedded NULs, 0/1/4095-4097 lengths, every starting dstr shape), again in a UTF-8 locale in a re-exec'd child. A check that the printf copy matches the original. A mutation check.
  5. Docs (rust/README.md, release-criteria.md) and port_comparison pairs: dstr/to_upper, dstr/from_wcs, dstr/wstrcmpi, dstr/wstrstri at 128 and 4096.

Tier 2 runs test_dstr unchanged against both libraries. Tier 4 does not apply.

Decisions

  • The printf family stays C. Stable Rust cannot define a C variadic function (c_variadic is unstable) or take a va_list. The four functions are copied unchanged into util/dstr-variadic.c.h, which only the hybrid compiles (group libobs.rust-variadic). base-variadic.c.h, pipe-variadic.c.h and profiler-variadic.c.h do the same. The C reference keeps the original. Tier 3 compiles the copy into the oracle under its own names and checks it against the original. So dstr-libc.c.h is out of the production build, and about 60 lines of it stay C as the copy.
  • Case mapping goes through the C library. The shims pass towupper and towlower into the safe core. They do not use Rust's char::to_uppercase, which ignores the locale and maps U+00DF to "SS". The results stay exactly the C's in every locale.
  • The conversions call what the C calls. os_mbs_to_utf8_ptr and os_utf8_to_wcs_ptr stay C until unit 1 ports them. Under cargo test, oracle/platform_host.c now forwards those two by name on Unix too, as it already does on Windows. Unit 1 will replace those forwarders.

Notes

The odd behaviours above are not changed here. If a fix is wanted, it goes in its own issue, so the port copies the C as it is.

Activity

  1. rdoupe commented on Oct 23, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1858 (unit 2 of #1846). Characterization, port and parity are ready; PR follows.

  2. added
    claimed
    on Oct 23, 2026
  3. rdoupe commented on Oct 23, 2026

    @rdoupe
    CollaboratorAuthor

    Duplicate of #1855 (claimed first); the port continues in #1857.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions