Skip to content

libobs: Move the caption text wrap into obs-output-caption-text.c - #1618

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

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/caption-text-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Refs #1617 (PR 1 of the plan: the extraction and the characterization; the port follows)

What and why

Behaviour-neutral extraction before the Rust port, as the testing policy asks for part of a large file: the caption text wrap and pagination move verbatim from libobs/obs-output.c.h into libobs/obs-output-caption-text.c.h. The move comes with new characterization cases in test_output_caption_text. No Rust, no signature or behaviour change, and no header change.

  • What moves: caption_text_page() and the functions only it uses: caption_utf8_continuation(), caption_utf8_char_length(), caption_is_newline(), caption_utf8_whitespace() and caption_text_row(). All of it stays static. The moved lines are byte-for-byte the same: obs-output.c.h loses 141 lines, and the new file is those lines (less the blank line between them and the next function) plus the license header, the include and a comment.
  • Not moved: caption_text_new() and obs_output_output_caption_text1/2(). They allocate and queue the pages (including the rule that only the first page may be empty), and only the page text is part of the unit. add_caption() is the consumer and stays too.
  • Build: the new file joins the existing libobs.core-output group just before obs-output.c.h (alphabetical), so it stays in the same translation unit as its caller and keeps internal linkage.
    • It calls nothing in obs-output.c.h, so it needs no forward declarations. It uses only memcpy and the CAPTION_* bounds from obs-internal.h.
    • Its precompiled header is libobs.pch.h, like every member of that group. No group, PCH, source-sets.json or CMake source list changes. obs-build builds the same group file, so the Rust lane needs no change either.
  • Exported symbols: unchanged. Everything moved is static.

test_output_caption_text goes through real libobs (the public obs_output_output_caption_text2(), then the caption queue through obs-internal.h), so in mode ON it will see the Rust swap directly. Its 12 existing cases are unchanged. It gets 20 new ones, and two harness changes that do not touch any case:

  • the harness keeps a list of live harnesses, and main() destroys any still on it before obs_shutdown(). Before this, a failed assertion left its case before harness_destroy(), and the output it had marked active made obs_shutdown() block, so any failure, old cases included, showed up as a hang instead of a failure;
  • assert_page_invariants() collects the queued pages under caption_mutex and checks them after unlocking, for the same reason.

The 20 new cases:

  • invalid UTF-8, each form dropped one byte at a time, the bytes after a bad lead judged on their own, and valid characters on either side kept: a lone continuation byte, C0/C1 overlong leads, E0 80-9F, ED A0-BF (surrogates), F0 80-8F, F4 90 and above, F5-FF, sequences truncated by the end of the string (2-, 3- and 4-byte) and by an ASCII byte. The valid edges of each range are kept (U+0080, U+0800, U+D7FF, U+10000, U+10FFFF). An invalid byte takes no column: 32 4-byte characters, each followed by a lone continuation byte, still make one 128-byte row.
  • empty pages: "", only spaces (1, 8 and 100) and only invalid bytes each queue the single empty page.
  • newlines: CR, LF and CRLF each end a row; blank lines and leading and trailing newlines give no rows.
  • whitespace: NBSP is a split point and is skipped as leading whitespace, but inside a row it is kept as its two bytes; an NBSP as character 33 ends a full row. DEL and the C0 controls (0x01, 0x1F, VT, FF) become spaces inside a row and are skipped when leading. Trailing whitespace before a newline or the end stays in the row, and a trailing tab becomes a space.
  • wrap: a word of exactly 32 characters fills one row, and the next word starts the next row; a word of 33 is hard-wrapped after character 32, and after a short word it moves to its own row first; a space as character 32 is dropped by the wrap; whitespace as characters 32 and 33; a 2-byte character as character 32 stays, and one as character 33 starts the next row whole.
  • bounds: 32 4-byte characters make the largest row (CAPTION_LINE_BYTES), and 15 of those rows make a page of exactly CAPTION_PAGE_BYTES; one more character starts a second page. 16 short lines give 15 rows and a second page. Whitespace, NBSP and invalid bytes after a full page queue no empty second page.
  • any input ends: every byte value 1-255 in one string, a 4096-byte run of each lead byte 0x80-0xFF alternating with a continuation byte, and 64 pseudo-random strings (up to 3844 bytes) weighted toward whitespace, newlines, multibyte leads and continuation bytes. Each call returns, so the cursor always moves forward. Every queued page is checked: at most CAPTION_PAGE_BYTES, 1-15 rows, each row 1-128 bytes and at most 32 characters, starting with a non-whitespace character, no CR inside, well-formed UTF-8, and only a sole page may be empty.

