Skip to content

libobs/util: Keep UTF-8 valid when truncating formatted filenames - #23

Merged
zackees merged 1 commit into
masterfrom
fix/utf8-filename-truncation
Oct 9, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/utf8-filename-truncation

Conversation

@zackees

@zackees zackees commented Oct 9, 2026

Copy link
Copy Markdown
Member

Fixes #21 (upstream obsproject/obs-studio#11211).

Problem

os_generate_formatted_filename() truncated its result with dstr_mid(&sf, &sf, 0, 255), which can split a multibyte UTF-8 character at the 255-byte limit. The resulting invalid UTF-8 makes recording abort downstream with incomplete UTF-8 string.

Fix

libobs/util/platform.c: when the cut would land on a UTF-8 continuation byte (10xxxxxx), back up to the start of that character. The 255-byte upper bound is unchanged, and ASCII names, short names and characters that end exactly at byte 255 behave as before. Extension loss on truncation is out of scope, per the issue.

Tests

New test/cmocka/test_formatted_filename.c (ASan + UBSan, non-MSVC):

  • the reported case: 23-byte timestamp prefix + 100 × U+200B, which must give 254 bytes of valid UTF-8
  • every interior cut of 2-, 3- and 4-byte characters at the limit, plus the exact-fit case
  • 300-byte ASCII name is still cut to 255 bytes
  • short UTF-8 name comes out unchanged (café %FPS → café_30.mkv)

The test compiles platform.c and utf8.c (not exported by libobs) into its own binary with an obs_get_video_info fixture, so no obs core is started. This is the same pattern test_avc uses. The target is added to rust/tools/linux-validate/validate.sh.

RED → GREEN: before the fix, 2 of 4 tests fail (255 != 254). After the fix, all pass.

rust/tools/linux-validate/run.sh: 6/6 CTest programs pass with ENABLE_RUST_LIBOBS OFF and ON, and both builds export the same 1752 symbols. ./lint passes, and the local-gate lanes (lint, rust-test, tier2-linux) pass and are stamped on the commit.

Not tested: the macOS tier2 script (the target is not added there) and MSVC (the test is skipped).

🤖 Generated with Claude Code

os_generate_formatted_filename() cut the result at exactly 255 bytes,
which can split a multibyte UTF-8 character and produce invalid UTF-8
(upstream obsproject/obs-studio#11211: recording aborts with
"incomplete UTF-8 string"). Back up to the start of the character
instead; the 255-byte upper bound is unchanged.

Add test_formatted_filename (cmocka, ASan+UBSan) covering the reported
U+200B case, every interior cut of 2-, 3- and 4-byte characters, and
unchanged ASCII/short-name behavior. It compiles platform.c and utf8.c
into the test with an obs_get_video_info fixture so no obs core is
needed. Fails 2/4 before the fix, passes after.

