From cd570b6f544c498d02515ef2d5f3d4b2c31f86ee Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:32:10 -0600 Subject: [PATCH] [aat] Match morx language-tag features against ltag without interning the font's tags Chain::compile_flags() resolved every type-39 (language tag) feature record by interning the font's ltag string with hb_language_from_string(), whose process-wide language list is a linear, never-freed linked list meant for the handful of languages an application passes in. A font controls both the number of records and the number of distinct tags, so shape-plan creation became quadratic (about a minute for a 125 KB font) and the tags stayed in the list for the life of the process. Add ltag::matches_language(i, specific), which compares the tag bytes directly against the buffer language, applying the same canonicalization hb_language_from_string() does (at most 63 bytes, lowercase, '_' to '-', stop at the first other non-alphanumeric) and the same prefix rule as hb_language_matches(); each record now costs at most the length of the buffer's language and nothing is added to the global list. get_language() is kept for the name table, which genuinely needs an hb_language_t. Behaviour is unchanged for every real tag; only a degenerate tag with no valid leading byte, which used to become the empty/invalid language, now matches nothing. Adds a regression font to the in-house aat-morx tests whose type-39 features gate substitutions by tags 'sr' and 'Zh_Hant', exercising case folding, '_' canonicalization, subtag prefix matching and non-matches. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KYZIZMO6. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-morx-table.hh | 2 +- src/hb-aat-ltag-table.hh | 26 ++++++++++++++++++ ...2a555639995ed746c7575416acb689ecb9549d.ttf | Bin 0 -> 924 bytes test/shape/data/in-house/tests/aat-morx.tests | 9 ++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index a85ad6d4d..5279d91d8 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -1164,7 +1164,7 @@ struct Chain #ifndef HB_NO_AAT else if (type == HB_AAT_LAYOUT_FEATURE_TYPE_LANGUAGE_TAG_TYPE && setting && /* TODO: Rudimentary language matching. */ - hb_language_matches (map->face->table.ltag->get_language (setting - 1), map->props.language)) + map->face->table.ltag->matches_language (setting - 1, map->props.language)) { flags &= feature.disableFlags; flags |= feature.enableFlags; diff --git a/src/hb-aat-ltag-table.hh b/src/hb-aat-ltag-table.hh index c974025d4..6dc69b0e8 100644 --- a/src/hb-aat-ltag-table.hh +++ b/src/hb-aat-ltag-table.hh @@ -71,6 +71,32 @@ struct ltag range.length); } + /* Equivalent to hb_language_matches (get_language (i), specific), but + * without interning the font's tag: hb_language_from_string() appends + * every new string to a process-wide, linearly-searched, never-freed list, + * so a hostile font with many tags and many feature records referencing + * them could make that quadratic. */ + bool matches_language (unsigned int i, hb_language_t specific) const + { + const char *s = hb_language_to_string (specific); + if (!s) return false; + + const FTStringRange &range = tagRanges[i]; + const HBUINT8 *tag = (this+range.tag).arrayZ; + /* Canonicalize the same way hb_language_from_string() does: at most 63 + * bytes, lowercase, '_' to '-', stop at the first other non-alphanumeric. */ + unsigned len = hb_min ((unsigned) range.length, 63u); + unsigned l = 0; + for (; l < len; l++) + { + unsigned char c = TOLOWER (tag[l]); + if (c == '_') c = '-'; + if (!ISALNUM (c) && c != '-') break; + if (c != (unsigned char) s[l]) return false; + } + return l && (s[l] == '\0' || s[l] == '-'); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); diff --git a/test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf b/test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf new file mode 100644 index 0000000000000000000000000000000000000000..c3f0d5311b3c4d7ab8882b5300bcf3858630e097 GIT binary patch literal 924 zcmb_aO-oxr6g@NZ5@Q=o5Q#x3yhXuHMF=8YxQNOp8*$fz4YJtHsHgX6eSIRF@#5y*OKYUI-9~Vrz8a2>giG9Zu z&*>3anxB~LOU%&MW0$Ut*fr}F**ZxC-Qf(%DB^$%zYncc<00=y1b83pT->3bC~M1UpMwk_NXfjlJ)pS zRij$TL?$OU(cp^I5MOj1YDgn?F^7S?FN&ic&79oIBpxZ6aoocyE0i})MY+=mYs`>E r*uw(yF-{T}u#Fm8Xrt*+9q+hPOFoJFf6XjC$V9J(3lGoa4gYC(tto5R literal 0 HcmV?d00001 diff --git a/test/shape/data/in-house/tests/aat-morx.tests b/test/shape/data/in-house/tests/aat-morx.tests index 2673c9627..100c646e4 100644 --- a/test/shape/data/in-house/tests/aat-morx.tests +++ b/test/shape/data/in-house/tests/aat-morx.tests @@ -6,3 +6,12 @@ ../fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf;--font-funcs=ot --no-glyph-names --no-positions --no-clusters;U+0061,U+0062;[2|2] ../fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf;--font-funcs=ot --no-glyph-names --no-positions --no-clusters;U+0061,U+0062,U+0063;[2|2|1] ../fonts/3503b8963831c2381809884633f483b7361baa97.ttf;--font-funcs=ot;U+0062,U+0062,U+0062,U+0062;[gid3=0+500|gid3=1+500|gid3=2+500|gid3=3+500] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=sr;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=SR;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=sr-Latn-RS;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=srb;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=s;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=en;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=zh-hant;U+0041;[gid3=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=ZH_HANT;U+0041;[gid3=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=zh;U+0041;[gid1=0+600]