]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hashmap: check if identical hash_ops is specified if already allocated
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 7 May 2025 17:06:28 +0000 (02:06 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 7 May 2025 20:58:33 +0000 (05:58 +0900)
src/basic/hashmap.c
src/test/test-hashmap-plain.c
src/test/test-set.c

index db55705fdbeeee778e0cb315eb39f779a0c29edc..3366d8f60b3c0a3b7fd850a8c18691608fec678a 100644 (file)
@@ -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)
index fd70978037d78aff88e8a62292de6b85df5078cc..28ddff643bd7823be82334c2773cfe4b417956e5 100644 (file)
@@ -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) {
index f2e2e946292d4023a8be3d99f8075d0c29d65cdf..16351676d45115583dba863f3e5473c3c00cb5c2 100644 (file)
@@ -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) {