From 3957e16e6cc3ade2139071b1c50d3f9c5cc929d6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 7 Oct 2026 12:09:14 -0600 Subject: [PATCH] [set] Skip empty pages during reverse iteration (#6332) Deleting the last member of a page leaves an empty page in the sparse set. `hb_bit_page_t::get_max()` returned zero for that page, so reverse iteration could report a value that was never in the set. For example, add and delete 512, then iterate backward from 1024: HarfBuzz reported 512 from an empty set. Return INVALID for an empty page, matching get_min(), so previous() skips it. Cover reverse scalar/range iteration with retained empty pages, including inverted sets. Assisted-by: OpenAI Codex --- src/hb-bit-page.hh | 2 +- test/api/test-set.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/hb-bit-page.hh b/src/hb-bit-page.hh index 8a4ad58cf..fccf678c4 100644 --- a/src/hb-bit-page.hh +++ b/src/hb-bit-page.hh @@ -350,7 +350,7 @@ struct hb_bit_page_t for (int i = len () - 1; i >= 0; i--) if (v[i]) return i * ELT_BITS + elt_get_max (v[i]); - return 0; + return INVALID; } /* diff --git a/test/api/test-set.c b/test/api/test-set.c index 3f29f1d4d..e38a519b3 100644 --- a/test/api/test-set.c +++ b/test/api/test-set.c @@ -1439,11 +1439,49 @@ test_set_next_previous_range (void) hb_set_destroy (s); } +static void +test_set_previous_empty_pages (void) +{ + hb_set_t *s = hb_set_create (); + hb_set_add (s, 512); + hb_set_del (s, 512); + hb_codepoint_t cp = 1024; + g_assert_false (hb_set_previous (s, &cp)); + g_assert_cmpuint (cp, ==, HB_SET_VALUE_INVALID); + + hb_set_add_range (s, 100, 101); + hb_set_add (s, 1536); + hb_set_del (s, 1536); + cp = 2048; + g_assert_true (hb_set_previous (s, &cp)); + g_assert_cmpuint (cp, ==, 101); + g_assert_true (hb_set_previous (s, &cp)); + g_assert_cmpuint (cp, ==, 100); + g_assert_false (hb_set_previous (s, &cp)); + + hb_codepoint_t first = 2048, last = 2048; + g_assert_true (hb_set_previous_range (s, &first, &last)); + g_assert_cmpuint (first, ==, 100); + g_assert_cmpuint (last, ==, 101); + g_assert_false (hb_set_previous_range (s, &first, &last)); + + hb_set_invert (s); + cp = 1537; + g_assert_true (hb_set_previous (s, &cp)); + g_assert_cmpuint (cp, ==, 1536); + first = last = 2048; + g_assert_true (hb_set_previous_range (s, &first, &last)); + g_assert_cmpuint (first, ==, 102); + g_assert_cmpuint (last, ==, 2047); + hb_set_destroy (s); +} + int main (int argc, char **argv) { hb_test_init (&argc, &argv); + hb_test_add (test_set_previous_empty_pages); hb_test_add (test_set_basic); hb_test_add (test_set_next_previous_range); hb_test_add (test_set_subsets);