Skip to content

libobs: Preserve extensions and UTF-8 boundaries in generated filenames - #22

Closed
icp4u wants to merge 1 commit into
obs-rust:masterfrom
icp4u:fix/21-filename-truncation
Closed

icp4u wants to merge 1 commit into
obs-rust:masterfrom
icp4u:fix/21-filename-truncation

Conversation

@icp4u

@icp4u icp4u commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Long filename formats could lose their extension and end inside a UTF-8 code
point at the 255-byte limit. Reserve space for the dot and extension before
truncating the base, and back off at most three bytes to preserve code-point
boundaries for valid UTF-8. Longer malformed continuation-byte runs retain
the byte cutoff instead of turning the result into NULL.

Fixes #21.
References: obsproject/obs-studio#11211

Add nine headless C regression tests covering the reported zero-width-space
input, 2/3/4-byte boundaries, extensions, tiny base budgets, and malformed
input. A suffix that cannot fit by itself retains the combined 255-byte cap.
This does not normalize locale encodings or change filesystem/path rules.
Existing C tests, public headers, and exported signatures are unchanged.

Validation: the expanded suite failed six tests against the first patch;
all nine now pass with ASan/UBSan/LSan. ./lint and ./unittest pass. The Linux
gate passed all six CTest executables with Rust OFF and ON and reported
IDENTICAL (1752 symbols). Windows/macOS target lists are updated but those
platforms have not been tested locally.

Prepared with assistance from Codex (GPT-6). Initial patch reviewed by
Claude Opus 5.5, with feedback relayed by the contributor and addressed
in this revision.
Human review is pending.

@zackees

zackees commented Oct 9, 2026

Copy link
Copy Markdown
Member

I am sorry I already fixed it.

@zackees

zackees commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

If you want, I can undo my fix. If you want to get this PR, or let's just go on to the next one. And any issue that you submit, you can do the PR and i'll merge.

Or maybe make you a contributor. IDK let me know.

@icp4u icp4u closed this Oct 9, 2026
@icp4u

icp4u commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

No worries I'm half paying attention

Should I be porting to rust as I go, or fixing in C? just lmk

MCPVOT pushed a commit to MCPVOT/obs-studio that referenced this pull request Oct 30, 2026
…bs-rust#2722)

* test(obs-websocket): Pin the bounded, mutex-guarded session drain (RED)

Stop() polled `_sessions.size()` with no lock while onClose() erased from the
same std::map under `sessionMutex`, and the poll had no bound, so a client that
never answered the close frame kept obs_module_unload from returning:

    uv run --no-project -m unittest ci/test_websocket_submodule.py
    FAILED (failures=3, errors=1)

- `test_pointer_keeps_the_bounded_session_drain`: the pointer is 30d6f511,
  which predates obs-rust/obs-websocket#22.
- `SessionDrainShapeTest`: no `_sessionsDrained.wait_for(`, no
  `kSessionDrainTimeout`, and the unlocked `while (_sessions.size() > 0)` poll
  is still there.

Green is the submodule bump to 3253206.

Refs obs-rust#2713

* fix(obs-websocket): Bump the submodule to the bounded session drain (obs-rust#22)

obs-rust/obs-websocket#22 made WebSocketServer::Stop() wait on a condition
variable whose predicate is read under `sessionMutex`, bounded it at 5 s, and
logs the sessions that did not close instead of spinning forever, so
obs_module_unload can always return.

plugins/obs-websocket: 30d6f511 -> 3253206 (merge of obs-rust#22).

GREEN of the RED in the previous commit:

    uv run --no-project -m unittest ci/test_websocket_submodule.py
    Ran 5 tests ... OK (skipped=1: bounded-parser ancestry is unknowable in a
    shallow checkout)

Refs obs-rust#2713

* docs(features): Record the obs-websocket shutdown drain fix (obs-rust#2713)
rdoupe added a commit that referenced this pull request Nov 1, 2026
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 "#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 "#22" to '#', each left to right and never into what it
  wrote ("#2222" stays "#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 "#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 #3380.
rdoupe added a commit that referenced this pull request Nov 1, 2026
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 "#22" with each pass once ("#2222" is "#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 #3380.
rdoupe added a commit that referenced this pull request Nov 1, 2026
The codec of the saved window string in obs_util::window_match:
encode() and encoded_len() map '#' to "#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 "#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 #3380.
rdoupe added a commit that referenced this pull request Nov 1, 2026
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 "#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 "#22" to '#', each left to right and never into what it
  wrote ("#2222" stays "#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 "#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 #3380.
rdoupe added a commit that referenced this pull request Nov 1, 2026
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 "#22" with each pass once ("#2222" is "#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 #3380.
rdoupe added a commit that referenced this pull request Nov 1, 2026
The codec of the saved window string in obs_util::window_match:
encode() and encoded_len() map '#' to "#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 "#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 #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)

2 participants