Repository navigation
Description
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:
towupperandtowlower(wstrcmpi,wstrcmpi_n,wstrstri, the case maps),mbstowcs(throughos_mbs_to_utf8_ptr) andvsnprintf. Their results depend on the libc and the currentLC_CTYPElocale. In glibc's "C" localetowupper(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 dstris public, and its layout is the contract (existing layout test). Buffers come from thebmallocfamily, so C can free or resize them.wchar_tis 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_dstrcovers onlydstr_to_upper/dstr_to_loweron ASCII (test_dstr_case, non-Windows) and fivedstr_printf/dstr_catfcalls. The characterization adds cases totest_dstr, with no new test executable. The symbols areEXPORT, sotest_dstrcalls them through libobs and sees the swap.
Behaviour of the C today, characterized and kept (not endorsed):
dstr_from_mbsmoves the converted buffer in and leavescapacity0.dstr_to_mbsconverts multibyte to UTF-8, not the other way round, and stops at the first NUL.dstr_to_upperanddstr_to_lowertruncate at an embedded NUL. They free a string that is not valid UTF-8, becausedstr_to_wcsgivesL""anddstr_from_wcs(L"")frees.wcsdepadmoves 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. Whenvsnprintffails (on glibc,%lsof a non-ASCII character in the "C" locale), it assumes 4095 bytes, sizes the buffer to 4096 and takes thestrlenof what glibc wrote.
Plan
One PR, as #1846 asks, with separate commits:
- 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-8LC_CTYPE(skipped where none exists), and on glibc the "C" locale and thevsnprintffailure path. They pass againstOBS_LIBOBS_BACKEND=C,libobs-c-referenceand the hybrid. - RED: Tier 1 tests against a stub safe core (
todo!()), a module of the existing obs-util harness. - GREEN: the safe core
obs_util::dstr_libc, generic over thewchar_tunit, and#[no_mangle]shims inffi/dstr_libc.rswith the same 11 symbols and C signatures. The include moves intolibobs.ported-util, and thelibobs.util-libcgroup, 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.pychecks that the production build excludes the C on Linux, Windows and macOS. - 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. - Docs (
rust/README.md,release-criteria.md) andport_comparisonpairs:dstr/to_upper,dstr/from_wcs,dstr/wstrcmpi,dstr/wstrstriat 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_variadicis unstable) or take ava_list. The four functions are copied unchanged intoutil/dstr-variadic.c.h, which only the hybrid compiles (grouplibobs.rust-variadic).base-variadic.c.h,pipe-variadic.c.handprofiler-variadic.c.hdo 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. Sodstr-libc.c.his 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
towupperandtowlowerinto the safe core. They do not use Rust'schar::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_ptrandos_utf8_to_wcs_ptrstay C until unit 1 ports them. Undercargo test,oracle/platform_host.cnow 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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
rdoupe commentedon Oct 23, 2026
Claiming: #1858 (unit 2 of #1846). Characterization, port and parity are ready; PR follows.
rdoupe commentedon Oct 23, 2026
Duplicate of #1855 (claimed first); the port continues in #1857.