From 2f8fb68973701b2ef427b22a151abd7fff73eb60 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:34:30 -0600 Subject: [PATCH] [map] Fix use-after-free when updating a map from itself hb_hashmap_t::update() iterates other's items array while inserting each pair into *this with set(). When other is *this and occupancy sits on a growth threshold (5 of 8, 10 of 16, ...), the first set() calls alloc(), which frees the items array the iterator is still walking, so the rest of the loop reads freed memory. The existing in_storage() guard only protects set()'s own key/value references, not this iterator. Merging a map into itself cannot change it, so make update() a no-op in that case with an early &other == this return, mirroring the self-check in hb_vector_t. hb_map_update() is the only caller. Regression tests are added in src/test-map.cc (including the tombstone case at 42 used / 10 live slots) and test/api/test-map.c; both fail under ASan without the fix. Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-7TMM2Z2T. Co-Authored-By: Claude Opus 4.8 --- src/hb-map.hh | 3 +++ src/test-map.cc | 22 ++++++++++++++++++++++ test/api/test-map.c | 12 ++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/hb-map.hh b/src/hb-map.hh index c708b0839..6b73ac29a 100644 --- a/src/hb-map.hh +++ b/src/hb-map.hh @@ -458,6 +458,9 @@ struct hb_hashmap_t void update (const hb_hashmap_t &other) { if (unlikely (!successful)) return; + /* other.iter () walks other's items array, which set () may free + * if it grows the table. Merging into ourselves changes nothing. */ + if (unlikely (&other == this)) return; hb_copy (other, *this); } diff --git a/src/test-map.cc b/src/test-map.cc index 6720e5f5a..e3b14b578 100644 --- a/src/test-map.cc +++ b/src/test-map.cc @@ -331,6 +331,28 @@ main (int argc, char **argv) hb_always_assert (m1[1] == 3); hb_always_assert (m1[2] == 4); } + /* Test update with self, with the table at its growth threshold so + * that set () would reallocate the items being iterated. */ + { + hb_map_t m; + for (unsigned i = 0; i < 5; i++) + m.set (i, i * 2); + + m.update (m); + hb_always_assert (m.get_population () == 5); + for (unsigned i = 0; i < 5; i++) + hb_always_assert (m[i] == i * 2); + + for (unsigned i = 5; i < 42; i++) + m.set (i, i * 2); + for (unsigned i = 0; i < 32; i++) + m.del (i); + + m.update (m); + hb_always_assert (m.get_population () == 10); + for (unsigned i = 32; i < 42; i++) + hb_always_assert (m[i] == i * 2); + } /* Test keys / values */ { hb_map_t m; diff --git a/test/api/test-map.c b/test/api/test-map.c index 1353aefa4..f1949e98a 100644 --- a/test/api/test-map.c +++ b/test/api/test-map.c @@ -102,6 +102,18 @@ test_map_copy_equal_hash_update (void) assert_map_has (copy, 44, 404); assert_map_has (m, 22, 202); + /* Updating a map from itself is a no-op; 5 entries put the table at + * its growth threshold. */ + hb_map_set (copy, 55, 505); + hb_map_set (copy, 66, 606); + hb_map_update (copy, copy); + g_assert_cmpint (hb_map_get_population (copy), ==, 5); + assert_map_has (copy, 11, 101); + assert_map_has (copy, 22, 222); + assert_map_has (copy, 44, 404); + assert_map_has (copy, 55, 505); + assert_map_has (copy, 66, 606); + hb_map_destroy (updated); hb_map_destroy (copy); hb_map_destroy (m);