diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index ccb62e7b2a..3366d8f60b 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) @@ -843,7 +845,7 @@ int ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops * return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_ORDERED); } -int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops) { +int set_ensure_allocated(Set **s, const struct hash_ops *hash_ops) { return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET); } @@ -1285,7 +1287,7 @@ int set_put(Set *s, const void *key) { int set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key) { int r; - r = _set_ensure_allocated(s, hash_ops); + r = set_ensure_allocated(s, hash_ops); if (r < 0) return r; @@ -1883,7 +1885,7 @@ int set_put_strndup_full(Set **s, const struct hash_ops *hash_ops, const char *p assert(s); assert(p); - r = _set_ensure_allocated(s, hash_ops); + r = set_ensure_allocated(s, hash_ops); if (r < 0) return r; diff --git a/src/basic/prioq.c b/src/basic/prioq.c index 0ff5faaff3..ed9b33e59d 100644 --- a/src/basic/prioq.c +++ b/src/basic/prioq.c @@ -58,8 +58,10 @@ Prioq* prioq_free(Prioq *q) { int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) { assert(q); - if (*q) + if (*q) { + assert((*q)->compare_func == compare_func); return 0; + } *q = prioq_new(compare_func); if (!*q) @@ -167,7 +169,7 @@ int prioq_put(Prioq *q, void *data, unsigned *idx) { return 0; } -int prioq_ensure_put(Prioq **q, compare_func_t compare_func, void *data, unsigned *idx) { +int _prioq_ensure_put(Prioq **q, compare_func_t compare_func, void *data, unsigned *idx) { int r; r = prioq_ensure_allocated(q, compare_func); diff --git a/src/basic/prioq.h b/src/basic/prioq.h index da06dcd819..204ba54fae 100644 --- a/src/basic/prioq.h +++ b/src/basic/prioq.h @@ -16,7 +16,13 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Prioq*, prioq_free); int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func); int prioq_put(Prioq *q, void *data, unsigned *idx); -int prioq_ensure_put(Prioq **q, compare_func_t compare_func, void *data, unsigned *idx); +int _prioq_ensure_put(Prioq **q, compare_func_t compare_func, void *data, unsigned *idx); +#define prioq_ensure_put(q, compare_func, data, idx) \ + ({ \ + int (*_func_)(const typeof((data)[0])*, const typeof((data)[0])*) = compare_func; \ + _prioq_ensure_put(q, (compare_func_t) _func_, data, idx); \ + }) + int prioq_remove(Prioq *q, void *data, unsigned *idx); void prioq_reshuffle(Prioq *q, void *data, unsigned *idx); diff --git a/src/basic/set.h b/src/basic/set.h index 789bf9b733..9dd7bd403d 100644 --- a/src/basic/set.h +++ b/src/basic/set.h @@ -16,8 +16,7 @@ static inline Set* set_free(Set *s) { #define set_copy(s) ((Set*) _hashmap_copy(HASHMAP_BASE(s))) -int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops); -#define set_ensure_allocated(h, ops) _set_ensure_allocated(h, ops) +int set_ensure_allocated(Set **s, const struct hash_ops *hash_ops); int set_put(Set *s, const void *key); /* no set_update */ @@ -103,7 +102,7 @@ int set_put_strndup_full(Set **s, const struct hash_ops *hash_ops, const char *p #define set_put_strndup(s, p, n) set_put_strndup_full(s, &string_hash_ops_free, p, n) #define set_put_strdup(s, p) set_put_strndup(s, p, SIZE_MAX) -int set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l ); +int set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l); #define set_put_strdupv(s, l) set_put_strdupv_full(s, &string_hash_ops_free, l) int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags flags); diff --git a/src/core/unit.c b/src/core/unit.c index d057b8ddc9..c706caae08 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -4629,7 +4629,7 @@ int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const ch /* Make sure the drop-in dir is registered in our path cache. This way we don't need to stupidly * recreate the cache after every drop-in we write. */ if (u->manager->unit_path_cache) { - r = set_put_strdup(&u->manager->unit_path_cache, p); + r = set_put_strdup_full(&u->manager->unit_path_cache, &path_hash_ops_free, p); if (r < 0) return r; } diff --git a/src/libsystemd-network/lldp-neighbor.c b/src/libsystemd-network/lldp-neighbor.c index 67500895f8..dd2a221403 100644 --- a/src/libsystemd-network/lldp-neighbor.c +++ b/src/libsystemd-network/lldp-neighbor.c @@ -37,9 +37,7 @@ DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR( sd_lldp_neighbor, lldp_neighbor_unlink); -int lldp_neighbor_prioq_compare_func(const void *a, const void *b) { - const sd_lldp_neighbor *x = a, *y = b; - +int lldp_neighbor_prioq_compare_func(const sd_lldp_neighbor *x, const sd_lldp_neighbor *y) { assert(x); assert(y); diff --git a/src/libsystemd-network/lldp-neighbor.h b/src/libsystemd-network/lldp-neighbor.h index 57ac3a3f20..f7945c2edf 100644 --- a/src/libsystemd-network/lldp-neighbor.h +++ b/src/libsystemd-network/lldp-neighbor.h @@ -84,7 +84,7 @@ static inline void* LLDP_NEIGHBOR_TLV_DATA(const sd_lldp_neighbor *n) { extern const struct hash_ops lldp_neighbor_hash_ops; int lldp_neighbor_id_compare_func(const LLDPNeighborID *x, const LLDPNeighborID *y); -int lldp_neighbor_prioq_compare_func(const void *a, const void *b); +int lldp_neighbor_prioq_compare_func(const sd_lldp_neighbor *x, const sd_lldp_neighbor *y); sd_lldp_neighbor *lldp_neighbor_unlink(sd_lldp_neighbor *n); sd_lldp_neighbor *lldp_neighbor_new(size_t raw_size); diff --git a/src/resolve/resolved-link-bus.c b/src/resolve/resolved-link-bus.c index 8b4e492e88..9c5f7a836a 100644 --- a/src/resolve/resolved-link-bus.c +++ b/src/resolve/resolved-link-bus.c @@ -675,10 +675,6 @@ int bus_link_method_set_dnssec_negative_trust_anchors(sd_bus_message *message, v if (r < 0) return r; - ns = set_new(&dns_name_hash_ops_free); - if (!ns) - return -ENOMEM; - r = sd_bus_message_read_strv(message, &ntas); if (r < 0) return r; @@ -691,7 +687,7 @@ int bus_link_method_set_dnssec_negative_trust_anchors(sd_bus_message *message, v return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid negative trust anchor domain: %s", *i); - r = set_put_strdup(&ns, *i); + r = set_put_strdup_full(&ns, &dns_name_hash_ops_free, *i); if (r < 0) return r; diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c index 2a9d606bb4..1e2d95b4ea 100644 --- a/src/resolve/resolved-link.c +++ b/src/resolve/resolved-link.c @@ -477,11 +477,7 @@ static int link_update_dnssec_negative_trust_anchors(Link *l) { if (r < 0) return r; - ns = set_new(&dns_name_hash_ops_free); - if (!ns) - return -ENOMEM; - - r = set_put_strdupv(&ns, ntas); + r = set_put_strdupv_full(&ns, &dns_name_hash_ops_free, ntas); if (r < 0) return r; diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index fd70978037..28ddff643b 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 f2e2e94629..16351676d4 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) {