mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-30 10:04:24 +01:00
The dependency-graph edge dedup table in hb_depend_data_builder_t::add_edge() was indexed by the low bits of an FNV-style hash whose chain of XORs and odd-constant multiplies only ever carries upwards, so bit j of the result depended only on bits 0..j of each field. A font controls feature tags (free 32-bit values) and substitute glyph IDs, so it can pick values that differ only in their high bits and pile every edge of a lookup onto one slot. The open-addressed probe loop then does about N^2/2 work and each resize_edge_slots() repeats it, so a 15 KB font with 680k colliding edges spent ~10 min of CPU while an ordinary font with the same edges takes 0.1 s. hb_depend_edge_key_t::hash() now runs fasthash32() over all identity fields, so every bit of every field reaches the index; equality and the probe sequence are unchanged, so real fonts produce the same graph at about two probes per edge. The now-unused hb_depend_edge_t::hash() is removed. A mixed hash alone is not enough: it is unkeyed, so colliding edges can still be found offline (each one then costs font bytes instead of coming free from a product of tags and pairs, but the walk stays quadratic in them). Every occupied slot stepped over in add_edge() and resize_edge_slots() is therefore charged to a new per-build counter edge_probes_remaining, seeded from HB_DEPEND_MAX_EDGE_PROBES (1<<26, in hb-limits.hh and overridable like the other HB_*_MAX_* limits, following the HB_RASTER_MAX_DRAW_EDGES precedent); exceeding it fails the build the way an allocation failure does, so hb_subset_depend_from_face_or_fail() returns NULL. At about two probes per edge the cap leaves more than 100x headroom over the largest test font. Honest fonts are already bounded well below it by the HB_DEPEND_MAX_EDGES cap added in the previous commit; the probe budget is what stops a crafted all-colliding chain from doing quadratic work before that cap is reached. A regression test builds a GSUB with 2048 features whose tags differ only in bits 20 and up and checks that all 2048 edges are recorded. test-ot-depend, the api suite and the depend fuzzing/closure-parity tests pass on the ASan experimental build, and the finding's reproducer now builds the hostile font in well under a second. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-4XW1PGV5. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>