Characterized, not endorsed (comments in the test):

  • an invalid byte is dropped, not replaced with U+FFFD, so the words on either side of it join;
  • a newline is skipped as leading whitespace, so blank lines give no empty rows;
  • whitespace before a newline or the end of the text stays at the end of the row; only a wrap drops the whitespace it splits at;
  • with whitespace as both characters 32 and 33, the wrap splits at the later one, so the row ends with a space;
  • every C0 control byte and DEL counts as whitespace, so inside a row each one becomes a space.

These are our own behaviours (the wrap that replaced the 128-byte truncation), not regressions. No bug filed.

RED → GREEN

  • RED commit: none. This is a move plus characterization cases, which pass on the moved code. e9c9d410d libobs: Move the caption text wrap into obs-output-caption-text.c.
  • test_output_caption_text: 32/32, run 3 times in each lane against both libraries, all passing. Also 32/32 with the test built with ASan and UBSan (leak detection on), with no reports. The moved functions themselves were also fuzzed standalone under ASan and UBSan (libFuzzer, about 27 million inputs, with an exact-size page buffer and checks for progress and empty pages), with no findings.
  • Mutation check of the new cases: 14 of 14 mutants of obs-output-caption-text.c.h fail the test. Each one was built into libobs and run against the test. The mutants: the E0, ED, F0 and F4 second-byte ranges; F5-F7 accepted as leads; C0/C1 accepted as leads; DEL not whitespace; NBSP not whitespace; CR not a newline; the split recorded after the column check; interior whitespace copied instead of written as a space; NBSP written as a space; 14 rows a page; invalid bytes counted as columns.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON, full builds). Each runs the full cmocka suite against libobs and against libobs-with-rust, headless (private Xvfb, private D-Bus, fresh HOME):
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)

Nothing was skipped or failed to run. These runs are on master at #1615; a final rebase onto #1616 (Rust only, rust/obs-scene) changed no C, and the lint lane passed again on it. The export lists are also identical across the two lanes, with the same count as master. test_output_caption_queue and the caption and ffmpeg output tests pass in all four, unchanged.

  • uv run ci/local_gate.py --lane lint: passed. --lane cross: passed. clang-format 23.1 is clean on the changed files, and ci/test_build_groups.py passes (group order).
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

Windows/macOS: not built there. The moved code has no host conditionals. The new cases are portable C in an existing all-platform test, use only snprintf, memset, strcat, strchr and libobs's bzalloc/brealloc/bfree, and no cmocka range assertions.

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 (this is the extraction with its characterization; the port follows)
  • 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): built with scripting ON (a flag change under cmake/ or build-aux/ runs the full builds) and cross-checked with clang and MSVC (not applicable: no flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not applicable (no port here)

Behavior-neutral extraction before the Rust port: the caption text wrap
and pagination, caption_text_page() and the functions only it uses
(caption_utf8_continuation, caption_utf8_char_length, caption_is_newline,
caption_utf8_whitespace and caption_text_row), moves verbatim from
libobs/obs-output.c.h into libobs/obs-output-caption-text.c.h. Everything
stays static and unchanged. The new file joins libobs.core-output just
before obs-output.c.h (alphabetical), so it is in the same translation
unit as its caller, obs_output_output_caption_text2(). It calls nothing
in obs-output.c.h, so it needs no forward declarations. No signature,
header or exported symbol changes.

caption_text_new() and obs_output_output_caption_text1/2() stay in
obs-output.c.h: they allocate and queue the pages, and only the page
text is part of the unit.

New characterization cases in test_output_caption_text (the existing 12
are unchanged). Through the public API and real libobs, they pin each
invalid UTF-8 form dropped one byte at a time (a lone continuation byte,
C0/C1, E0 80-9F, ED A0-BF, F0 80-8F, F4 90+, F5-FF, sequences truncated
by NUL or by ASCII) and the valid edges of each range; "", only spaces
and only invalid bytes (one empty page); CR, LF, CRLF and blank lines;
NBSP as a split point kept as two bytes; control bytes and DEL; words of
32 and 33 characters; whitespace at columns 32 and 33; a 2-byte
character at the wrap; the 128-byte row and the CAPTION_PAGE_BYTES page;
more than 15 rows; whitespace after a full page; and, over every byte
value and pseudo-random input, that the call ends and every page keeps
the row, page and UTF-8 bounds.

The harness also destroys any harness a failed case left behind before
obs_shutdown(), and the page check asserts after releasing the caption
mutex. Before, an output left marked active made obs_shutdown() block,
so any failed assertion, in the old cases too, hung instead of failing.

Characterized, not endorsed: an invalid byte is dropped, not replaced,
so the words on either side of it join; blank lines give no empty rows;
whitespace before a newline or the end stays in the row; with
whitespace at columns 32 and 33 the row ends with a space; every C0
control byte and DEL becomes a space inside a row.

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