[layout] Collect intersected ClassDef classes once per ContextFormat2 visit

The class-based (Chain)Context closure and depend paths called
ClassDef::intersects_class() for every rule-set index / class, and each
call walks the whole ClassDef, so a subtable with many classes sharing
one large ClassDef was quadratic per closure visit (and thus in
subsetting).

Collect ClassDef::intersected_classes() of the glyph set into a set
once per visit and answer the per-class query from that set.  All eight
call sites across ContextFormat2 and ChainContextFormat2 (closure and
depend) use the precomputed set; results are identical.  Adds a fuzzer
regression font.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ZCMLEQFJ.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-11-10 19:15:42 -06:00
1 parent 0cf5addc3e
commit 8b0e9da51f
2 files changed
+48 -40

No files matched your search

+48 -40
View File
@@ -1656,19 +1656,11 @@ static inline bool intersects_glyph (const hb_set_t *glyphs, unsigned value, con
{
return glyphs->has (value);
}
static inline bool intersects_class (const hb_set_t *glyphs, unsigned value, const void *data, void *cache)
static inline bool intersects_class (const hb_set_t *glyphs HB_UNUSED, unsigned value, const void *data HB_UNUSED, void *cache)
{
const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
hb_map_t *map = (hb_map_t *) cache;
hb_codepoint_t *cached_v;
if (map->has (value, &cached_v))
return *cached_v;
bool v = class_def.intersects_class (glyphs, value);
map->set (value, v);
return v;
/* ClassDef::intersects_class() walks the whole ClassDef, so callers collect
* ClassDef::intersected_classes() of glyphs into the cache upfront, once. */
return reinterpret_cast<const hb_set_t *> (cache)->has (value);
}
static inline bool intersects_coverage (const hb_set_t *glyphs, unsigned value, const void *data, void *cache HB_UNUSED)
{
@@ -3737,7 +3729,8 @@ struct ContextFormat2_5
const ClassDef &class_def = this+classDef;
hb_map_t cache;
hb_set_t cache;
class_def.intersected_classes (glyphs, &cache);
struct ContextClosureLookupContext lookup_context = {
{intersects_class, nullptr},
ContextFormat::ClassBasedContext,
@@ -3757,8 +3750,7 @@ struct ContextFormat2_5
| hb_map (hb_add (this))
| hb_enumerate
| hb_map ([&] (const hb_pair_t<unsigned, const RuleSet &> p)
{ return class_def.intersects_class (glyphs, p.first) &&
coverage_glyph_classes.has (p.first) &&
{ return coverage_glyph_classes.has (p.first) &&
p.second.intersects (glyphs, lookup_context); })
| hb_any
;
@@ -3778,7 +3770,8 @@ struct ContextFormat2_5
const ClassDef &class_def = this+classDef;
hb_map_t cache;
hb_set_t cache;
class_def.intersected_classes (c->glyphs, &cache);
intersected_class_cache_t intersected_cache;
ContextDependLookupContext lookup_context = {
{intersects_class, intersected_class_glyphs},
@@ -3788,10 +3781,11 @@ struct ContextFormat2_5
&intersected_cache
};
hb_set_t active_classes;
class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes);
+ hb_enumerate (ruleSet)
| hb_filter ([&] (unsigned _)
{ return class_def.intersects_class (&c->parent_active_glyphs (), _); },
hb_first)
| hb_filter (active_classes, hb_first)
| hb_apply ([&] (const hb_pair_t<unsigned, const typename Types::template OffsetTo<RuleSet>&> _)
{
const RuleSet& rule_set = this+_.second;
@@ -3814,7 +3808,8 @@ struct ContextFormat2_5
const ClassDef &class_def = this+classDef;
hb_map_t cache;
hb_set_t cache;
class_def.intersected_classes (c->glyphs, &cache);
intersected_class_cache_t intersected_cache;
struct ContextClosureLookupContext lookup_context = {
{intersects_class, intersected_class_glyphs},
@@ -3824,10 +3819,11 @@ struct ContextFormat2_5
&intersected_cache
};
hb_set_t active_classes;
class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes);
+ hb_enumerate (ruleSet)
| hb_filter ([&] (unsigned _)
{ return class_def.intersects_class (&c->parent_active_glyphs (), _); },
hb_first)
| hb_filter (active_classes, hb_first)
| hb_apply ([&] (const hb_pair_t<unsigned, const typename Types::template OffsetTo<RuleSet>&> _)
{
const RuleSet& rule_set = this+_.second;
@@ -3845,7 +3841,8 @@ struct ContextFormat2_5
const ClassDef &class_def = this+classDef;
hb_map_t cache;
hb_set_t cache;
class_def.intersected_classes (c->glyphs, &cache);
struct ContextClosureLookupContext lookup_context = {
{intersects_class, nullptr},
ContextFormat::ClassBasedContext,
@@ -3856,8 +3853,7 @@ struct ContextFormat2_5
+ hb_iter (ruleSet)
| hb_map (hb_add (this))
| hb_enumerate
| hb_filter ([&] (const hb_pair_t<unsigned, const RuleSet &> p)
{ return class_def.intersects_class (c->glyphs, p.first); })
| hb_filter (cache, hb_first)
| hb_map (hb_second)
| hb_apply ([&] (const RuleSet & _)
{ _.closure_lookups (c, lookup_context); });
@@ -5396,7 +5392,10 @@ struct ChainContextFormat2_5
const ClassDef &input_class_def = this+inputClassDef;
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
hb_map_t caches[3] = {};
hb_set_t caches[3] = {};
backtrack_class_def.intersected_classes (glyphs, &caches[0]);
input_class_def.intersected_classes (glyphs, &caches[1]);
lookahead_class_def.intersected_classes (glyphs, &caches[2]);
struct ChainContextClosureLookupContext lookup_context = {
{intersects_class, nullptr},
ContextFormat::ClassBasedContext,
@@ -5417,8 +5416,7 @@ struct ChainContextFormat2_5
| hb_map (hb_add (this))
| hb_enumerate
| hb_map ([&] (const hb_pair_t<unsigned, const ChainRuleSet &> p)
{ return input_class_def.intersects_class (glyphs, p.first) &&
coverage_glyph_classes.has (p.first) &&
{ return coverage_glyph_classes.has (p.first) &&
p.second.intersects (glyphs, lookup_context); })
| hb_any
;
@@ -5440,7 +5438,10 @@ struct ChainContextFormat2_5
const ClassDef &input_class_def = this+inputClassDef;
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
hb_map_t caches[3] = {};
hb_set_t caches[3] = {};
backtrack_class_def.intersected_classes (c->glyphs, &caches[0]);
input_class_def.intersected_classes (c->glyphs, &caches[1]);
lookahead_class_def.intersected_classes (c->glyphs, &caches[2]);
intersected_class_cache_t intersected_cache;
ChainContextDependLookupContext lookup_context = {
{intersects_class, intersected_class_glyphs},
@@ -5452,10 +5453,11 @@ struct ChainContextFormat2_5
&intersected_cache
};
hb_set_t active_classes;
input_class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes);
+ hb_enumerate (ruleSet)
| hb_filter ([&] (unsigned _)
{ return input_class_def.intersects_class (&c->parent_active_glyphs (), _); },
hb_first)
| hb_filter (active_classes, hb_first)
| hb_apply ([&] (const hb_pair_t<unsigned, const typename Types::template OffsetTo<ChainRuleSet>&> _)
{
const ChainRuleSet& chainrule_set = this+_.second;
@@ -5480,7 +5482,10 @@ struct ChainContextFormat2_5
const ClassDef &input_class_def = this+inputClassDef;
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
hb_map_t caches[3] = {};
hb_set_t caches[3] = {};
backtrack_class_def.intersected_classes (c->glyphs, &caches[0]);
input_class_def.intersected_classes (c->glyphs, &caches[1]);
lookahead_class_def.intersected_classes (c->glyphs, &caches[2]);
intersected_class_cache_t intersected_cache;
struct ChainContextClosureLookupContext lookup_context = {
{intersects_class, intersected_class_glyphs},
@@ -5492,10 +5497,11 @@ struct ChainContextFormat2_5
&intersected_cache
};
hb_set_t active_classes;
input_class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes);
+ hb_enumerate (ruleSet)
| hb_filter ([&] (unsigned _)
{ return input_class_def.intersects_class (&c->parent_active_glyphs (), _); },
hb_first)
| hb_filter (active_classes, hb_first)
| hb_apply ([&] (const hb_pair_t<unsigned, const typename Types::template OffsetTo<ChainRuleSet>&> _)
{
const ChainRuleSet& chainrule_set = this+_.second;
@@ -5515,7 +5521,10 @@ struct ChainContextFormat2_5
const ClassDef &input_class_def = this+inputClassDef;
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
hb_map_t caches[3] = {};
hb_set_t caches[3] = {};
backtrack_class_def.intersected_classes (c->glyphs, &caches[0]);
input_class_def.intersected_classes (c->glyphs, &caches[1]);
lookahead_class_def.intersected_classes (c->glyphs, &caches[2]);
struct ChainContextClosureLookupContext lookup_context = {
{intersects_class, nullptr},
ContextFormat::ClassBasedContext,
@@ -5528,8 +5537,7 @@ struct ChainContextFormat2_5
+ hb_iter (ruleSet)
| hb_map (hb_add (this))
| hb_enumerate
| hb_filter([&] (unsigned klass)
{ return input_class_def.intersects_class (c->glyphs, klass); }, hb_first)
| hb_filter (caches[1], hb_first)
| hb_map (hb_second)
| hb_apply ([&] (const ChainRuleSet &_)
{ _.closure_lookups (c, lookup_context); })