From ecf0bc81aa361c65b9a2f98ca058514cc6702f6b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 29 Oct 2026 19:30:56 -0600 Subject: [PATCH] [subset] Keep ClassDef range traversal in sync for class zero (#6473) The class-zero intersection loop skipped ranges starting at glyph 0 without advancing the last glyph, and consulted a separate iterator instead of the range being visited. Probe only actual gaps and always advance past each range, so unlisted glyphs retain class-zero rules. Check all 256 glyph sets over eight glyphs against per-glyph class lookup for empty, contiguous, gapped, and explicit class-zero ranges. The regression fails on the old source. The full Meson suite passes (293 tests), including shape and subset. Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-K9HEP9ZU. Assisted-by: OpenAI Codex --- src/graph/test-classdef-graph.cc | 38 ++++++++++++++++++++++++++++++++ src/hb-ot-layout-common.hh | 21 ++++++++---------- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/graph/test-classdef-graph.cc b/src/graph/test-classdef-graph.cc index a61bbb328..d82ffc3e9 100644 --- a/src/graph/test-classdef-graph.cc +++ b/src/graph/test-classdef-graph.cc @@ -327,9 +327,47 @@ static void test_running_class_size_estimates_with_locally_consecutive_glyphs () hb_always_assert(check_add_class_def_size(estimator, map, 3, {2, 3})); } +static void +test_class_def_intersects_class () +{ + /* Range count, then first/last/class for each range. Include gaps, + * adjacent ranges, explicit class zero, and ranges starting at gid 0. */ + const unsigned cases[][7] = { + {0, 0, 0, 0, 0, 0, 0}, + {1, 0, 3, 1, 0, 0, 0}, + {1, 0, 7, 1, 0, 0, 0}, + {2, 0, 1, 1, 4, 5, 2}, + {2, 0, 1, 1, 2, 3, 2}, + {2, 0, 1, 0, 4, 5, 2}, + {2, 1, 2, 1, 4, 5, 2}, + }; + for (const auto &ranges : cases) + { + OT::HBUINT16 data[8]; + data[0] = 2; + for (unsigned i = 0; i < 7; i++) + data[i + 1] = ranges[i]; + const auto &class_def = *reinterpret_cast (data); + for (unsigned mask = 0; mask < 256; mask++) + { + hb_set_t glyphs; + for (unsigned g = 0; g < 8; g++) + if (mask & (1u << g)) glyphs.add (g); + for (unsigned klass = 0; klass < 4; klass++) + { + bool expected = false; + for (auto g : glyphs) + expected |= class_def.get_class (g) == klass; + hb_always_assert (class_def.intersects_class (&glyphs, klass) == expected); + } + } + } +} + int main (int argc, char **argv) { + test_class_def_intersects_class (); test_class_and_coverage_size_estimates (); test_running_class_and_coverage_size_estimates (); test_running_class_size_estimates_with_locally_consecutive_glyphs (); diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index a7cd03241..c13fab3bd 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -1972,21 +1972,18 @@ struct ClassDefFormat2_4 /* Match if there's any glyph that is not listed! */ hb_codepoint_t g = HB_SET_VALUE_INVALID; hb_codepoint_t last = HB_SET_VALUE_INVALID; - auto it = hb_iter (rangeRecord); - for (auto &range : it) + for (auto &range : rangeRecord) { - if (it->first == last + 1) + /* Only probe for a glyph in the gap before this range; a range + * starting right after the previous one (or at glyph 0) has none. */ + if (range.first != last + 1) { - it++; - continue; + if (!glyphs->next (&g)) + break; + if (g < range.first) + return true; } - - if (!glyphs->next (&g)) - break; - if (g < range.first) - return true; - g = range.last; - last = g; + g = last = range.last; } if (g != HB_SET_VALUE_INVALID && glyphs->next (&g)) return true;