hashmap: check if identical hash_ops is specified if already allocated

This commit is contained in:
Yu Watanabe
2025-05-08 02:06:28 +09:00
parent 472379925c
commit bc169c4fa2
3 changed files with 17 additions and 22 deletions

View 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)

View 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) {

View 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) {