mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-21 06:51:01 +01:00
[cmap] Bound format 4 collect_unicodes/collect_mapping over overlapping segments
CmapSubtableFormat4::accelerator_t::collect_unicodes() and collect_mapping() trusted the format's invariant that segments are sorted and disjoint, so a subtable whose 32766 segments all cover U+0000..U+FFFE was walked once per segment: ~2.1 billion insertions (about 30 s of CPU) for a single hb_subset() or hb_face_collect_nominal_glyph_mapping() call on a 262 KB font. Both walkers now keep a cursor `next` past the last collected code point and clamp each segment's start to it, skipping segments that end before the cursor. Total work per subtable is therefore at most 65536 code points plus one step per segment, mirroring the ordering guard CmapSubtableLongSegmented::collect_mapping() already has for formats 12/13. The glyphIdArray index is still computed from the raw startCount[i], so range-offset segments map exactly as before, and get_glyph() is untouched. Behaviour changes only for malformed subtables: where sorted segments overlap the earlier segment now wins the shared code points, and a segment lying wholly inside an earlier one is ignored. Adds test_collect_unicodes_format4_overlapping_segments to test/api/test-collect-unicodes.c. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GV7L48CF. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
71d5df6158
commit
acdd1707a3
2 files changed
+80
-2
No files matched your search
+14
-2
@@ -615,10 +615,16 @@ struct CmapSubtableFormat4
|
||||
unsigned int count = this->segCount;
|
||||
if (count && this->startCount[count - 1] == 0xFFFFu)
|
||||
count--; /* Skip sentinel segment. */
|
||||
hb_codepoint_t next = 0;
|
||||
for (unsigned int i = 0; i < count; i++)
|
||||
{
|
||||
hb_codepoint_t start = this->startCount[i];
|
||||
/* Segments must be sorted and disjoint; never walk a codepoint twice,
|
||||
* so that the total work is bounded regardless of segment count. */
|
||||
hb_codepoint_t start = hb_max ((hb_codepoint_t) this->startCount[i], next);
|
||||
hb_codepoint_t end = this->endCount[i];
|
||||
if (unlikely (start > end))
|
||||
continue;
|
||||
next = end + 1;
|
||||
unsigned int rangeOffset = this->idRangeOffset[i];
|
||||
out->add_range(start, end);
|
||||
if (rangeOffset == 0)
|
||||
@@ -656,10 +662,16 @@ struct CmapSubtableFormat4
|
||||
unsigned count = this->segCount;
|
||||
if (count && this->startCount[count - 1] == 0xFFFFu)
|
||||
count--; /* Skip sentinel segment. */
|
||||
hb_codepoint_t next = 0;
|
||||
for (unsigned i = 0; i < count; i++)
|
||||
{
|
||||
hb_codepoint_t start = this->startCount[i];
|
||||
/* Segments must be sorted and disjoint; never walk a codepoint twice,
|
||||
* so that the total work is bounded regardless of segment count. */
|
||||
hb_codepoint_t start = hb_max ((hb_codepoint_t) this->startCount[i], next);
|
||||
hb_codepoint_t end = this->endCount[i];
|
||||
if (unlikely (start > end))
|
||||
continue;
|
||||
next = end + 1;
|
||||
unsigned rangeOffset = this->idRangeOffset[i];
|
||||
if (rangeOffset == 0)
|
||||
{
|
||||
|
||||
@@ -107,6 +107,71 @@ test_collect_unicodes_format4_with_range_offsets_and_deltas (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_collect_unicodes_format4_overlapping_segments (void)
|
||||
{
|
||||
/* Segments overlap and one lies entirely inside an earlier one; each
|
||||
* codepoint must be collected once, from the first segment covering it. */
|
||||
const char cmap_data[60] = {
|
||||
0, 0, 0, 1, /* cmap header */
|
||||
0, 0, 0, 3, 0, 0, 0, 12, /* encoding records */
|
||||
/* cmap 4 */
|
||||
0, 4, /* format */
|
||||
0, 48, /* length */
|
||||
0, 0, /* lang */
|
||||
0, 8, /* segcountx2 */
|
||||
0, 0, /* search range */
|
||||
0, 0, /* entry selector */
|
||||
0, 0, /* range shift */
|
||||
0, 0x45, /* end code [0] */
|
||||
0, 0x47, /* end code [1] */
|
||||
0, 0x47, /* end code [2] */
|
||||
-1, -1, /* end code [3] */
|
||||
0, 0, /* pad */
|
||||
0, 0x41, /* start code [0] */
|
||||
0, 0x43, /* start code [1] */
|
||||
0, 0x40, /* start code [2] */
|
||||
-1, -1, /* start code [3] */
|
||||
0, 0, /* id delta [0] */
|
||||
1, 0, /* id delta [1] */
|
||||
2, 0, /* id delta [2] */
|
||||
0, 1, /* id delta [3] */
|
||||
0, 0, /* id range offset [0] */
|
||||
0, 0, /* id range offset [1] */
|
||||
0, 0, /* id range offset [2] */
|
||||
0, 0, /* id range offset [3] */
|
||||
};
|
||||
|
||||
hb_blob_t* cmap = hb_blob_create(cmap_data, 60, HB_MEMORY_MODE_READONLY, 0, 0);
|
||||
hb_face_t* face = hb_face_builder_create();
|
||||
hb_face_builder_add_table(face, HB_TAG('c', 'm', 'a', 'p'), cmap);
|
||||
hb_blob_destroy(cmap);
|
||||
|
||||
hb_set_t *codepoints = hb_set_create();
|
||||
hb_map_t *mapping = hb_map_create();
|
||||
hb_codepoint_t cp;
|
||||
|
||||
hb_face_collect_unicodes (face, codepoints);
|
||||
|
||||
g_assert_cmpuint (hb_set_get_population (codepoints), ==, 7);
|
||||
g_assert_cmpuint (hb_set_get_min (codepoints), ==, 0x41);
|
||||
g_assert_cmpuint (hb_set_get_max (codepoints), ==, 0x47);
|
||||
|
||||
hb_set_clear(codepoints);
|
||||
hb_face_collect_nominal_glyph_mapping(face, mapping, codepoints);
|
||||
|
||||
g_assert_cmpuint (hb_set_get_population (codepoints), ==, 7);
|
||||
g_assert_cmpuint (hb_map_get_population (mapping), ==, 7);
|
||||
for (cp = 0x41; cp <= 0x45; cp++)
|
||||
g_assert_cmpuint (hb_map_get(mapping, cp), ==, cp);
|
||||
g_assert_cmpuint (hb_map_get(mapping, 0x46), ==, 0x146);
|
||||
g_assert_cmpuint (hb_map_get(mapping, 0x47), ==, 0x147);
|
||||
|
||||
hb_map_destroy(mapping);
|
||||
hb_set_destroy (codepoints);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_collect_unicodes_format12_notdef (void)
|
||||
{
|
||||
@@ -275,6 +340,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_collect_unicodes_format12_clamped);
|
||||
hb_test_add (test_collect_unicodes_format13);
|
||||
hb_test_add (test_collect_unicodes_format4_with_range_offsets_and_deltas);
|
||||
hb_test_add (test_collect_unicodes_format4_overlapping_segments);
|
||||
|
||||
return hb_test_run();
|
||||
}
|
||||
Reference in new issue
Block a user