mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-19 05:51:05 +01:00
ContextFormat3_6<MediumTypes> (context format 6) stores its input Coverage offsets as Offset24, but depend() passed them to context_depend_lookup() cast to HBUINT16, so every input position after the first decoded the wrong bytes as its offset. intersects_coverage() then followed that value to a Coverage table sanitize never checked. With small offsets the misread value is 0, which points at the subtable header, and the nested lookup's dependencies are silently dropped. A crafted rule can instead point the misread offset at bytes shaped like a large Coverage, and CoverageFormat1_3::intersects() reads past the end of the GSUB blob (ASan heap-buffer-overflow via hb_subset_depend_from_face_or_fail). The other ContextFormat3_6 methods pass coverageZ.arrayZ + 1 as is and let glyph_data_t pick the width. Do the same here. ChainContextFormat3 has the same cast but only has a 16-bit instantiation. Add a format 6 font and test to test-ot-depend.c; it fails without the fix. Tested with meson test under ASan+UBSan with -Dexperimental_api=true. Assisted-by: Claude
82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate a minimal font with a GSUB ContextSubst format 6 lookup.
|
|
|
|
Format 6 is the beyond-64k coverage-based context format; its Coverage
|
|
offsets are 24-bit. Lookup 0 (feature 'liga') matches glyphs 1 2 and
|
|
calls lookup 1 at sequence index 1. Lookup 1 is a SingleSubst that
|
|
maps glyph 2 to glyph 3 and is not referenced by any feature, so the
|
|
2 -> 3 dependency is only found by recursing through the format 6 rule.
|
|
"""
|
|
from pathlib import Path
|
|
import struct
|
|
|
|
OUT = Path(__file__).parent / "fonts" / "context-format6.ttf"
|
|
|
|
|
|
def u16(v):
|
|
return struct.pack(">H", v)
|
|
|
|
|
|
def u24(v):
|
|
return struct.pack(">I", v)[1:]
|
|
|
|
|
|
def u32(v):
|
|
return struct.pack(">I", v)
|
|
|
|
|
|
def coverage(*glyphs):
|
|
return u16(1) + u16(len(glyphs)) + b"".join(u16(g) for g in glyphs)
|
|
|
|
|
|
# ContextFormat6: format, glyphCount, lookupCount, Offset24 coverages,
|
|
# then LookupRecords.
|
|
context_header_len = 6 + 2 * 3 + 4
|
|
cov0 = coverage(1)
|
|
cov1 = coverage(2)
|
|
context = (u16(6) + u16(2) + u16(1)
|
|
+ u24(context_header_len)
|
|
+ u24(context_header_len + len(cov0))
|
|
+ u16(1) + u16(1) # sequenceIndex 1 -> lookup 1
|
|
+ cov0 + cov1)
|
|
|
|
# SingleSubstFormat1: 2 -> 3.
|
|
single = u16(1) + u16(6) + u16(1) + coverage(2)
|
|
|
|
|
|
def lookup(lookup_type, subtable):
|
|
return u16(lookup_type) + u16(0) + u16(1) + u16(8) + subtable
|
|
|
|
|
|
lookups = [lookup(5, context), lookup(1, single)]
|
|
lookup_list = u16(len(lookups))
|
|
offset = 2 + 2 * len(lookups)
|
|
for l in lookups:
|
|
lookup_list += u16(offset)
|
|
offset += len(l)
|
|
lookup_list += b"".join(lookups)
|
|
|
|
feature_list = u16(1) + b"liga" + u16(8) + u16(0) + u16(1) + u16(0)
|
|
lang_sys = u16(0) + u16(0xFFFF) + u16(1) + u16(0)
|
|
script_list = u16(1) + b"DFLT" + u16(8) + u16(4) + u16(0) + lang_sys
|
|
|
|
script_list_offset = 10
|
|
feature_list_offset = script_list_offset + len(script_list)
|
|
lookup_list_offset = feature_list_offset + len(feature_list)
|
|
gsub = (u16(1) + u16(0)
|
|
+ u16(script_list_offset)
|
|
+ u16(feature_list_offset)
|
|
+ u16(lookup_list_offset)
|
|
+ script_list + feature_list + lookup_list)
|
|
|
|
maxp = u32(0x00005000) + u16(4)
|
|
|
|
tables = sorted([(b"GSUB", gsub), (b"maxp", maxp)])
|
|
font = u32(0x00010000) + u16(len(tables)) + u16(32) + u16(1) + u16(0)
|
|
data = b""
|
|
offset = 12 + 16 * len(tables)
|
|
for tag, table in tables:
|
|
font += tag + u32(0) + u32(offset + len(data)) + u32(len(table))
|
|
data += table + b"\0" * (-len(table) % 4)
|
|
OUT.write_bytes(font + data)
|