From: Yu Watanabe Date: Wed, 7 May 2025 17:06:28 +0000 (+0900) Subject: hashmap: check if identical hash_ops is specified if already allocated X-Git-Tag: v258-rc1~663^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc169c4fa2bfe6a31c98f6f0218cfbbc3554a64f;p=thirdparty%2Fsystemd.git hashmap: check if identical hash_ops is specified if already allocated --- diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index db55705fdbe..3366d8f60b3 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -824,8 +824,10 @@ static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops assert(h); - if (*h) + if (*h) { + assert((*h)->hash_ops == (hash_ops ?: &trivial_hash_ops)); return 0; + } q = hashmap_base_new(hash_ops, type); if (!q) diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index fd70978037d..28ddff643bd 100644 --- a/src/test/test-hashmap-plain.c +++ b/src/test/test-hashmap-plain.c @@ -419,17 +419,10 @@ TEST(hashmap_remove_and_replace) { TEST(hashmap_ensure_allocated) { _cleanup_hashmap_free_ Hashmap *m = NULL; - int r; - r = hashmap_ensure_allocated(&m, &string_hash_ops); - assert_se(r == 1); - - r = hashmap_ensure_allocated(&m, &string_hash_ops); - assert_se(r == 0); - - /* different hash ops shouldn't matter at this point */ - r = hashmap_ensure_allocated(&m, &trivial_hash_ops); - assert_se(r == 0); + ASSERT_OK_POSITIVE(hashmap_ensure_allocated(&m, &string_hash_ops)); + ASSERT_OK_ZERO(hashmap_ensure_allocated(&m, &string_hash_ops)); + ASSERT_SIGNAL(hashmap_ensure_allocated(&m, &trivial_hash_ops), SIGABRT); } TEST(hashmap_foreach_key) { diff --git a/src/test/test-set.c b/src/test/test-set.c index f2e2e946292..16351676d45 100644 --- a/src/test/test-set.c +++ b/src/test/test-set.c @@ -123,10 +123,10 @@ TEST(set_put_strdupv) { TEST(set_ensure_allocated) { _cleanup_set_free_ Set *m = NULL; - assert_se(set_ensure_allocated(&m, &string_hash_ops) == 1); - assert_se(set_ensure_allocated(&m, &string_hash_ops) == 0); - assert_se(set_ensure_allocated(&m, NULL) == 0); - assert_se(set_isempty(m)); + ASSERT_OK_POSITIVE(set_ensure_allocated(&m, &string_hash_ops)); + ASSERT_OK_ZERO(set_ensure_allocated(&m, &string_hash_ops)); + ASSERT_SIGNAL(set_ensure_allocated(&m, NULL), SIGABRT); + ASSERT_TRUE(set_isempty(m)); } TEST(set_copy) { @@ -159,13 +159,13 @@ TEST(set_copy) { TEST(set_ensure_put) { _cleanup_set_free_ Set *m = NULL; - assert_se(set_ensure_put(&m, &string_hash_ops, "a") == 1); - assert_se(set_ensure_put(&m, &string_hash_ops, "a") == 0); - assert_se(set_ensure_put(&m, NULL, "a") == 0); - assert_se(set_ensure_put(&m, &string_hash_ops, "b") == 1); - assert_se(set_ensure_put(&m, &string_hash_ops, "b") == 0); - assert_se(set_ensure_put(&m, &string_hash_ops, "a") == 0); - assert_se(set_size(m) == 2); + ASSERT_OK_POSITIVE(set_ensure_put(&m, &string_hash_ops, "a")); + ASSERT_OK_ZERO(set_ensure_put(&m, &string_hash_ops, "a")); + ASSERT_SIGNAL(set_ensure_put(&m, NULL, "a"), SIGABRT); + ASSERT_OK_POSITIVE(set_ensure_put(&m, &string_hash_ops, "b")); + ASSERT_OK_ZERO(set_ensure_put(&m, &string_hash_ops, "b")); + ASSERT_OK_ZERO(set_ensure_put(&m, &string_hash_ops, "a")); + ASSERT_EQ(set_size(m), 2u); } TEST(set_ensure_consume) {