Fixes #21

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=2bb50584f67abadc16e57c468dcd91ec85f3eada secs=57 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791522565,"gate":"rust/all/fmt","host":"linux-x86_64","key":"830a5e833af68cf70bfbdb5967ce346cc77dbfd819a2824776a1ab7c60bea27d","lane":"lint","parents":["f71cae806fd9792ef1120376ec0073c883096343"],"secs":16,"stamp":"068439bc2b0ddaa61ab841591b1e5108","tree":"2bb50584f67abadc16e57c468dcd91ec85f3eada","v":1,"via":"run"}
Ci-Attestation: {"at":1791522565,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"830a5e833af68cf70bfbdb5967ce346cc77dbfd819a2824776a1ab7c60bea27d","lane":"lint","parents":["f71cae806fd9792ef1120376ec0073c883096343"],"secs":16,"stamp":"b3fbfa6e42fae4bc9cd8598c44c81880","tree":"2bb50584f67abadc16e57c468dcd91ec85f3eada","v":1,"via":"run"}
Ci-Attestation: {"at":1791522565,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"a0f0c1673755a2ff7ffec1abd46d4d02cd43f9964b9e1954dd83d0e3931cf895","lane":"rust-test","parents":["f71cae806fd9792ef1120376ec0073c883096343"],"secs":19,"stamp":"d4b6d3593272ea9e0acd6f2d779d3d52","tree":"2bb50584f67abadc16e57c468dcd91ec85f3eada","v":1,"via":"run"}
Ci-Attestation: {"at":1791522565,"gate":"general/linux-x64/tier2","host":"linux-x86_64","key":"bf6844e2bc53ef0b5a9048f8a1feef6baf80548fc198351ed9dbdefda5e8f848","lane":"tier2-linux","parents":["f71cae806fd9792ef1120376ec0073c883096343"],"secs":22,"stamp":"39c79f905e4fb88872151b2fc79b8cb9","tree":"2bb50584f67abadc16e57c468dcd91ec85f3eada","v":1,"via":"run"}
@zackees
zackees merged commit 09e90ad into master Oct 9, 2026
21 of 22 checks passed
icp4u pushed a commit to icp4u/obs-studio that referenced this pull request Oct 9, 2026
Invalid input made only of continuation bytes could back the truncation
added in obs-rust#23 up to length 0, making os_generate_formatted_filename()
return NULL (GenerateSpecifiedFilename then builds a std::string from
it). A UTF-8 character has at most three continuation bytes, so stop at
252; valid input is unaffected. Add a regression test.

Follow-up to obs-rust#21, from review of obs-rust#23.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
MCPVOT pushed a commit to MCPVOT/obs-studio that referenced this pull request Oct 30, 2026
…ix (obs-rust#2720) (obs-rust#2723)

obs-rust#2722 bumped plugins/obs-websocket to 3253206 (obs-rust/obs-websocket#22,
the bounded session drain). The submodule's master has since moved to
777071b, which adds obs-rust#23: onClose() returns when the handle is absent
instead of default-constructing and inserting a null SessionPtr that it
then dereferences.

Point the submodule at 777071b and record the fix in the features ledger.

Refs obs-rust#2720
Refs obs-rust#2713
rdoupe added a commit that referenced this pull request Nov 1, 2026
test_window_match_steps_rust builds the unchanged
test_window_match_steps.c against libobs_rust-static, so the C test
runs the Rust shims of all eight window match functions, the codec
included, not only the C it compiles in. The functions are hidden in
libobs, so the executable takes them from the production staticlib.
obs-util's archive member also needs libuuid, GIO, glib and libobs's
hidden base_default_log_handler() and base_log_literal(): the test
links the first three and compiles its own util/base-variadic.c, as
test_effect_param_types_steps_rust does with its adapters.

Hybrid backend and Linux only. On macOS and Windows obs-util's member
pulls in other host code, and no PR lane builds this test there to
check that link; the Tier 3 parity tests compare the same shims with
the C on every host.

Checked: 20 of 20 pass, and a planted Rust mutant ('#' escaped as
"#23") fails encode, encode_truncates and round_trip.

Refs #3380.
rdoupe added a commit that referenced this pull request Nov 1, 2026
test_window_match_steps_rust builds the unchanged
test_window_match_steps.c against libobs_rust-static, so the C test
runs the Rust shims of all eight window match functions, the codec
included, not only the C it compiles in. The functions are hidden in
libobs, so the executable takes them from the production staticlib.
obs-util's archive member also needs libuuid, GIO, glib and libobs's
hidden base_default_log_handler() and base_log_literal(): the test
links the first three and compiles its own util/base-variadic.c, as
test_effect_param_types_steps_rust does with its adapters.

Hybrid backend and Linux only. On macOS and Windows obs-util's member
pulls in other host code, and no PR lane builds this test there to
check that link; the Tier 3 parity tests compare the same shims with
the C on every host.

Checked: 20 of 20 pass, and a planted Rust mutant ('#' escaped as
"#23") fails encode, encode_truncates and round_trip.

