Skip to content

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

Closed
chriscoveries/obsrust-fork
#8

Description

@chriscoveries

Class: OOB read + OOB write, reachable from a user text file (source from_file/text_file settings or scene-collection JSON).

load_text_from_file (plugins/text-freetype2/text-functionality.c:391-403): when the file starts with 0xFEFF it does

srcdata->text = bzalloc(filesize);
bytes_read = fread(srcdata->text, filesize - 2, 1, tmp_file);

— a raw UTF-16 (2-byte-unit) buffer into srcdata->text, which is wchar_t *. On Linux/macOS wchar_t is 4 bytes and the buffer ends with only two zero bytes — no wide-NUL terminator.

remove_cr (:359-367) then runs for (i; source[i] != L'\0'; ++i) reading 4-byte wchar_ts and source[j] = L'\0' writes 4 bytes at the scan length — both range past the allocation whenever the file lacks a 4-aligned U+0000. wcslen calls in cache_glyphs (:283), set_up_vertex_buffer (:63) and fill_vertex_buffer (:143) do the same OOB scan. read_from_end stores UTF-16 the same way (:475-481).

Bonus defect in read_from_end (:452-471): cur_pos is uint32_t; for a UTF-16 file whose size leaves cur_pos odd (e.g. 2-byte BOM + 1 byte), cur_pos -= 2 underflows to 0xFFFFFFFF and the loop spins through ~2³¹ fseek/fread calls → hang.

Fix sketch: decode UTF-16 to UTF-8 and go through os_utf8_to_wcs_ptr (or store UTF-32); clamp the backwards scan to cur_pos >= 2.

— RD1

Activity

  1. b7216309-jpg commented on Oct 10, 2026

    @b7216309-jpg
    Contributor

    Working on this: decoding UTF-16 files into wchar_t properly on 4-byte-wchar_t platforms, bounding the backwards scan in read_from_end, plus a cmocka test. PR to follow.

  2. added a commit that references this issue on Oct 10, 2026
    8db8f86
  3. added a commit that references this issue on Oct 31, 2026
    aabba78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions