1From 85be877925ddbf34f74a1229f3ca1716bb6170dc Mon Sep 17 00:00:00 2001 2From: Behdad Esfahbod <behdad@behdad.org> 3Date: Wed, 1 Feb 2023 20:00:43 -0700 4Subject: [PATCH] [layout] Limit how far we skip when looking back 5 6See comments. 7--- 8 src/hb-ot-layout-gsubgpos.hh | 12 +++++++++++- 9 1 file changed, 11 insertions(+), 1 deletion(-) 10 11diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh 12index c17bf92..712e307 100644 13--- a/src/hb-ot-layout-gsubgpos.hh 14+++ b/src/hb-ot-layout-gsubgpos.hh 15@@ -535,7 +535,19 @@ struct hb_ot_apply_context_t : 16 bool prev () 17 { 18 assert (num_items > 0); 19- while (idx > num_items - 1) 20+ /* The alternate condition below is faster at string boundaries, 21+ * but produces subpar "unsafe-to-concat" values. */ 22+ unsigned stop = num_items - 1; 23+ if (c->buffer->flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) 24+ stop = 1 - 1; 25+ 26+ /* When looking back, limit how far we search; this function is mostly 27+ * used for looking back for base glyphs when attaching marks. If we 28+ * don't limit, we can get O(n^2) behavior where n is the number of 29+ * consecutive marks. */ 30+ stop = (unsigned) hb_max ((int) stop, (int) idx - HB_MAX_CONTEXT_LENGTH); 31+ 32+ while (idx > stop) 33 { 34 idx--; 35 const hb_glyph_info_t &info = c->buffer->out_info[idx]; 36-- 372.33.0 38 39