Skip to content

text-freetype2: Decode UTF-16 text files into wide strings - #121

Merged
zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:fix/freetype-utf16
Oct 11, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:fix/freetype-utf16

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

Fixes #95.

Both text file readers of the FreeType 2 text source (load_text_from_file, read_from_end) copied the UTF-16 units of a file with a BOM straight into the wchar_t * text:

  • 4-byte wchar_t (Linux, macOS): pairs of units became one garbage character, and the text had no 4-byte terminator, so remove_cr and every later wcslen read past the buffer, and remove_cr wrote past it (the issue's report).
  • Every platform:
    • read_from_end allocated no room for a terminator;
    • an odd file size over-read by a byte;
    • with an odd size, cur_pos -= 2 on a uint32_t could wrap and loop over ~2³¹ seeks.
  • Found with the test: load_text_from_file never removed \r from UTF-16 files, unlike the UTF-8 path and both paths of read_from_end. When read_from_end reached the start, it also kept the BOM as text.

Commits

  1. Move, no behavior change. remove_cr and the two readers' bodies move to text-file.c as ft2_read_text_file / ft2_read_text_file_end. They return the text (NULL if the file can't be opened) instead of writing ft2_source. load_text_from_file / read_from_end keep the logging, file_load_failed and the swap of srcdata->text. This lets a test build the readers without FreeType.
  2. test/cmocka/test_text_file.c. Expectations are UTF-8 strings converted with os_utf8_to_wcs_ptr, so they hold for both wchar_t sizes:
    • UTF-8 and UTF-16 files, \r removed, U+00E9 and a surrogate pair (U+1F600), empty and BOM-only files, missing file
    • a stray last byte of a UTF-16 file is dropped (whole file and from the end)
    • characterization of the "last lines" count on UTF-8
    • for 5 texts × log_lines 0..6, a UTF-16 file gives the same text as the same UTF-8 file
  3. Fix. UTF-16 is decoded unit by unit into a terminated wchar_t string. Where WCHAR_MAX > 0xFFFF, surrogate pairs are joined and unpaired surrogates become U+FFFD. A stray last byte is dropped, the backwards scan stops at the BOM, and whole UTF-16 files get remove_cr too.

Testing

Run locally on Windows (MSVC, a minimal cmocka stand-in, and stubs for bmalloc/os_fopen plus the UTF-8 converters around the real util/utf8.c):

  • at the test commit, 3 of 5 tests fail: \r kept (8 != 7), odd size over-read (3 != 1), and from-the-end text with the BOM plus garbage (71 != 1);
  • with the fix, 5/5 pass.

The 4-byte wchar_t branch only runs on the Linux/macOS CI.

🤖 Generated with Claude Code

@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto master (old head 71a05a0, replaced with --force-with-lease); the test/cmocka/CMakeLists.txt conflict is resolved with both blocks kept. RED→GREEN in Tier 2: at the test commit, test_text_file fails test_utf16_file and test_utf16_odd_size; with the fix, 46/46 pass with the Rust port OFF and ON. The local gate passes and is stamped. This is the claimed fix for #95; #123 duplicates it. Mark it ready when you're happy and I'll merge once CI is green.

b7216309-jpg and others added 3 commits October 11, 2026 09:57
Moves remove_cr and the bodies of load_text_from_file and
read_from_end to text-file.c as ft2_read_text_file and
ft2_read_text_file_end, which return the text (NULL when the file
cannot be opened) instead of storing it in ft2_source. The two
functions in text-functionality.c keep the logging, the
file_load_failed flag and the swap of srcdata->text.

No behavior change; this lets a test build the readers without
FreeType.

Refs obs-rust#95

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reads UTF-8 and UTF-16 (BOM) files with ft2_read_text_file and
ft2_read_text_file_end and compares with the UTF-8 text through
os_utf8_to_wcs_ptr, so the expectations hold for 2- and 4-byte wchar_t:

- carriage returns removed, a surrogate pair is one character
- a stray last byte of a UTF-16 file is dropped
- the last lines of a UTF-16 file are those of the same UTF-8 file,
  without the BOM

The UTF-16 cases fail until the next commit: the units are copied raw
into wchar_t without a terminator, an odd size over-reads, the reader
of whole UTF-16 files keeps \r, and reading from the end can include
the BOM.

Refs obs-rust#95

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both file readers copied the UTF-16 units of a file with a BOM straight
into the wchar_t text:

- where wchar_t is 4 bytes (Linux, macOS), pairs of units became one
  garbage character, and the text had no 4-byte terminator, so
  remove_cr and every later wcslen ran past the buffer and remove_cr
  wrote past it
