Skip to content

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

Draft
chriscoveries wants to merge 1 commit into
masterfrom
fix/95-utf16-wchar
Draft

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

Conversation

@chriscoveries

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Owner Author

Source review of 8db8f86 against fork-held integration c2d617e; no local build, reproduction or CI claim.

Keep #8 as the single UTF-16 loading/tail-selection concern. It is not redundant: this head decodes UTF-16LE through UTF-8 into wchar_t:370–427, while the integration baseline still reads UTF-16 bytes directly into wchar_t storage:421–433. The tail underflow/BOM corrections are also unique. Refresh only this production-file delta onto the chosen current base, preserving the baseline's glyph-atlas work rather than carrying unrelated history.

One small source gap belongs in that refresh: cur_pos starts at unaligned filesize:505–533. With an odd trailing byte, stepping backwards by two reads misaligned UTF-16 pairs and misses real newlines. Source-derived regression: BOM + “A\nB\nC”, log_lines=1, selects “B\nC”; appending one incomplete byte makes this loop select “A\nB\nC”. Align the scan to complete UTF-16 code units and apply an explicit trailing-incomplete-byte policy.

The fix commit adds no tests. Preserve a RED/GREEN pair with BOM-only input, BMP text, a valid surrogate pair, malformed/truncated surrogates, correct wide-string termination, BOM exclusion and that odd-tail regression. Keep tail selection unchanged when an incomplete final code unit is ignored, and cover supported wchar_t widths. This is a focused refresh/test request, not a retirement candidate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

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)

1 participant