diff --git a/src/OT/Color/CBDT/CBDT.hh b/src/OT/Color/CBDT/CBDT.hh index 009848fa1..f3e300f49 100644 --- a/src/OT/Color/CBDT/CBDT.hh +++ b/src/OT/Color/CBDT/CBDT.hh @@ -105,6 +105,11 @@ struct cbdt_dedup_context_t hb_hashmap_t map; hb_vector_t entries; hb_vector_t lengths; /* Concatenated per-entry lengths. */ + + /* Bytes that may still be copied out of the source CBDT table, shared + * across strikes. Blocks of different glyphs may overlap or repeat, so + * checking each against the table length does not bound their sum. */ + int64_t budget = hb_int_max (int64_t); }; struct cblc_bitmap_size_subset_context_t @@ -448,6 +453,9 @@ struct IndexSubtableRecord if (unlikely (offset > bitmap_size_context->cbdt_length || bitmap_size_context->cbdt_length - offset < length)) return_trace (false); + if (unlikely (bitmap_size_context->dedup && + !hb_budget_spend (bitmap_size_context->dedup->budget, length))) + return_trace (false); if (unlikely (!c->serializer->check_success ( _copy_data_to_cbdt (&data, bitmap_size_context->cbdt + offset, @@ -1075,6 +1083,7 @@ CBLC::subset (hb_subset_context_t *c) const _copy_data_to_cbdt (&cbdt_prime, cbdt, CBDT::min_size); cbdt_dedup_context_t dedup; + dedup.budget = (int64_t) cbdt_length * HB_SUBSET_CBDT_MAX_COPY_FACTOR; for (const BitmapSizeTable& table : + sizeTables.iter ()) subset_size_table (c, table, (const char *) cbdt, cbdt_length, cblc_prime, &cbdt_prime, &dedup); diff --git a/src/hb-limits.hh b/src/hb-limits.hh index bd61f985d..4a08238a3 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -120,6 +120,14 @@ #define HB_GPU_DRAW_MAX_CURVES 65536 #endif +/* Bytes the CBDT subsetter may copy out of the source CBDT table, as a + * multiple of that table's length. Image blocks of different glyphs or + * strikes may overlap or repeat, so bounds-checking each block against the + * table does not bound their sum. */ +#ifndef HB_SUBSET_CBDT_MAX_COPY_FACTOR +#define HB_SUBSET_CBDT_MAX_COPY_FACTOR 64 +#endif + /* Total bytes of sub-blobs (gradient color stops, clip outlines) in one * hb_gpu_paint_t walk; exceeding it makes the paint unsupported. */ #ifndef HB_GPU_PAINT_MAX_SUB_BYTES diff --git a/test/api/test-subset-cbdt.c b/test/api/test-subset-cbdt.c index dcc06e191..2113d5218 100644 --- a/test/api/test-subset-cbdt.c +++ b/test/api/test-subset-cbdt.c @@ -135,6 +135,71 @@ test_subset_cbdt_index_format_3 (void) hb_face_destroy (face); } +static void +test_subset_cbdt_overlapping_images (void) +{ + /* One format 3 subtable whose offsetArray alternates 0, CBDT_LENGTH, so + * every even glyph claims the whole CBDT table as its image and every odd + * glyph has none. Each block is in bounds, but copying them all would + * blow the output up to NUM_GLYPHS / 2 times the source table. */ + enum { NUM_GLYPHS = 1024, CBDT_LENGTH = 1024 }; + static const char MAXP_data[6] = + "\x00\x00\x50\x00" /* version */ + "\x04\x00" /* numGlyphs */ + ; + static const char CBDT_data[CBDT_LENGTH] = + "\x00\x03\x00\x00" /* version */ + ; + static char CBLC_data[8 + 48 + 8 + 8 + 2 * (NUM_GLYPHS + 1)] = + "\x00\x03\x00\x00" /* version */ + "\x00\x00\x00\x01" /* numSizes */ + /* BitmapSize */ + "\x00\x00\x00\x38" /* indexSubtableArrayOffset */ + "\x00\x00\x00\x00" /* indexTablesSize */ + "\x00\x00\x00\x01" /* numberOfIndexSubtables */ + "\x00\x00\x00\x00" /* colorRef */ + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" /* hori */ + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" /* vert */ + "\x00\x00" /* startGlyphIndex */ + "\x03\xFF" /* endGlyphIndex */ + "\x20\x20\x20\x01" /* ppemX, ppemY, bitDepth, flags */ + /* IndexSubtableRecord */ + "\x00\x00" /* firstGlyphIndex */ + "\x03\xFF" /* lastGlyphIndex */ + "\x00\x00\x00\x08" /* offsetToSubtable */ + /* IndexSubtableFormat3 */ + "\x00\x03" /* indexFormat */ + "\x00\x11" /* imageFormat */ + "\x00\x00\x00\x00" /* imageDataOffset */ + ; + char *offsets = CBLC_data + 8 + 48 + 8 + 8; + for (unsigned i = 1; i <= NUM_GLYPHS; i += 2) + { + offsets[2 * i] = CBDT_LENGTH >> 8; + offsets[2 * i + 1] = CBDT_LENGTH & 0xFF; + } + + hb_face_t *face = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face, "maxp", MAXP_data); + HB_FACE_ADD_TABLE (face, "CBLC", CBLC_data); + HB_FACE_ADD_TABLE (face, "CBDT", CBDT_data); + + hb_set_t *glyphs = hb_set_create (); + hb_set_add_range (glyphs, 0, NUM_GLYPHS - 1); + hb_face_t *face_subset = hb_subset_test_create_subset ( + face, hb_subset_test_create_input_from_glyphs (glyphs)); + hb_set_destroy (glyphs); + + /* The subsetter copies at most HB_SUBSET_CBDT_MAX_COPY_FACTOR times the + * source table; here that drops the strike instead of emitting 512 KiB. */ + hb_blob_t *cbdt = hb_face_reference_table (face_subset, HB_TAG ('C','B','D','T')); + g_assert_cmpuint (hb_blob_get_length (cbdt), <=, 64 * CBDT_LENGTH); + hb_blob_destroy (cbdt); + + hb_face_destroy (face_subset); + hb_face_destroy (face); +} + // TODO: add support/tests for index formats 2,4,5 (image formats are treated as // opaque blobs when subsetting so don't need to be tested separately). // TODO: add a test that keeps no codepoints. @@ -153,6 +218,7 @@ main (int argc, char **argv) // Can use FontTools after https://github.com/fonttools/fonttools/issues/1817 // is resolved. hb_test_add (test_subset_cbdt_index_format_3); + hb_test_add (test_subset_cbdt_overlapping_images); return hb_test_run(); }