From 3b42dc01880d8d31bf472f22c93df16603fde7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Mon, 18 Nov 2024 19:29:48 +0100 Subject: [PATCH] iord-map: detect node relinking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: László Várady --- lib/adt/iord_map.c | 10 ++++++++-- lib/adt/tests/test_iord_map.c | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/adt/iord_map.c b/lib/adt/iord_map.c index 50dc3579a..b09341748 100644 --- a/lib/adt/iord_map.c +++ b/lib/adt/iord_map.c @@ -107,10 +107,13 @@ iord_map_foreach(IOrdMap *self, IOrdMapForeachFunc func, gpointer user_data) gboolean iord_map_insert(IOrdMap *self, gpointer key, gpointer value) { + IOrdMapNode *key_node = iord_map_node_from_container(key, self->key_container_offset); + g_assert(!key_node->next || iv_list_empty(key_node)); + gpointer orig_key, old_value; if (!g_hash_table_lookup_extended(self->ht, key, &orig_key, &old_value)) { - iv_list_add_tail(iord_map_node_from_container(key, self->key_container_offset), &self->keys); + iv_list_add_tail(key_node, &self->keys); goto finish; } @@ -128,10 +131,13 @@ iord_map_insert(IOrdMap *self, gpointer key, gpointer value) gboolean iord_map_prepend(IOrdMap *self, gpointer key, gpointer value) { + IOrdMapNode *key_node = iord_map_node_from_container(key, self->key_container_offset); + g_assert(!key_node->next || iv_list_empty(key_node)); + gpointer orig_key, old_value; if (!g_hash_table_lookup_extended(self->ht, key, &orig_key, &old_value)) { - iv_list_add(iord_map_node_from_container(key, self->key_container_offset), &self->keys); + iv_list_add(key_node, &self->keys); goto finish; } diff --git a/lib/adt/tests/test_iord_map.c b/lib/adt/tests/test_iord_map.c index 8d79f16d0..ac1c2b134 100644 --- a/lib/adt/tests/test_iord_map.c +++ b/lib/adt/tests/test_iord_map.c @@ -89,6 +89,7 @@ Test(iord_map, insert_lookup_and_contains) IOrdMap *map = iord_map_new(g_str_hash, g_str_equal, offsetof(TestKey, n), offsetof(TestData, n)); TestKey key = { "key" }; + TestKey key_same = { "key" }; TestData value = { .a = 1, .b = 2, .c = 3 }; TestData value2 = { .a = 4, .b = 5, .c = 6 }; @@ -97,7 +98,7 @@ Test(iord_map, insert_lookup_and_contains) TestData *actual = iord_map_lookup(map, &key); assert_value_equals(actual, &value); - cr_assert_not(iord_map_insert(map, &key, &value2)); + cr_assert_not(iord_map_insert(map, &key_same, &value2)); actual = iord_map_lookup(map, &key); assert_value_equals(actual, &value2); @@ -194,6 +195,7 @@ Test(iord_map, reinsert_moves_key_to_end) IOrdMap *map = iord_map_new(g_str_hash, g_str_equal, offsetof(TestKey, n), offsetof(TestData, n)); TestKey keys[] = {{"key1"}, {"key2"}, {"key3"}, {"key4"}, {"key5"}}; + TestKey keys_reinsert = {"key1"}; TestData values[] = {{1}, {2}, {3}, {4}, {5}}; TestData new_value = {9}; @@ -202,7 +204,7 @@ Test(iord_map, reinsert_moves_key_to_end) iord_map_insert(map, &keys[i], &values[i]); } - iord_map_insert(map, &keys[0], &new_value); + iord_map_insert(map, &keys_reinsert, &new_value); IOrdMapNode *actual_keys = iord_map_get_keys(map);