- ft2_read_text_file_end allocated no room for a terminator on any
  platform, and an odd file size over-read on Windows too
- with an odd size, the backwards scan (cur_pos -= 2 on a uint32_t)
  could step below 0 and wrap, looping over ~2^31 seeks

The text is now decoded unit by unit into a terminated wchar_t string;
where wchar_t is UTF-32, surrogate pairs are joined and unpaired
surrogates become U+FFFD. A stray last byte is dropped, and the scan
from the end stops at the BOM, which is no longer part of the text.
Whole UTF-16 files now have carriage returns removed like every other
case.

Fixes obs-rust#95

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=baf6344f2022afeba3e26ae8bd217e5de78f685a secs=365 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791671646,"gate":"rust/all/fmt","host":"linux-x86_64","key":"8cd6ee2baa7c11f5ea5f4c06b58caca818a5cd883ff975fdf62868855f8bce59","lane":"lint","parents":["a0fb5573845a038c6bfa1e3367c8eb22aa1abeaf"],"secs":172,"stamp":"d31d83fbc9862159d6e1d6cace73bf36","tree":"baf6344f2022afeba3e26ae8bd217e5de78f685a","v":1,"via":"run"}
Ci-Attestation: {"at":1791671646,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"8cd6ee2baa7c11f5ea5f4c06b58caca818a5cd883ff975fdf62868855f8bce59","lane":"lint","parents":["a0fb5573845a038c6bfa1e3367c8eb22aa1abeaf"],"secs":172,"stamp":"2cba1a3c7f6a5cffc69eb655dd14b4d6","tree":"baf6344f2022afeba3e26ae8bd217e5de78f685a","v":1,"via":"run"}
Ci-Attestation: {"at":1791671646,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"06091c0dd7710d795557ee417d411a9949649a045b418ecf18441c9b49e293fd","lane":"rust-test","parents":["a0fb5573845a038c6bfa1e3367c8eb22aa1abeaf"],"secs":141,"stamp":"141ab2fc2a703e334783eb057e77821c","tree":"baf6344f2022afeba3e26ae8bd217e5de78f685a","v":1,"via":"run"}
Ci-Attestation: {"at":1791671646,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"77f72abd975a117377f43e6ec9115982f7a84a6453fe420ebcbebbfae1cc3c7b","lane":"tier2-linux","parents":["a0fb5573845a038c6bfa1e3367c8eb22aa1abeaf"],"secs":50,"stamp":"24250aba9b863e823ecd918260056932","tree":"baf6344f2022afeba3e26ae8bd217e5de78f685a","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

please switch this merge request from draft something more final

@b7216309-jpg
b7216309-jpg marked this pull request as ready for review October 11, 2026 19:44
@b7216309-jpg
b7216309-jpg requested a review from zackees as a code owner October 11, 2026 19:44
@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

Marked ready, thanks for the rebase and the Tier 2 run.

It conflicts again since #168: only the test target moves, to test/cmocka/tests.d/test_text_file.cmake. I have that rebase locally (lint passes), but my token can't push it yet: master now changes .github/workflows/, and pushing those commits to the fork needs the workflow scope. I'll push it as soon as that's sorted. If you get to it first, the conflict is only that block.

@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

Pushed the rebase onto current master (8182022, lease on your 57e38f5). The only change is that the test target moved to test/cmocka/tests.d/test_text_file.cmake, in the test commit. The local lint lane passes. It's ready and mergeable once CI is green.

@zackees
zackees merged commit a7cc3ad into obs-rust:master Oct 11, 2026
23 checks passed
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally after rebasing onto current master. It's ready, but I can't push the rebase to your branch ("Allow edits by maintainers" is off).

Your base predates #220, so CI configure fails on it with VERSION "obs-studio-rust-v0.1.0" format invalid. That's not your change. On current master:

  • Refactor (Move the text file readers): Tier 2 passed 85 of 85.
  • RED (Add text-freetype2 text file test): Tier 2 passed 85 of 86. test_utf16_file, test_utf16_odd_size and test_utf16_end_matches_utf8 failed.
  • GREEN: Tier 2 passed 86 of 86 with ENABLE_RUST_LIBOBS OFF and 86 of 86 with it ON. The local gate passed.

The rebase applies cleanly. Please either run git fetch upstream && git rebase upstream/master && git push --force-with-lease, or turn on "Allow edits by maintainers" and I'll push it. Merging once CI is green on the rebased head.

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.

[crash] text-freetype2: UTF-16 file stored as wchar_t* without wide NUL → OOB read+write (Linux/macOS)

2 participants