Refs #3380.
alerque pushed a commit to alerque/obs-studio that referenced this pull request Nov 2, 2026
* libobs: Move the window setting's string codec

Behaviour-neutral extraction before the Rust port. Window and game
capture save the chosen window as "title:class:exe", each part escaped.
The codec's core moves from libobs/util/windows/window-helpers.c.h to
libobs/util/windows/window-match.c.h, as three hidden functions declared
without EXPORT in window-match.h:

- ms_window_match_encode(): encode_dstr()'s two dstr_replace() passes,
  '#' to "obs-rust#22", then ':' to "#3A", as one pass over the bytes (the
  second pass never met a ':' the first wrote), snprintf-style: it
  returns the escaped length and writes only when out is not NULL;
- ms_window_match_decode(): decode_str()'s two passes in place, "#3A"
  to ':', then "obs-rust#22" to '#', each left to right and never into what it
  wrote ("obs-rust#2222" stays "obs-rust#22"); it returns the new length;
- ms_window_match_split(): strlist_split(str, ':', true)'s first three
  fields as offsets and lengths, false with fewer than three.

window-helpers.c keeps every allocation: encode_dstr() sizes the
string, bmalloc()s it, fills it and moves it into the dstr only when
the length changed, so an empty dstr keeps its NULL array as
dstr_replace() did. decode_str() copies a field with dstr_ncopy(),
which gives NULL for an empty field as dstr_copy("") did, and decodes
it. ms_build_window_strings() keeps its NULL outputs, its NULL-string
return and its three decodes in order; the strlist allocation goes.
add_window()'s description and join are unchanged.

Checked in a scratch harness (not committed). It extracts
origin/master's encode_dstr(), decode_str(), ms_build_window_strings()
and add_window()'s string building verbatim, and this branch's with the
moved file, over the real C dstr and bmem. It runs 299,614 strings
(NULL, every string up to 6 bytes over "#:23Aax" and 0xC3, and 20
picked ones such as "obs-rust#2222", "##3A22", "x#3a", "a:b:c:d" and high
bytes) through the split, the encode of a dstr, the window list's
description and join, and the split and decode of that join; an empty
dstr and empty join parts too. Each output and each dstr length is
logged. GCC and Clang, -O0 and -O2, with and without ASan and UBSan
without recovery: identical logs of 3,894,989 lines and no leaks.

23 planted mutants; the harness kills 21: each escape or its order
changed or dropped, the NUL left out, a decode pass at the end of the
string missed, a pass resuming inside its match, the passes swapped or
the second dropped, lower-case "#3a" decoded, too few fields accepted,
a field one byte long, the split from the last ':', and in the callers
the empty-dstr shortcut, dstr_ncopy() replaced by a copy that keeps
"", the decode dropped, the fields swapped or one too long, and an
output left unset. The two it cannot see are equivalent: a write of
the last byte, which the NUL then overwrites, and the NULL-string
return, which the split also makes.

Refs obs-rust#3380.

* test: Characterize the window string codec

test_window_match_steps pins the moved codec of the saved window
string before the port: the encode of NULL, plain, '#', ':' and high
bytes, its snprintf-style length and truncation; the decode of each
escape, "#3A" before "obs-rust#22" with each pass once ("obs-rust#2222" is "obs-rust#22",
"#223A" is "#3A", "##3A22" is "#:22"), lower-case "#3a" left alone and
NULL; the split of NULL, "", ":", "a:b", "::", ":::", "a:b:c:d" and
empty fields, with all six outputs 0 when it fails; and the round trip
of add_window()'s join through the split and decode for 169 triples of
titles, classes and exes holding '#', ':' and both.

Refs obs-rust#3380.

* obs-util: Test the window string codec

