From 906c9928bb49a1694f6e32d80bb2a1ed637674f8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:13:45 -0600 Subject: [PATCH 01/25] [buffer] Return success status from buffer ops that can fail Previous error-handling philosophy was that user doesn't need to immediately know whether operation failed. But as can be seen after we added malloc-failing fuzzing, there's just so many places in the code that a failure of these operations needs to be mitigated before further operations. So I'm moving towards returning success here, and possibly making it nodiscard. --- src/hb-buffer.cc | 6 ++++-- src/hb-buffer.hh | 33 ++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 0e293f6b5..e09c40ea0 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -343,12 +343,12 @@ hb_buffer_t::swap_buffers () } -void +bool hb_buffer_t::replace_glyphs (unsigned int num_in, unsigned int num_out, const uint32_t *glyph_data) { - if (unlikely (!make_room_for (num_in, num_out))) return; + if (unlikely (!make_room_for (num_in, num_out))) return false; assert (idx + num_in <= len); @@ -365,6 +365,8 @@ hb_buffer_t::replace_glyphs (unsigned int num_in, idx += num_in; out_len += num_out; + + return true; } bool diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 9cad5206e..01ec250c3 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -210,20 +210,22 @@ struct hb_buffer_t HB_INTERNAL void clear_output (); HB_INTERNAL void clear_positions (); - HB_INTERNAL void replace_glyphs (unsigned int num_in, + HB_INTERNAL bool replace_glyphs (unsigned int num_in, unsigned int num_out, const hb_codepoint_t *glyph_data); - void replace_glyph (hb_codepoint_t glyph_index) + bool replace_glyph (hb_codepoint_t glyph_index) { - if (unlikely (out_info != info || out_len != idx)) { - if (unlikely (!make_room_for (1, 1))) return; + if (unlikely (out_info != info || out_len != idx)) + { + if (unlikely (!make_room_for (1, 1))) return false; out_info[out_len] = info[idx]; } out_info[out_len].codepoint = glyph_index; idx++; out_len++; + return true; } /* Makes a copy of the glyph at idx to output and replace glyph_index */ hb_glyph_info_t & output_glyph (hb_codepoint_t glyph_index) @@ -237,59 +239,60 @@ struct hb_buffer_t out_info[out_len].codepoint = glyph_index; out_len++; - return out_info[out_len - 1]; } - void output_info (const hb_glyph_info_t &glyph_info) + bool output_info (const hb_glyph_info_t &glyph_info) { - if (unlikely (!make_room_for (0, 1))) return; + if (unlikely (!make_room_for (0, 1))) return false; out_info[out_len] = glyph_info; out_len++; + return true; } /* Copies glyph at idx to output but doesn't advance idx */ - void copy_glyph () + bool copy_glyph () { - if (unlikely (!make_room_for (0, 1))) return; + if (unlikely (!make_room_for (0, 1))) return false; out_info[out_len] = info[idx]; out_len++; + return true; } /* Copies glyph at idx to output and advance idx. * If there's no output, just advance idx. */ - void - next_glyph () + bool next_glyph () { if (have_output) { if (out_info != info || out_len != idx) { - if (unlikely (!make_room_for (1, 1))) return; + if (unlikely (!make_room_for (1, 1))) return false; out_info[out_len] = info[idx]; } out_len++; } idx++; + return true; } /* Copies n glyphs at idx to output and advance idx. * If there's no output, just advance idx. */ - void - next_glyphs (unsigned int n) + bool next_glyphs (unsigned int n) { if (have_output) { if (out_info != info || out_len != idx) { - if (unlikely (!make_room_for (n, n))) return; + if (unlikely (!make_room_for (n, n))) return false; memmove (out_info + out_len, info + idx, n * sizeof (out_info[0])); } out_len += n; } idx += n; + return true; } /* Advance idx without copying to output. */ void skip_glyph () { idx++; } From 607979d12f85eb8260b7d7b786a7866e60c3c49b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:23:48 -0600 Subject: [PATCH 02/25] [buffer] HB_NODISCARD replace_glyphs() --- src/hb-buffer.hh | 7 ++++--- src/hb-ot-shape-complex-hangul.cc | 10 ++++------ src/hb-ot-shape-normalize.cc | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 01ec250c3..08781869f 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -210,9 +210,10 @@ struct hb_buffer_t HB_INTERNAL void clear_output (); HB_INTERNAL void clear_positions (); - HB_INTERNAL bool replace_glyphs (unsigned int num_in, - unsigned int num_out, - const hb_codepoint_t *glyph_data); + HB_INTERNAL HB_NODISCARD + bool replace_glyphs (unsigned int num_in, + unsigned int num_out, + const hb_codepoint_t *glyph_data); bool replace_glyph (hb_codepoint_t glyph_index) { diff --git a/src/hb-ot-shape-complex-hangul.cc b/src/hb-ot-shape-complex-hangul.cc index 533e928dc..2f1bf5e1a 100644 --- a/src/hb-ot-shape-complex-hangul.cc +++ b/src/hb-ot-shape-complex-hangul.cc @@ -231,7 +231,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, chars[0] = 0x25CCu; chars[1] = u; } - buffer->replace_glyphs (1, 2, chars); + (void) buffer->replace_glyphs (1, 2, chars); } else { @@ -273,7 +273,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, hb_codepoint_t s = SBase + (l - LBase) * NCount + (v - VBase) * TCount + tindex; if (font->has_glyph (s)) { - buffer->replace_glyphs (t ? 3 : 2, 1, &s); + (void) buffer->replace_glyphs (t ? 3 : 2, 1, &s); end = start + 1; continue; } @@ -323,9 +323,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, hb_codepoint_t new_s = s + new_tindex; if (font->has_glyph (new_s)) { - buffer->replace_glyphs (2, 1, &new_s); - if (unlikely (!buffer->successful)) - break; + (void) buffer->replace_glyphs (2, 1, &new_s); end = start + 1; continue; } @@ -349,7 +347,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, (!tindex || font->has_glyph (decomposed[2]))) { unsigned int s_len = tindex ? 3 : 2; - buffer->replace_glyphs (1, s_len, decomposed); + (void) buffer->replace_glyphs (1, s_len, decomposed); /* If we decomposed an LV because of a non-combining T following, * we want to include this T in the syllable. diff --git a/src/hb-ot-shape-normalize.cc b/src/hb-ot-shape-normalize.cc index 1453d2e3b..776e29902 100644 --- a/src/hb-ot-shape-normalize.cc +++ b/src/hb-ot-shape-normalize.cc @@ -229,7 +229,7 @@ handle_variation_selector_cluster (const hb_ot_shape_normalize_context_t *c, if (font->get_variation_glyph (buffer->cur().codepoint, buffer->cur(+1).codepoint, &buffer->cur().glyph_index())) { hb_codepoint_t unicode = buffer->cur().codepoint; - buffer->replace_glyphs (2, 1, &unicode); + (void) buffer->replace_glyphs (2, 1, &unicode); } else { From 3f1998a065075284e7fe57788411bcc086259349 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:33:44 -0600 Subject: [PATCH 03/25] [buffer] HB_NODISCARD replace_glyph() --- src/hb-aat-layout-morx-table.hh | 4 ++-- src/hb-buffer.hh | 2 +- src/hb-ot-layout-gsubgpos.hh | 4 ++-- src/hb-ot-shape-complex-thai.cc | 5 ++--- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index cf6766b04..972c2974b 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -525,7 +525,7 @@ struct LigatureSubtable hb_codepoint_t lig = ligatureData; DEBUG_MSG (APPLY, nullptr, "Produced ligature %u", lig); - buffer->replace_glyph (lig); + if (unlikely (!buffer->replace_glyph (lig))) return; unsigned int lig_end = match_positions[(match_length - 1u) % ARRAY_LENGTH (match_positions)] + 1u; /* Now go and delete all subsequent components. */ @@ -533,7 +533,7 @@ struct LigatureSubtable { DEBUG_MSG (APPLY, nullptr, "Skipping ligature component"); buffer->move_to (match_positions[--match_length % ARRAY_LENGTH (match_positions)]); - buffer->replace_glyph (DELETED_GLYPH); + if (unlikely (!buffer->replace_glyph (DELETED_GLYPH))) return; } buffer->move_to (lig_end); diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 08781869f..00eeda4f1 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -215,7 +215,7 @@ struct hb_buffer_t unsigned int num_out, const hb_codepoint_t *glyph_data); - bool replace_glyph (hb_codepoint_t glyph_index) + HB_NODISCARD bool replace_glyph (hb_codepoint_t glyph_index) { if (unlikely (out_info != info || out_len != idx)) { diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 7655af12d..ee9fea99b 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -659,7 +659,7 @@ struct hb_ot_apply_context_t : void replace_glyph (hb_codepoint_t glyph_index) const { _set_glyph_props (glyph_index); - buffer->replace_glyph (glyph_index); + (void) buffer->replace_glyph (glyph_index); } void replace_glyph_inplace (hb_codepoint_t glyph_index) const { @@ -670,7 +670,7 @@ struct hb_ot_apply_context_t : unsigned int class_guess) const { _set_glyph_props (glyph_index, class_guess, true); - buffer->replace_glyph (glyph_index); + (void) buffer->replace_glyph (glyph_index); } void output_glyph_for_component (hb_codepoint_t glyph_index, unsigned int class_guess) const diff --git a/src/hb-ot-shape-complex-thai.cc b/src/hb-ot-shape-complex-thai.cc index 347ea2e7a..9159c9f36 100644 --- a/src/hb-ot-shape-complex-thai.cc +++ b/src/hb-ot-shape-complex-thai.cc @@ -334,9 +334,8 @@ preprocess_text_thai (const hb_ot_shape_plan_t *plan, /* Is SARA AM. Decompose and reorder. */ hb_glyph_info_t &nikhahit = buffer->output_glyph (NIKHAHIT_FROM_SARA_AM (u)); _hb_glyph_info_set_continuation (&nikhahit); - buffer->replace_glyph (SARA_AA_FROM_SARA_AM (u)); - if (unlikely (!buffer->successful)) - return; + if (unlikely (!buffer->replace_glyph (SARA_AA_FROM_SARA_AM (u)))) + break; /* Make Nikhahit be recognized as a ccc=0 mark when zeroing widths. */ unsigned int end = buffer->out_len; From d8028a076208d95fa66050d158f2c4b8c50040ad Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:34:36 -0600 Subject: [PATCH 04/25] [buffer] HB_NODISCARD enlarge() --- src/hb-buffer.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 00eeda4f1..ae96825db 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -335,7 +335,7 @@ struct hb_buffer_t /* Internal methods */ HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */ - HB_INTERNAL bool enlarge (unsigned int size); + HB_INTERNAL HB_NODISCARD bool enlarge (unsigned int size); bool ensure (unsigned int size) { return likely (!size || size < allocated) ? true : enlarge (size); } From 05d2d37f9a6753c2c3e0a2404e171f0b0a26c4ac Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:43:29 -0600 Subject: [PATCH 05/25] [buffer] HB_NODISCARD ensure() --- src/hb-buffer.cc | 7 +++++-- src/hb-buffer.hh | 2 +- src/hb-graphite2.cc | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index e09c40ea0..8da090ad4 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -1318,7 +1318,7 @@ hb_buffer_set_length (hb_buffer_t *buffer, if (unlikely (hb_object_is_immutable (buffer))) return length == 0; - if (!buffer->ensure (length)) + if (unlikely (!buffer->ensure (length))) return false; /* Wipe the new space */ @@ -1548,7 +1548,10 @@ hb_buffer_add_utf (hb_buffer_t *buffer, if (item_length == -1) item_length = text_length - item_offset; - buffer->ensure (buffer->len + item_length * sizeof (T) / 4); + if (unlikely (item_length < 0 || + item_length > INT_MAX / 8 || + !buffer->ensure (buffer->len + item_length * sizeof (T) / 4))) + return; /* If buffer is empty and pre-context provided, install it. * This check is written this way, to make sure people can diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index ae96825db..8fbc1950a 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -337,7 +337,7 @@ struct hb_buffer_t HB_INTERNAL HB_NODISCARD bool enlarge (unsigned int size); - bool ensure (unsigned int size) + HB_NODISCARD bool ensure (unsigned int size) { return likely (!size || size < allocated) ? true : enlarge (size); } bool ensure_inplace (unsigned int size) diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc index 220ba4fc1..9dafe654c 100644 --- a/src/hb-graphite2.cc +++ b/src/hb-graphite2.cc @@ -289,7 +289,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, return true; } - buffer->ensure (glyph_count); + (void) buffer->ensure (glyph_count); scratch = buffer->get_scratch_buffer (&scratch_size); while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) + DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size) From cac6c86d2f8013a6f803f95e5760d7be6d27bbe0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:46:54 -0600 Subject: [PATCH 06/25] [buffer] HB_NODISCARD move_to() --- src/hb-aat-layout-morx-table.hh | 14 +++++++------- src/hb-buffer.hh | 2 +- src/hb-ot-layout-gsubgpos.hh | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 972c2974b..15c03dbe6 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -499,7 +499,7 @@ struct LigatureSubtable } DEBUG_MSG (APPLY, nullptr, "Moving to stack position %u", cursor - 1); - buffer->move_to (match_positions[--cursor % ARRAY_LENGTH (match_positions)]); + if (unlikely (!buffer->move_to (match_positions[--cursor % ARRAY_LENGTH (match_positions)]))) return; if (unlikely (!actionData->sanitize (&c->sanitizer))) break; action = *actionData; @@ -532,18 +532,18 @@ struct LigatureSubtable while (match_length - 1u > cursor) { DEBUG_MSG (APPLY, nullptr, "Skipping ligature component"); - buffer->move_to (match_positions[--match_length % ARRAY_LENGTH (match_positions)]); + if (unlikely (!buffer->move_to (match_positions[--match_length % ARRAY_LENGTH (match_positions)]))) return; if (unlikely (!buffer->replace_glyph (DELETED_GLYPH))) return; } - buffer->move_to (lig_end); + if (unlikely (!buffer->move_to (lig_end))) return; buffer->merge_out_clusters (match_positions[cursor % ARRAY_LENGTH (match_positions)], buffer->out_len); } actionData++; } while (!(action & LigActionLast)); - buffer->move_to (end); + if (unlikely (!buffer->move_to (end))) return; } } @@ -733,7 +733,7 @@ struct InsertionSubtable bool before = flags & MarkedInsertBefore; unsigned int end = buffer->out_len; - buffer->move_to (mark); + if (unlikely (!buffer->move_to (mark))) return; if (buffer->idx < buffer->len && !before) buffer->copy_glyph (); @@ -743,7 +743,7 @@ struct InsertionSubtable if (buffer->idx < buffer->len && !before) buffer->skip_glyph (); - buffer->move_to (end + count); + if (unlikely (!buffer->move_to (end + count))) return; buffer->unsafe_to_break_from_outbuffer (mark, hb_min (buffer->idx + 1, buffer->len)); } @@ -786,7 +786,7 @@ struct InsertionSubtable * * https://github.com/harfbuzz/harfbuzz/issues/1224#issuecomment-427691417 */ - buffer->move_to ((flags & DontAdvance) ? end : end + count); + if (unlikely (!buffer->move_to ((flags & DontAdvance) ? end : end + count))) return; } } diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 8fbc1950a..a3819a375 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -333,7 +333,7 @@ struct hb_buffer_t /* Internal methods */ - HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */ + HB_INTERNAL HB_NODISCARD bool move_to (unsigned int i); /* i is output-buffer index. */ HB_INTERNAL HB_NODISCARD bool enlarge (unsigned int size); diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index ee9fea99b..bfc58624f 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -1272,7 +1272,7 @@ static inline bool apply_lookup (hb_ot_apply_context_t *c, match_positions[next] += delta; } - buffer->move_to (end); + (void) buffer->move_to (end); return_trace (true); } From bc22305b6ab626719e8b0c0d6fa518bf7b7267f6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:57:18 -0600 Subject: [PATCH 07/25] [buffer] HB_NODISCARD ensure_inplace() --- src/hb-buffer.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index a3819a375..959ce70bd 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -340,7 +340,7 @@ struct hb_buffer_t HB_NODISCARD bool ensure (unsigned int size) { return likely (!size || size < allocated) ? true : enlarge (size); } - bool ensure_inplace (unsigned int size) + HB_NODISCARD bool ensure_inplace (unsigned int size) { return likely (!size || size < allocated); } void assert_glyphs () From c355508a8907584e41c4317fb56eac49f9627969 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:58:30 -0600 Subject: [PATCH 08/25] [buffer] HB_NODISCARD ensure_glyphs() / ensure_unicode() --- src/hb-buffer.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 959ce70bd..6a8415e2e 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -353,7 +353,7 @@ struct hb_buffer_t assert ((content_type == HB_BUFFER_CONTENT_TYPE_UNICODE) || (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID))); } - bool ensure_glyphs () + HB_NODISCARD bool ensure_glyphs () { if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_GLYPHS)) { @@ -364,7 +364,7 @@ struct hb_buffer_t } return true; } - bool ensure_unicode () + HB_NODISCARD bool ensure_unicode () { if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_UNICODE)) { From 83b3784d1d984ed3a94b696252b03eab514e3ffa Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 13:59:14 -0600 Subject: [PATCH 09/25] [buffer] HB_NODISCARD make_room_for() --- src/hb-buffer.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 6a8415e2e..48f44092f 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -376,7 +376,7 @@ struct hb_buffer_t return true; } - HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out); + HB_INTERNAL HB_NODISCARD bool make_room_for (unsigned int num_in, unsigned int num_out); HB_INTERNAL bool shift_forward (unsigned int count); typedef long scratch_buffer_t; From 41e05479b6f1cc024576aca1f52b2185bdd93cef Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:00:00 -0600 Subject: [PATCH 10/25] [buffer] HB_NODISCARD shift_forward() --- src/hb-buffer.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 48f44092f..e5d360f11 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -377,7 +377,7 @@ struct hb_buffer_t } HB_INTERNAL HB_NODISCARD bool make_room_for (unsigned int num_in, unsigned int num_out); - HB_INTERNAL bool shift_forward (unsigned int count); + HB_INTERNAL HB_NODISCARD bool shift_forward (unsigned int count); typedef long scratch_buffer_t; HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size); From 8d3701f507cfda835855a64f598bed468a4b31e3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:01:32 -0600 Subject: [PATCH 11/25] [buffer] HB_NODISCARD in_error() --- src/hb-buffer.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index e5d360f11..0354bbb9e 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -139,7 +139,7 @@ struct hb_buffer_t /* Methods */ - bool in_error () const { return !successful; } + HB_NODISCARD bool in_error () const { return !successful; } void allocate_var (unsigned int start, unsigned int count) { From 4ae8aab83b83bc088719a993a359625b9ff1b6ab Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:01:52 -0600 Subject: [PATCH 12/25] [buffer] HB_NODISCARD has_separate_output() --- src/hb-buffer.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 0354bbb9e..0582d3a21 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -186,7 +186,7 @@ struct hb_buffer_t hb_glyph_info_t &prev () { return out_info[out_len ? out_len - 1 : 0]; } hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; } - bool has_separate_output () const { return info != out_info; } + HB_NODISCARD bool has_separate_output () const { return info != out_info; } HB_INTERNAL void reset (); From b05e5d9a7998529ffd0e34f3b8d3ae5bf4c200eb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:08:08 -0600 Subject: [PATCH 13/25] [buffer] HB_NODISCARD next_glyphs() --- src/hb-buffer.cc | 2 +- src/hb-buffer.hh | 2 +- src/hb-ot-shape-normalize.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 8da090ad4..494bf395e 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -319,7 +319,7 @@ hb_buffer_t::swap_buffers () if (unlikely (!successful)) return; assert (idx <= len); - next_glyphs (len - idx); + if (unlikely (!next_glyphs (len - idx))) return; assert (have_output); have_output = false; diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 0582d3a21..2e43f2e69 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -280,7 +280,7 @@ struct hb_buffer_t } /* Copies n glyphs at idx to output and advance idx. * If there's no output, just advance idx. */ - bool next_glyphs (unsigned int n) + HB_NODISCARD bool next_glyphs (unsigned int n) { if (have_output) { diff --git a/src/hb-ot-shape-normalize.cc b/src/hb-ot-shape-normalize.cc index 776e29902..bce9dcf5d 100644 --- a/src/hb-ot-shape-normalize.cc +++ b/src/hb-ot-shape-normalize.cc @@ -348,7 +348,7 @@ _hb_ot_shape_normalize (const hb_ot_shape_plan_t *plan, sizeof (buffer->info[0]), &buffer->cur().glyph_index(), sizeof (buffer->info[0])); - buffer->next_glyphs (done); + if (unlikely (!buffer->next_glyphs (done))) break; } while (buffer->idx < end && buffer->successful) decompose_current_character (&c, might_short_circuit); From 2a0dbb3ee59a83d1db568b002a17d617f4c45b63 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:10:39 -0600 Subject: [PATCH 14/25] [buffer] HB_NODISCARD copy_glyph() --- src/hb-aat-layout-morx-table.hh | 4 ++-- src/hb-buffer.hh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 15c03dbe6..b9fabe2b1 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -736,7 +736,7 @@ struct InsertionSubtable if (unlikely (!buffer->move_to (mark))) return; if (buffer->idx < buffer->len && !before) - buffer->copy_glyph (); + if (unlikely (!buffer->copy_glyph ())) return; /* TODO We ignore KashidaLike setting. */ for (unsigned int i = 0; i < count; i++) buffer->output_glyph (glyphs[i]); @@ -764,7 +764,7 @@ struct InsertionSubtable unsigned int end = buffer->out_len; if (buffer->idx < buffer->len && !before) - buffer->copy_glyph (); + if (unlikely (!buffer->copy_glyph ())) return; /* TODO We ignore KashidaLike setting. */ for (unsigned int i = 0; i < count; i++) buffer->output_glyph (glyphs[i]); diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 2e43f2e69..7ee204496 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -252,7 +252,7 @@ struct hb_buffer_t return true; } /* Copies glyph at idx to output but doesn't advance idx */ - bool copy_glyph () + HB_NODISCARD bool copy_glyph () { if (unlikely (!make_room_for (0, 1))) return false; From e6be9eb4fbb9a5d1e028015cf1f92601da2984ac Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:12:10 -0600 Subject: [PATCH 15/25] [buffer] HB_NODISCARD output_info() --- src/hb-buffer.hh | 2 +- src/hb-ot-shape-complex-syllabic.cc | 2 +- src/hb-ot-shape.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 7ee204496..97c5a0279 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -242,7 +242,7 @@ struct hb_buffer_t out_len++; return out_info[out_len - 1]; } - bool output_info (const hb_glyph_info_t &glyph_info) + HB_NODISCARD bool output_info (const hb_glyph_info_t &glyph_info) { if (unlikely (!make_room_for (0, 1))) return false; diff --git a/src/hb-ot-shape-complex-syllabic.cc b/src/hb-ot-shape-complex-syllabic.cc index 9f8c790bc..843fed690 100644 --- a/src/hb-ot-shape-complex-syllabic.cc +++ b/src/hb-ot-shape-complex-syllabic.cc @@ -88,7 +88,7 @@ hb_syllabic_insert_dotted_circles (hb_font_t *font, buffer->next_glyph (); } - buffer->output_info (ginfo); + (void) buffer->output_info (ginfo); } else buffer->next_glyph (); diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index 92831e022..86ab0b426 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -534,7 +534,7 @@ hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font) hb_glyph_info_t info = dottedcircle; info.cluster = buffer->cur().cluster; info.mask = buffer->cur().mask; - buffer->output_info (info); + (void) buffer->output_info (info); buffer->swap_buffers (); } From 34a1204f10eb1dedbd49c63eb7d89b86315ca093 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:39:06 -0600 Subject: [PATCH 16/25] [buffer] HB_NODISCARD output_glyph() Also, generalize and use replace_glyphs() in morx where output_glyph() was used in a loop. --- src/gen-vowel-constraints.py | 4 +- src/hb-aat-layout-morx-table.hh | 6 +-- src/hb-buffer.cc | 27 ------------- src/hb-buffer.hh | 40 ++++++++++++++------ src/hb-ot-layout-gsubgpos.hh | 2 +- src/hb-ot-shape-complex-thai.cc | 7 ++-- src/hb-ot-shape-complex-vowel-constraints.cc | 4 +- src/hb-ot-shape-normalize.cc | 3 +- 8 files changed, 41 insertions(+), 52 deletions(-) diff --git a/src/gen-vowel-constraints.py b/src/gen-vowel-constraints.py index 55a9ecf86..2e9b50bad 100755 --- a/src/gen-vowel-constraints.py +++ b/src/gen-vowel-constraints.py @@ -171,8 +171,8 @@ print () print ('static void') print ('_output_dotted_circle (hb_buffer_t *buffer)') print ('{') -print (' hb_glyph_info_t &dottedcircle = buffer->output_glyph (0x25CCu);') -print (' _hb_glyph_info_reset_continuation (&dottedcircle);') +print (' (void) buffer->output_glyph (0x25CCu);') +print (' _hb_glyph_info_reset_continuation (&buffer->prev());') print ('}') print () print ('static void') diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index b9fabe2b1..e3bc268d2 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -738,8 +738,7 @@ struct InsertionSubtable if (buffer->idx < buffer->len && !before) if (unlikely (!buffer->copy_glyph ())) return; /* TODO We ignore KashidaLike setting. */ - for (unsigned int i = 0; i < count; i++) - buffer->output_glyph (glyphs[i]); + if (unlikely (!buffer->replace_glyphs (0, count, glyphs))) return; if (buffer->idx < buffer->len && !before) buffer->skip_glyph (); @@ -766,8 +765,7 @@ struct InsertionSubtable if (buffer->idx < buffer->len && !before) if (unlikely (!buffer->copy_glyph ())) return; /* TODO We ignore KashidaLike setting. */ - for (unsigned int i = 0; i < count; i++) - buffer->output_glyph (glyphs[i]); + if (unlikely (!buffer->replace_glyphs (0, count, glyphs))) return; if (buffer->idx < buffer->len && !before) buffer->skip_glyph (); diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 494bf395e..8cad6ab8e 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -342,33 +342,6 @@ hb_buffer_t::swap_buffers () idx = 0; } - -bool -hb_buffer_t::replace_glyphs (unsigned int num_in, - unsigned int num_out, - const uint32_t *glyph_data) -{ - if (unlikely (!make_room_for (num_in, num_out))) return false; - - assert (idx + num_in <= len); - - merge_clusters (idx, idx + num_in); - - hb_glyph_info_t orig_info = info[idx]; - hb_glyph_info_t *pinfo = &out_info[out_len]; - for (unsigned int i = 0; i < num_out; i++) - { - *pinfo = orig_info; - pinfo->codepoint = glyph_data[i]; - pinfo++; - } - - idx += num_in; - out_len += num_out; - - return true; -} - bool hb_buffer_t::move_to (unsigned int i) { diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 97c5a0279..c457e7392 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -210,10 +210,31 @@ struct hb_buffer_t HB_INTERNAL void clear_output (); HB_INTERNAL void clear_positions (); - HB_INTERNAL HB_NODISCARD - bool replace_glyphs (unsigned int num_in, - unsigned int num_out, - const hb_codepoint_t *glyph_data); + template + HB_NODISCARD bool replace_glyphs (unsigned int num_in, + unsigned int num_out, + const T *glyph_data) + { + if (unlikely (!make_room_for (num_in, num_out))) return false; + + assert (idx + num_in <= len); + + merge_clusters (idx, idx + num_in); + + hb_glyph_info_t &orig_info = idx < len ? cur() : prev(); + + hb_glyph_info_t *pinfo = &out_info[out_len]; + for (unsigned int i = 0; i < num_out; i++) + { + *pinfo = orig_info; + pinfo->codepoint = glyph_data[i]; + pinfo++; + } + + idx += num_in; + out_len += num_out; + return true; + } HB_NODISCARD bool replace_glyph (hb_codepoint_t glyph_index) { @@ -229,18 +250,15 @@ struct hb_buffer_t return true; } /* Makes a copy of the glyph at idx to output and replace glyph_index */ - hb_glyph_info_t & output_glyph (hb_codepoint_t glyph_index) + HB_NODISCARD bool output_glyph (hb_codepoint_t glyph_index) { - if (unlikely (!make_room_for (0, 1))) return Crap (hb_glyph_info_t); + if (unlikely (!make_room_for (0, 1))) return false; - if (unlikely (idx == len && !out_len)) - return Crap (hb_glyph_info_t); - - out_info[out_len] = idx < len ? info[idx] : out_info[out_len - 1]; + out_info[out_len] = idx < len ? cur() : prev(); out_info[out_len].codepoint = glyph_index; out_len++; - return out_info[out_len - 1]; + return true; } HB_NODISCARD bool output_info (const hb_glyph_info_t &glyph_info) { diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index bfc58624f..b93b73c04 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -676,7 +676,7 @@ struct hb_ot_apply_context_t : unsigned int class_guess) const { _set_glyph_props (glyph_index, class_guess, false, true); - buffer->output_glyph (glyph_index); + (void) buffer->output_glyph (glyph_index); } }; diff --git a/src/hb-ot-shape-complex-thai.cc b/src/hb-ot-shape-complex-thai.cc index 9159c9f36..925e28d3b 100644 --- a/src/hb-ot-shape-complex-thai.cc +++ b/src/hb-ot-shape-complex-thai.cc @@ -332,10 +332,9 @@ preprocess_text_thai (const hb_ot_shape_plan_t *plan, } /* Is SARA AM. Decompose and reorder. */ - hb_glyph_info_t &nikhahit = buffer->output_glyph (NIKHAHIT_FROM_SARA_AM (u)); - _hb_glyph_info_set_continuation (&nikhahit); - if (unlikely (!buffer->replace_glyph (SARA_AA_FROM_SARA_AM (u)))) - break; + if (unlikely (!buffer->output_glyph (NIKHAHIT_FROM_SARA_AM (u)))) break; + _hb_glyph_info_set_continuation (&buffer->prev()); + if (unlikely (!buffer->replace_glyph (SARA_AA_FROM_SARA_AM (u)))) break; /* Make Nikhahit be recognized as a ccc=0 mark when zeroing widths. */ unsigned int end = buffer->out_len; diff --git a/src/hb-ot-shape-complex-vowel-constraints.cc b/src/hb-ot-shape-complex-vowel-constraints.cc index 124716eb6..2c61e976f 100644 --- a/src/hb-ot-shape-complex-vowel-constraints.cc +++ b/src/hb-ot-shape-complex-vowel-constraints.cc @@ -23,8 +23,8 @@ static void _output_dotted_circle (hb_buffer_t *buffer) { - hb_glyph_info_t &dottedcircle = buffer->output_glyph (0x25CCu); - _hb_glyph_info_reset_continuation (&dottedcircle); + (void) buffer->output_glyph (0x25CCu); + _hb_glyph_info_reset_continuation (&buffer->prev()); } static void diff --git a/src/hb-ot-shape-normalize.cc b/src/hb-ot-shape-normalize.cc index bce9dcf5d..8ad2d771a 100644 --- a/src/hb-ot-shape-normalize.cc +++ b/src/hb-ot-shape-normalize.cc @@ -101,8 +101,9 @@ set_glyph (hb_glyph_info_t &info, hb_font_t *font) static inline void output_char (hb_buffer_t *buffer, hb_codepoint_t unichar, hb_codepoint_t glyph) { + /* This is very confusing indeed. */ buffer->cur().glyph_index() = glyph; - buffer->output_glyph (unichar); /* This is very confusing indeed. */ + (void) buffer->output_glyph (unichar); _hb_glyph_info_set_unicode_props (&buffer->prev(), buffer); } From 862f913489a868154a3733a93e1ea0407e68cc05 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:48:50 -0600 Subject: [PATCH 17/25] [buffer] Implement output_glyph() in terms of replace_glyphs(0,1) To my surprise, saves ~20kb in my build (non-size-optimized) build. The output_glyph() method is never used in the fast paths, so doesn't matter if is not fully optimized for the special case it is. --- src/hb-buffer.hh | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index c457e7392..1a5229c57 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -251,15 +251,8 @@ struct hb_buffer_t } /* Makes a copy of the glyph at idx to output and replace glyph_index */ HB_NODISCARD bool output_glyph (hb_codepoint_t glyph_index) - { - if (unlikely (!make_room_for (0, 1))) return false; + { return replace_glyphs (0, 1, &glyph_index); } - out_info[out_len] = idx < len ? cur() : prev(); - out_info[out_len].codepoint = glyph_index; - - out_len++; - return true; - } HB_NODISCARD bool output_info (const hb_glyph_info_t &glyph_info) { if (unlikely (!make_room_for (0, 1))) return false; From f73982a6992445141ea7ca402d1f4fea80f151c4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:52:19 -0600 Subject: [PATCH 18/25] [buffer] Implement replace_glyph() in terms of replace_glyphs(1,1) I get exact same binary size with this, suggesting that compiler is optimizing these as needed. --- src/hb-buffer.hh | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 1a5229c57..7b4e92985 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -237,18 +237,8 @@ struct hb_buffer_t } HB_NODISCARD bool replace_glyph (hb_codepoint_t glyph_index) - { - if (unlikely (out_info != info || out_len != idx)) - { - if (unlikely (!make_room_for (1, 1))) return false; - out_info[out_len] = info[idx]; - } - out_info[out_len].codepoint = glyph_index; + { return replace_glyphs (1, 1, &glyph_index); } - idx++; - out_len++; - return true; - } /* Makes a copy of the glyph at idx to output and replace glyph_index */ HB_NODISCARD bool output_glyph (hb_codepoint_t glyph_index) { return replace_glyphs (0, 1, &glyph_index); } From f4bc7673db762db8dbb83caaeeb32b2c04dbcf9a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 14:54:45 -0600 Subject: [PATCH 19/25] [buffer] Implement copy_glyph() in terms of output_info() --- src/hb-buffer.hh | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 7b4e92985..dac2a461d 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -255,13 +255,11 @@ struct hb_buffer_t /* Copies glyph at idx to output but doesn't advance idx */ HB_NODISCARD bool copy_glyph () { - if (unlikely (!make_room_for (0, 1))) return false; - - out_info[out_len] = info[idx]; - - out_len++; - return true; + /* Extra copy because cur()'s return can be freed within + * output_info() call if buffer reallocates. */ + return output_info (hb_glyph_info_t (cur())); } + /* Copies glyph at idx to output and advance idx. * If there's no output, just advance idx. */ bool next_glyph () From 8450f43ae1d1a5825bdfc1ec665cb81b05f35cc2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 15:18:06 -0600 Subject: [PATCH 20/25] [buffer] HB_NODISCARD next_glyph() --- src/gen-vowel-constraints.py | 6 ++-- src/hb-aat-layout-common.hh | 2 +- src/hb-buffer.hh | 2 +- src/hb-ot-layout-gsubgpos.hh | 2 +- src/hb-ot-layout.cc | 2 +- src/hb-ot-shape-complex-hangul.cc | 18 +++++------ src/hb-ot-shape-complex-syllabic.cc | 4 +-- src/hb-ot-shape-complex-thai.cc | 9 +++--- src/hb-ot-shape-complex-vowel-constraints.cc | 34 ++++++++++---------- src/hb-ot-shape-normalize.cc | 33 ++++++++++--------- 10 files changed, 57 insertions(+), 55 deletions(-) diff --git a/src/gen-vowel-constraints.py b/src/gen-vowel-constraints.py index 2e9b50bad..184ec29cf 100755 --- a/src/gen-vowel-constraints.py +++ b/src/gen-vowel-constraints.py @@ -101,7 +101,7 @@ class ConstraintSet (object): self._indent (depth + 2), cp, index + i, ')' if i == len (self._c) - 1 else ' &&')) s.append ('{}{{\n'.format (indent)) for i in range (index): - s.append ('{}buffer->next_glyph ();\n'.format (self._indent (depth + 1))) + s.append ('{}(void) buffer->next_glyph ();\n'.format (self._indent (depth + 1))) s.append ('{}matched = true;\n'.format (self._indent (depth + 1))) s.append ('{}}}\n'.format (indent)) else: @@ -179,7 +179,7 @@ print ('static void') print ('_output_with_dotted_circle (hb_buffer_t *buffer)') print ('{') print (' _output_dotted_circle (buffer);') -print (' buffer->next_glyph ();') +print (' (void) buffer->next_glyph ();') print ('}') print () @@ -211,7 +211,7 @@ for script, constraints in sorted (constraints.items (), key=lambda s_c: script_ print (' {') print ('\tbool matched = false;') write (str (constraints)) - print ('\tbuffer->next_glyph ();') + print ('\t(void) buffer->next_glyph ();') print ('\tif (matched) _output_with_dotted_circle (buffer);') print (' }') print (' break;') diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index d8223dc8e..98ed20d8e 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -831,7 +831,7 @@ struct StateTableDriver break; if (!(entry.flags & context_t::DontAdvance) || buffer->max_ops-- <= 0) - buffer->next_glyph (); + (void) buffer->next_glyph (); } if (!c->in_place) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index dac2a461d..7a754175e 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -262,7 +262,7 @@ struct hb_buffer_t /* Copies glyph at idx to output and advance idx. * If there's no output, just advance idx. */ - bool next_glyph () + HB_NODISCARD bool next_glyph () { if (have_output) { diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index b93b73c04..36a95ead1 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -1043,7 +1043,7 @@ static inline bool ligate_input (hb_ot_apply_context_t *c, hb_min (this_comp, last_num_components); _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp); } - buffer->next_glyph (); + (void) buffer->next_glyph (); } last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur()); diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 089c7948c..89df949b2 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -1782,7 +1782,7 @@ apply_forward (OT::hb_ot_apply_context_t *c, if (applied) ret = true; else - buffer->next_glyph (); + (void) buffer->next_glyph (); } return ret; } diff --git a/src/hb-ot-shape-complex-hangul.cc b/src/hb-ot-shape-complex-hangul.cc index 2f1bf5e1a..dbedd6af0 100644 --- a/src/hb-ot-shape-complex-hangul.cc +++ b/src/hb-ot-shape-complex-hangul.cc @@ -205,7 +205,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, { /* Tone mark follows a valid syllable; move it in front, unless it's zero width. */ buffer->unsafe_to_break_from_outbuffer (start, buffer->idx); - buffer->next_glyph (); + if (unlikely (!buffer->next_glyph ())) break; if (!is_zero_width_char (font, u)) { buffer->merge_out_clusters (start, end + 1); @@ -236,7 +236,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, else { /* No dotted circle available in the font; just leave tone mark untouched. */ - buffer->next_glyph (); + (void) buffer->next_glyph (); } } start = end = buffer->out_len; @@ -285,13 +285,13 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, * Set jamo features on the individual glyphs, and advance past them. */ buffer->cur().hangul_shaping_feature() = LJMO; - buffer->next_glyph (); + (void) buffer->next_glyph (); buffer->cur().hangul_shaping_feature() = VJMO; - buffer->next_glyph (); + (void) buffer->next_glyph (); if (t) { buffer->cur().hangul_shaping_feature() = TJMO; - buffer->next_glyph (); + (void) buffer->next_glyph (); end = start + 3; } else @@ -354,7 +354,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, */ if (has_glyph && !tindex) { - buffer->next_glyph (); + (void) buffer->next_glyph (); s_len++; } if (unlikely (!buffer->successful)) @@ -382,17 +382,15 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, if (has_glyph) { - /* We didn't decompose the S, so just advance past it. */ + /* We didn't decompose the S, so just advance past it and fall through. */ end = start + 1; - buffer->next_glyph (); - continue; } } /* Didn't find a recognizable syllable, so we leave end <= start; * this will prevent tone-mark reordering happening. */ - buffer->next_glyph (); + (void) buffer->next_glyph (); } buffer->swap_buffers (); } diff --git a/src/hb-ot-shape-complex-syllabic.cc b/src/hb-ot-shape-complex-syllabic.cc index 843fed690..46509abee 100644 --- a/src/hb-ot-shape-complex-syllabic.cc +++ b/src/hb-ot-shape-complex-syllabic.cc @@ -85,13 +85,13 @@ hb_syllabic_insert_dotted_circles (hb_font_t *font, while (buffer->idx < buffer->len && buffer->successful && last_syllable == buffer->cur().syllable() && buffer->cur().complex_var_u8_category() == (unsigned) repha_category) - buffer->next_glyph (); + (void) buffer->next_glyph (); } (void) buffer->output_info (ginfo); } else - buffer->next_glyph (); + (void) buffer->next_glyph (); } buffer->swap_buffers (); } diff --git a/src/hb-ot-shape-complex-thai.cc b/src/hb-ot-shape-complex-thai.cc index 925e28d3b..4c3068173 100644 --- a/src/hb-ot-shape-complex-thai.cc +++ b/src/hb-ot-shape-complex-thai.cc @@ -323,16 +323,17 @@ preprocess_text_thai (const hb_ot_shape_plan_t *plan, buffer->clear_output (); unsigned int count = buffer->len; - for (buffer->idx = 0; buffer->idx < count && buffer->successful;) + for (buffer->idx = 0; buffer->idx < count /* No need for: && buffer->successful */;) { hb_codepoint_t u = buffer->cur().codepoint; - if (likely (!IS_SARA_AM (u))) { - buffer->next_glyph (); + if (likely (!IS_SARA_AM (u))) + { + if (unlikely (!buffer->next_glyph ())) break; continue; } /* Is SARA AM. Decompose and reorder. */ - if (unlikely (!buffer->output_glyph (NIKHAHIT_FROM_SARA_AM (u)))) break; + (void) buffer->output_glyph (NIKHAHIT_FROM_SARA_AM (u)); _hb_glyph_info_set_continuation (&buffer->prev()); if (unlikely (!buffer->replace_glyph (SARA_AA_FROM_SARA_AM (u)))) break; diff --git a/src/hb-ot-shape-complex-vowel-constraints.cc b/src/hb-ot-shape-complex-vowel-constraints.cc index 2c61e976f..103762699 100644 --- a/src/hb-ot-shape-complex-vowel-constraints.cc +++ b/src/hb-ot-shape-complex-vowel-constraints.cc @@ -31,7 +31,7 @@ static void _output_with_dotted_circle (hb_buffer_t *buffer) { _output_dotted_circle (buffer); - buffer->next_glyph (); + (void) buffer->next_glyph (); } void @@ -96,12 +96,12 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, buffer->idx + 2 < count && 0x0907u == buffer->cur (2).codepoint) { - buffer->next_glyph (); + (void) buffer->next_glyph (); matched = true; } break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -122,7 +122,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, matched = 0x09E2u == buffer->cur (1).codepoint; break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -158,7 +158,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, } break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -182,7 +182,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, matched = 0x0ABEu == buffer->cur (1).codepoint; break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -200,7 +200,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, matched = 0x0B57u == buffer->cur (1).codepoint; break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -214,7 +214,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, { matched = true; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -237,7 +237,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, matched = 0x0C55u == buffer->cur (1).codepoint; break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -255,7 +255,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, matched = 0x0CCCu == buffer->cur (1).codepoint; break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -281,7 +281,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, } break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -316,7 +316,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, } break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -337,7 +337,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, matched = 0x11042u == buffer->cur (1).codepoint; break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -358,7 +358,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, } break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -384,7 +384,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, } break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -404,7 +404,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, } break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; @@ -427,7 +427,7 @@ _hb_preprocess_text_vowel_constraints (const hb_ot_shape_plan_t *plan HB_UNUSED, matched = 0x116B2u == buffer->cur (1).codepoint; break; } - buffer->next_glyph (); + (void) buffer->next_glyph (); if (matched) _output_with_dotted_circle (buffer); } break; diff --git a/src/hb-ot-shape-normalize.cc b/src/hb-ot-shape-normalize.cc index 8ad2d771a..778b5b8bd 100644 --- a/src/hb-ot-shape-normalize.cc +++ b/src/hb-ot-shape-normalize.cc @@ -111,7 +111,7 @@ static inline void next_char (hb_buffer_t *buffer, hb_codepoint_t glyph) { buffer->cur().glyph_index() = glyph; - buffer->next_glyph (); + (void) buffer->next_glyph (); } static inline void @@ -236,24 +236,29 @@ handle_variation_selector_cluster (const hb_ot_shape_normalize_context_t *c, { /* Just pass on the two characters separately, let GSUB do its magic. */ set_glyph (buffer->cur(), font); - buffer->next_glyph (); + (void) buffer->next_glyph (); set_glyph (buffer->cur(), font); - buffer->next_glyph (); + (void) buffer->next_glyph (); } /* Skip any further variation selectors. */ - while (buffer->idx < end && unlikely (buffer->unicode->is_variation_selector (buffer->cur().codepoint))) + while (buffer->idx < end && + buffer->successful && + unlikely (buffer->unicode->is_variation_selector (buffer->cur().codepoint))) { set_glyph (buffer->cur(), font); - buffer->next_glyph (); + (void) buffer->next_glyph (); } - } else { + } + else + { set_glyph (buffer->cur(), font); - buffer->next_glyph (); + (void) buffer->next_glyph (); } } - if (likely (buffer->idx < end)) { + if (likely (buffer->idx < end)) + { set_glyph (buffer->cur(), font); - buffer->next_glyph (); + (void) buffer->next_glyph (); } } @@ -430,8 +435,8 @@ _hb_ot_shape_normalize (const hb_ot_shape_plan_t *plan, buffer->clear_output (); count = buffer->len; unsigned int starter = 0; - buffer->next_glyph (); - while (buffer->idx < count && buffer->successful) + (void) buffer->next_glyph (); + while (buffer->idx < count /* No need for: && buffer->successful */) { hb_codepoint_t composed, glyph; if (/* We don't try to compose a non-mark character with it's preceding starter. @@ -453,9 +458,7 @@ _hb_ot_shape_normalize (const hb_ot_shape_plan_t *plan, font->get_nominal_glyph (composed, &glyph)) { /* Composes. */ - buffer->next_glyph (); /* Copy to out-buffer. */ - if (unlikely (!buffer->successful)) - return; + if (unlikely (!buffer->next_glyph ())) break; /* Copy to out-buffer. */ buffer->merge_out_clusters (starter, buffer->out_len); buffer->out_len--; /* Remove the second composable. */ /* Modify starter and carry on. */ @@ -468,7 +471,7 @@ _hb_ot_shape_normalize (const hb_ot_shape_plan_t *plan, } /* Blocked, or doesn't compose. */ - buffer->next_glyph (); + if (unlikely (!buffer->next_glyph ())) break; if (info_cc (buffer->prev()) == 0) starter = buffer->out_len - 1; From 15f3ef01366c8dc1d6fa18f6c9e072b18f92afd7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 15:36:47 -0600 Subject: [PATCH 21/25] [test] Fix output format --- test/shaping/run-tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/shaping/run-tests.py b/test/shaping/run-tests.py index 6dea5b950..4e9a3de77 100755 --- a/test/shaping/run-tests.py +++ b/test/shaping/run-tests.py @@ -97,7 +97,7 @@ for filename in args: if not reference: print ('%s "%s" %s %s --unicodes %s' % - (hb_shape, fontfile, ' '.join(extra_options), options, unicodes)) + (hb_shape, fontfile, ' '.join(extra_options), ' '.join(options), unicodes)) if "--font-funcs=ft" in options and not have_freetype: skips += 1 From c0b3b9b2584c6dcb5fc37e3b49e26fba718d0e6c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 16:46:13 -0600 Subject: [PATCH 22/25] [ci] Don't test C++2a build One of the bots is unhappy when HB_NODISCARD comes after HB_INTERNAL. No idea why. But, again, we're testing HarfBuzz, not C++, not clang. Ugh. In file included from src/harfbuzz.cc:1: In file included from src/hb-aat-layout.cc:30: In file included from src/hb-aat-layout.hh:32: In file included from src/hb-ot-shape.hh:32: In file included from src/hb-ot-map.hh:32: src/hb-buffer.hh:335:15: error: an attribute list cannot appear here HB_INTERNAL HB_NODISCARD bool move_to (unsigned int i); /* i is output-buffer index. */ ^~~~~~~~~~~~ https://app.circleci.com/pipelines/github/harfbuzz/harfbuzz/1693/workflows/77459205-a189-45d3-bc58-52a8fd952c3f/jobs/155912/parallel-runs/0/steps/0-110?invite=true --- .circleci/config.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d5e7de213..d6bfa38e0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -132,9 +132,7 @@ jobs: # msan, needs --force-fallback-for=glib,freetype2 also which doesn't work yet but runs fuzzer cases at least - run: rm -rf build && meson build --default-library=static -Db_sanitize=memory --buildtype=debugoptimized --wrap-mode=nodownload -Dauto_features=disabled -Dtests=enabled -Dexperimental_api=true - run: ninja -Cbuild -j8 && meson test -Cbuild --print-errorlogs | asan_symbolize | c++filt - # test -std=c++2a and -Weverything of nightly clang builds - - run: clang -c src/harfbuzz.cc src/hb-subset*.cc -DHB_NO_MT -Werror -std=c++2a - - run: clang -c src/hb-*.cc -DHB_NO_MT -Werror -Weverything -Wno-old-style-cast -Wno-documentation -Wno-documentation-unknown-command -Wno-c++98-compat -Wno-cast-qual -Wno-c++98-compat-pedantic -Wno-sign-conversion -Wno-padded -Wno-shorten-64-to-32 -Wno-reserved-id-macro -Wno-float-conversion -Wno-format-pedantic -Wno-shadow -Wno-conversion -Wno-zero-as-null-pointer-constant -Wno-missing-field-initializers -Wno-used-but-marked-unused -Wno-unused-macros -Wno-comma -Wno-float-equal -Wno-disabled-macro-expansion -Wno-weak-vtables -Wno-unused-parameter -Wno-covered-switch-default -Wno-unreachable-code -Wno-unused-template -DHB_WITH_WIN1256 + - run: clang -c src/harfbuzz.cc src/hb-subset*.cc -DHB_NO_MT -Werror -std=c++17 crossbuild-win32: executor: win32-executor From 9582640eedbf73fa95e6877a83e9b70970d9d115 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 16:47:27 -0600 Subject: [PATCH 23/25] [ci] Remove more cruft All were commented out already. --- .circleci/config.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d6bfa38e0..6c1a6b7f3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -117,11 +117,7 @@ jobs: - image: ubuntu:20.04 steps: - checkout - - run: apt update || true; DEBIAN_FRONTEND=noninteractive apt install -y wget gnupg - #- run: wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - - #- run: echo "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main" > /etc/apt/sources.list.d/llvmdev.list - #- run: echo "deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main" > /etc/apt/sources.list.d/llvmdevsrc.list - #- run: apt update || true + - run: apt update || true - run: DEBIAN_FRONTEND=noninteractive apt install -y clang lld git binutils meson pkg-config ragel libfreetype6-dev libglib2.0-dev libcairo2-dev libicu-dev libgraphite2-dev # asan+ubsan - run: rm -rf build && meson build --default-library=static -Db_sanitize=address,undefined --buildtype=debugoptimized --wrap-mode=nodownload -Dexperimental_api=true From 9e397ff2fb1beb08a226f12d68a406a13023f03d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 16:50:54 -0600 Subject: [PATCH 24/25] [buffer] Fix order of HB_INTERNAL HB_DISCARD Sucks that has to be specified in this order. But that is what it is for now. Was only exhibiting problem on C++>=17 since that's when the [[nodiscard]] was introduced. --- src/hb-buffer.hh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 7a754175e..8b432b5f9 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -332,9 +332,9 @@ struct hb_buffer_t /* Internal methods */ - HB_INTERNAL HB_NODISCARD bool move_to (unsigned int i); /* i is output-buffer index. */ + HB_NODISCARD HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */ - HB_INTERNAL HB_NODISCARD bool enlarge (unsigned int size); + HB_NODISCARD HB_INTERNAL bool enlarge (unsigned int size); HB_NODISCARD bool ensure (unsigned int size) { return likely (!size || size < allocated) ? true : enlarge (size); } @@ -375,8 +375,8 @@ struct hb_buffer_t return true; } - HB_INTERNAL HB_NODISCARD bool make_room_for (unsigned int num_in, unsigned int num_out); - HB_INTERNAL HB_NODISCARD bool shift_forward (unsigned int count); + HB_NODISCARD HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out); + HB_NODISCARD HB_INTERNAL bool shift_forward (unsigned int count); typedef long scratch_buffer_t; HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size); From f5356bcb6186d21158275e69fce75f015eac5478 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Mar 2021 16:52:39 -0600 Subject: [PATCH 25/25] [ci] Switch back to c++2a Okay I guess we can be bothered to fix things up for next version of C++. Reverts meat of c0b3b9b2584c6dcb5fc37e3b49e26fba718d0e6c --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6c1a6b7f3..46bb0f81a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -128,7 +128,7 @@ jobs: # msan, needs --force-fallback-for=glib,freetype2 also which doesn't work yet but runs fuzzer cases at least - run: rm -rf build && meson build --default-library=static -Db_sanitize=memory --buildtype=debugoptimized --wrap-mode=nodownload -Dauto_features=disabled -Dtests=enabled -Dexperimental_api=true - run: ninja -Cbuild -j8 && meson test -Cbuild --print-errorlogs | asan_symbolize | c++filt - - run: clang -c src/harfbuzz.cc src/hb-subset*.cc -DHB_NO_MT -Werror -std=c++17 + - run: clang -c src/harfbuzz.cc src/hb-subset*.cc -DHB_NO_MT -Werror -std=c++2a crossbuild-win32: executor: win32-executor