diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc index c035543c1..c214de6f9 100644 --- a/src/hb-graphite2.cc +++ b/src/hb-graphite2.cc @@ -298,7 +298,12 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, return true; } - (void) buffer->ensure (glyph_count); + if (unlikely (!buffer->ensure (glyph_count))) + { + if (feats) gr_featureval_destroy (feats); + gr_seg_destroy (seg); + return false; + } scratch = buffer->get_scratch_buffer (&scratch_size); while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) + DIV_CEIL (sizeof (hb_graphite2_glyph_t) * glyph_count, sizeof (*scratch))) > scratch_size) diff --git a/src/meson.build b/src/meson.build index 98b113eef..3b78eda5c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1040,6 +1040,17 @@ if get_option('tests').enabled() suite: ['src', 'subset']) endif + if not cpp_is_microsoft_compiler and conf.get('HAVE_GRAPHITE2', 0) == 1 + test('test-graphite2-buffer', executable('test-graphite2-buffer', ['test-graphite2-buffer.cc', 'hb-static.cc'], + include_directories: incconfig, + cpp_args: cpp_args + ['-UNDEBUG'], + dependencies: libharfbuzz_dep, + install: false, + ), + args: [meson.project_source_root() / 'test/api/fonts/graphite2-expansion.ttf'], + suite: ['src', 'shape']) + endif + if not get_option('raster').disabled() test('test-raster-paint', executable('test-raster-paint', 'test-raster-paint.cc', include_directories: incconfig, diff --git a/src/test-graphite2-buffer.cc b/src/test-graphite2-buffer.cc new file mode 100644 index 000000000..89a2731b1 --- /dev/null +++ b/src/test-graphite2-buffer.cc @@ -0,0 +1,65 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#include "hb-buffer.hh" + +int +main (int argc, char **argv) +{ + hb_always_assert (argc == 2); + hb_face_t *face = hb_face_create_from_file_or_fail (argv[1], 0); + hb_always_assert (face); + hb_font_t *font = hb_font_create (face); + const char *shapers[] = {"graphite2", nullptr}; + + for (unsigned fail = 0; fail < 2; fail++) + { + hb_buffer_t *buffer = hb_buffer_create (); + hb_always_assert (hb_buffer_pre_allocate (buffer, 128)); + hb_buffer_add_utf8 (buffer, "aaaa", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_segment_properties_t props; + hb_buffer_get_segment_properties (buffer, &props); + hb_shape_plan_t *plan = hb_shape_plan_create (face, &props, nullptr, 0, shapers); + hb_always_assert (!strcmp (hb_shape_plan_get_shaper (plan), "graphite2")); + + /* The font expands each character to 41 glyphs. Scratch storage fits + * in the existing allocation, but the 164 output glyphs do not. + * Refuse output growth without depending on system memory pressure. */ + hb_always_assert (buffer->allocated < 164); + hb_always_assert (buffer->allocated * sizeof (hb_glyph_position_t) >= 4 * 24 + 164 * 8); + if (fail) + buffer->max_len = buffer->allocated; + + hb_always_assert (bool (hb_shape_plan_execute (plan, font, buffer, nullptr, 0)) == !fail); + hb_always_assert (bool (hb_buffer_allocation_successful (buffer)) == !fail); + hb_always_assert (hb_buffer_get_length (buffer) == (fail ? 4u : 164u)); + + hb_shape_plan_destroy (plan); + hb_buffer_destroy (buffer); + } + + hb_font_destroy (font); + hb_face_destroy (face); +} diff --git a/test/api/fonts/README b/test/api/fonts/README index 29038b5d4..828ba4dad 100644 --- a/test/api/fonts/README +++ b/test/api/fonts/README @@ -9,3 +9,7 @@ RanaKufi-Regular.subset.otf is from https://github.com/alif-type/rana-kufi/ but glyphs.ttf is from https://github.com/RazrFalcon/ttf-parser/blob/337e7d1/tests/fonts/glyphs.ttf Estedad-VF.ttf, licensed under OFL 1.1, is from https://github.com/aminabedi68/Estedad + +graphite2-expansion.ttf is a synthetic three-glyph font generated by +../generate-graphite2-expansion-font.py. Its Graphite substitution rule expands +each "a" to 41 glyphs, for testing buffer growth failure. diff --git a/test/api/fonts/graphite2-expansion.ttf b/test/api/fonts/graphite2-expansion.ttf new file mode 100644 index 000000000..fb16ea1cf Binary files /dev/null and b/test/api/fonts/graphite2-expansion.ttf differ diff --git a/test/api/generate-graphite2-expansion-font.py b/test/api/generate-graphite2-expansion-font.py new file mode 100644 index 000000000..737d92ef0 --- /dev/null +++ b/test/api/generate-graphite2-expansion-font.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +# Generate the synthetic font used by test-graphite2-buffer. +# Based on Anthropic OSS Scanner finding ANT-2026-HEQW39B7. +# Usage: generate-graphite2-expansion-font.py FONT_OUT +import struct +import sys + +GID_A, GID_X, NUM_GLYPHS = 1, 2, 3 +X_PER_A = 40 # Graphite2 refuses more than 64 glyphs per character + +NEXT, INSERT, RET_ZERO, PUT_GLYPH = 0x19, 0x1F, 0x31, 0x3B # Graphite action opcodes + + +def silf_pass(pass_offset): + # one substitution rule: a > a x x ... x + insert_x = bytes([INSERT, PUT_GLYPH]) + struct.pack(">H", 0) + bytes([NEXT]) # PUT_GLYPH takes a class index + action = bytes([NEXT]) + insert_x * X_PER_A + bytes([RET_ZERO]) + + body = struct.pack(">4H", 1, 6, 0, 0) # numRange and its search header + body += struct.pack(">3H", GID_A, GID_A, 0) # glyph range a..a -> FSM column 0 + body += struct.pack(">2H", 0, 1) # oRuleMap: success state 1 owns ruleMap[0:1] + body += struct.pack(">H", 0) # ruleMap: rule 0 + body += struct.pack(">2B", 0, 0) # min/maxRulePreContext + body += struct.pack(">H", 0) # start state + body += struct.pack(">H", 1) # rule sort key: the rule matches 1 slot + body += struct.pack(">B", 0) # rule pre-context + body += struct.pack(">BH", 0, 0) # collision threshold, pass constraint length + body += struct.pack(">2H", 0, 0) # oConstraints: no rule constraint + body += struct.pack(">2H", 0, len(action)) # oActions + body += struct.pack(">H", 1) # transition: state 0, column 0 -> state 1 + body += b"\0" + code_offset = pass_offset + 32 + len(body) + header = struct.pack(">4B2H4L4H", + 0, 1, 1, 0, # flags, maxRuleLoop, maxRuleContext, maxBackup + 1, 0, # numRules, fsmOffset + code_offset, code_offset, code_offset, 0, # pcCode, rcCode, aCode, oDebug + 2, 1, 1, 1) # numRows, numTransitional, numSuccess, numColumns + return header + body + action + + +def silf_table(): + sub = struct.pack(">H2h", NUM_GLYPHS - 1, 0, 0) # maxGlyphID, extraAscent, extraDescent + sub += struct.pack(">5B", 1, 0, 1, 1, 0xFF) # numPasses, iSubst, iPos, iJust, iBidi + sub += bytes(9) # flags, maxPre/PostContext, five attribute numbers, numJLevels + sub += struct.pack(">H4B", 0, 0, 0, 1, 0) # numLigComp, numUserDefn, maxCompPerLig, direction LTR, attCollisions + sub += bytes(6) # 3 reserved, numCritFeatures, reserved, numScriptTag + sub += struct.pack(">H", 0xFFFF) # lbGID + pseudos = struct.pack(">4H", 0, 0, 0, 0) # numPseudo and its search header + classes = struct.pack(">4H", 1, 1, 8, 10) # numClass, numLinear, class offsets + classes += struct.pack(">H", GID_X) # class 0 = [x] + pass_offset = len(sub) + 8 + len(pseudos) + len(classes) + the_pass = silf_pass(pass_offset) + sub += struct.pack(">2L", pass_offset, pass_offset + len(the_pass)) + pseudos + classes + the_pass + return struct.pack(">L2HL", 0x00020000, 1, 0, 12) + sub # version 2, one sub-table at offset 12 + + +def cmap_table(): + segments = struct.pack(">4H", 4, 4, 1, 0) # segCountX2 and its search header + segments += struct.pack(">2H", 0x61, 0xFFFF) + b"\0\0" # endCode + segments += struct.pack(">2H", 0x61, 0xFFFF) # startCode + segments += struct.pack(">2h", GID_A - 0x61, 1) # idDelta: 'a' -> GID_A + segments += struct.pack(">2H", 0, 0) # idRangeOffset + format_4 = struct.pack(">3H", 4, 6 + len(segments), 0) + segments + return struct.pack(">2H2HL", 0, 1, 3, 1, 12) + format_4 # one Windows Unicode BMP subtable + + +tables = { + b"head": struct.pack(">4L2H16x4h2H3h", 0x00010000, 0, 0, 0x5F0F3CF5, 0, 1000, 0, 0, 0, 0, 0, 8, 2, 0, 0), + b"hhea": struct.pack(">L3hH11hH", 0x00010000, 800, -200, 0, 500, *[0] * 11, NUM_GLYPHS), + b"maxp": struct.pack(">LH26x", 0x00010000, NUM_GLYPHS), + b"hmtx": struct.pack(">Hh", 500, 0) * NUM_GLYPHS, + b"cmap": cmap_table(), + b"Silf": silf_table(), + # Graphite2 wants an attribute run for every glyph: attribute 0, one value, 0 + b"Glat": struct.pack(">L", 0x00010000) + struct.pack(">2Bh", 0, 1, 0) * NUM_GLYPHS, + b"Gloc": struct.pack(">L2H", 0x00010000, 0, 1) + b"".join(struct.pack(">H", 4 + 4 * gid) for gid in range(NUM_GLYPHS + 1)), +} + +directory, data = b"", b"" +for tag in sorted(tables): + directory += struct.pack(">4s3L", tag, 0, 12 + 16 * len(tables) + len(data), len(tables[tag])) + data += tables[tag] + b"\0" * (-len(tables[tag]) % 4) +font = struct.pack(">L4H", 0x00010000, len(tables), 128, 3, 0) + directory + data + +open(sys.argv[1], "wb").write(font)