Tier 1 for the codec of the saved window string, against todo!()
stubs: encode(), encoded_len(), encode_into(), decode() and split() in
obs_util::window_match. The five new cases of test_window_match_steps
are ported 1:1 as steps_encode, steps_encode_truncates, steps_decode,
steps_split and steps_round_trip. New edge cases compare the core with
a model of origin/master's C (encode_dstr() and decode_str() as two
dstr_replace() passes each, the split as strlist_split(str, ':',
true)): every string up to 5 bytes over "#:23Aa", NUL and 0xC3, and a
proptest over strings rich in '#', ':', '2', '3' and 'A', with the
window list's join round-tripped.

The three shims in src/ffi/window_match.rs come with them, hidden
(rust-hidden.d), so the hybrid libobs on Windows links at every
commit; the oracle renames the moved functions to oracle_* so they
link next to the shims.

Refs obs-rust#3380.

* obs-util: Port the window string codec

The codec of the saved window string in obs_util::window_match:
encode() and encoded_len() map '#' to "obs-rust#22" and ':' to "#3A" byte by
byte, which is what encode_dstr()'s two passes do (the second never
meets a ':' the first wrote); encode_into() is the snprintf-style
ms_window_match_encode(); decode() runs the two passes of
ms_window_match_decode() in place, "#3A" then "obs-rust#22", each left to
right with a NUL after its result; split() gives the first three
':'-separated fields, empty ones kept. Each reads its input up to the
first NUL, as the C does, and works on bytes, not UTF-8. Nothing here
can panic or allocate except encode().

The shims, already in place, now reach a real core. Tier 1 passes
(29 of 29).

Refs obs-rust#3380.

* obs-util: Compare the window string codec with C

Tier 3 for the codec: oracle (the moved window-match.c.h), shims and
core over NULL, 19 picked strings, and every string up to 5 bytes over
"#:23Aa", 0xC3 and 0x01 (all up to 3 bytes at every level, a share of
the longer ones at the reduced level). Each encode runs with a NULL
buffer and at every size from 0 to two past the escaped length, and
the whole buffer is compared; each decode compares the whole buffer,
the bytes past the new NUL included; each split compares all six
outputs, prefilled. Proptests run strings rich in '#', ':', '2', '3'
and 'A', and the window list's title:class:exe round trip through the
shims. No intentional difference.

Mutation check: 10 mutants of the core, each killed by these tests
(each escape changed, the decode passes swapped, a match needing a
fourth byte or resuming inside itself, no NUL after a pass, a write
past the room, two fields accepted, a field start off by one, and the
measured length unescaped).

The port_comparison bench gets a window_match/codec/32 pair: each
window's saved string built, split and decoded in stack buffers.

Refs obs-rust#3380.

* docs: Document the window string codec port

The rust/README.md row of window-match.c.h and the window_match module
docs name the codec of the saved window string: what it encodes and
decodes, in which order, what window-helpers.c keeps, and that it
works on bytes. naming.md already maps libobs/util/ to obs-util, and
obs-util's description and lib.rs list no module detail.

Fixes obs-rust#3380.

* test: Run the window match steps against Rust

test_window_match_steps_rust builds the unchanged
test_window_match_steps.c against libobs_rust-static, so the C test
runs the Rust shims of all eight window match functions, the codec
included, not only the C it compiles in. The functions are hidden in
libobs, so the executable takes them from the production staticlib.
obs-util's archive member also needs libuuid, GIO, glib and libobs's
hidden base_default_log_handler() and base_log_literal(): the test
links the first three and compiles its own util/base-variadic.c, as
test_effect_param_types_steps_rust does with its adapters.

Hybrid backend and Linux only. On macOS and Windows obs-util's member
pulls in other host code, and no PR lane builds this test there to
check that link; the Tier 3 parity tests compare the same shims with
the C on every host.

Checked: 20 of 20 pass, and a planted Rust mutant ('#' escaped as
"obs-rust#23") fails encode, encode_truncates and round_trip.

Refs obs-rust#3380.
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.

UTF-8 filename truncation crash (upstream #11211)

1 participant