Skip to content

text-freetype2: Decode UTF-16 through UTF-8 and bound tail scan - #123

Closed
chriscoveries wants to merge 1 commit into
obs-rust:masterfrom
chriscoveries:fix/95-utf16-wchar
Closed

chriscoveries wants to merge 1 commit into
obs-rust:masterfrom
chriscoveries:fix/95-utf16-wchar

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

UTF-16 file bytes were fread straight into a wchar_t buffer: on Linux/macOS wchar_t is 4 bytes, so the data never formed a NUL-terminated wide string and remove_cr/wcslen scanned out of bounds. read_from_end also underflowed cur_pos (uint32_t) below 0 on odd file sizes, wrapping the tail scan into an unbounded loop.

  • decode UTF-16LE (incl. surrogate pairs) to UTF-8, then reuse the existing os_utf8_to_wcs path so srcdata->text is a proper wchar_t string everywhere
  • clamp the utf16 tail-scan decrement at 0 instead of wrapping
  • skip the BOM when the tail scan reaches the start of the file

Fixes #95

UTF-16 file bytes were fread straight into a wchar_t buffer: on
Linux/macOS wchar_t is 4 bytes, so the bytes never formed a NUL-
terminated wide string and remove_cr/wcslen scanned out of bounds.
read_from_end could also underflow cur_pos (uint32) below 0 on odd
file sizes, wrapping the tail scan into an unbounded loop.

- decode UTF-16LE (with surrogate pairs) to UTF-8, then reuse the
  existing os_utf8_to_wcs path for a proper wchar_t string
- clamp the utf16 tail-scan decrement at 0 instead of wrapping
- skip the BOM when the scan reaches the start of the file

Fixes obs-rust#95

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Heads-up @chriscoveries: b7216309-jpg claimed #95 on the issue at 21:55 ("Working on this… plus a cmocka test. PR to follow") and opened #121 at 22:00 with a RED→GREEN test. This PR, opened at 22:09, covers the same fix without a test. I'm reviewing and landing #121 as the claimed one. If your bounded tail scan differs in a way #121 misses, please comment on #121. Otherwise, could you close this one? Checking the issue for a claim comment before starting avoids the duplicate.

@chriscoveries

Copy link
Copy Markdown
Contributor Author

Closing ours in favour of #121 by @b7216309-jpg, which is the better fix: it adds tests, handles \r on the UTF-16 path, and maps unpaired surrogates to U+FFFD. Thanks for taking #95, and sorry for the duplicate.

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