mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-21 06:51:01 +01:00
[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 <noreply@anthropic.com>
This commit is contained in:
1 parent
e8f8ee612f
commit
2f8fb68973
3 files changed
+37
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user