1
0
mirror of https://github.com/systemd/systemd.git synced 2025-02-09 13:57:42 +03:00

Merge pull request #7492 from keszybz/coverity-fixes

A few more cleanups based on coverity and valgrind
This commit is contained in:
Yu Watanabe 2017-11-29 12:50:49 +09:00 committed by GitHub
commit 3235b4e70c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 142 additions and 205 deletions

View File

@ -329,6 +329,29 @@ static inline void *ordered_hashmap_first(OrderedHashmap *h) {
return internal_hashmap_first(HASHMAP_BASE(h)); return internal_hashmap_first(HASHMAP_BASE(h));
} }
#define hashmap_clear_with_destructor(_s, _f) \
({ \
void *_item; \
while ((_item = hashmap_steal_first(_s))) \
_f(_item); \
})
#define hashmap_free_with_destructor(_s, _f) \
({ \
hashmap_clear_with_destructor(_s, _f); \
hashmap_free(_s); \
})
#define ordered_hashmap_clear_with_destructor(_s, _f) \
({ \
void *_item; \
while ((_item = ordered_hashmap_steal_first(_s))) \
_f(_item); \
})
#define ordered_hashmap_free_with_destructor(_s, _f) \
({ \
ordered_hashmap_clear_with_destructor(_s, _f); \
ordered_hashmap_free(_s); \
})
/* no hashmap_next */ /* no hashmap_next */
void *ordered_hashmap_next(OrderedHashmap *h, const void *key); void *ordered_hashmap_next(OrderedHashmap *h, const void *key);

View File

@ -108,6 +108,18 @@ static inline void *set_steal_first(Set *s) {
return internal_hashmap_steal_first(HASHMAP_BASE(s)); return internal_hashmap_steal_first(HASHMAP_BASE(s));
} }
#define set_clear_with_destructor(_s, _f) \
({ \
void *_item; \
while ((_item = set_steal_first(_s))) \
_f(_item); \
})
#define set_free_with_destructor(_s, _f) \
({ \
set_clear_with_destructor(_s, _f); \
set_free(_s); \
})
/* no set_steal_first_key */ /* no set_steal_first_key */
/* no set_first_key */ /* no set_first_key */

View File

@ -692,12 +692,7 @@ static void member_free(Member *m) {
DEFINE_TRIVIAL_CLEANUP_FUNC(Member*, member_free); DEFINE_TRIVIAL_CLEANUP_FUNC(Member*, member_free);
static void member_set_free(Set *s) { static void member_set_free(Set *s) {
Member *m; set_free_with_destructor(s, member_free);
while ((m = set_steal_first(s)))
member_free(m);
set_free(s);
} }
DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, member_set_free); DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, member_set_free);

View File

@ -105,10 +105,7 @@ static void group_free(Group *g) {
} }
static void group_hashmap_clear(Hashmap *h) { static void group_hashmap_clear(Hashmap *h) {
Group *g; hashmap_clear_with_destructor(h, group_free);
while ((g = hashmap_steal_first(h)))
group_free(g);
} }
static void group_hashmap_free(Hashmap *h) { static void group_hashmap_free(Hashmap *h) {

View File

@ -52,16 +52,11 @@ static void vacuum_candidate_free(struct vacuum_candidate *c) {
DEFINE_TRIVIAL_CLEANUP_FUNC(struct vacuum_candidate*, vacuum_candidate_free); DEFINE_TRIVIAL_CLEANUP_FUNC(struct vacuum_candidate*, vacuum_candidate_free);
static void vacuum_candidate_hasmap_free(Hashmap *h) { static void vacuum_candidate_hashmap_free(Hashmap *h) {
struct vacuum_candidate *c; hashmap_free_with_destructor(h, vacuum_candidate_free);
while ((c = hashmap_steal_first(h)))
vacuum_candidate_free(c);
hashmap_free(h);
} }
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, vacuum_candidate_hasmap_free); DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, vacuum_candidate_hashmap_free);
static int uid_from_file_name(const char *filename, uid_t *uid) { static int uid_from_file_name(const char *filename, uid_t *uid) {
const char *p, *e, *u; const char *p, *e, *u;
@ -160,7 +155,7 @@ int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) {
} }
for (;;) { for (;;) {
_cleanup_(vacuum_candidate_hasmap_freep) Hashmap *h = NULL; _cleanup_(vacuum_candidate_hashmap_freep) Hashmap *h = NULL;
struct vacuum_candidate *worst = NULL; struct vacuum_candidate *worst = NULL;
struct dirent *de; struct dirent *de;
uint64_t sum = 0; uint64_t sum = 0;

View File

@ -219,18 +219,12 @@ static int create_disk(
return 0; return 0;
} }
static void free_arg_disks(void) { static void crypt_device_free(crypto_device *d) {
crypto_device *d; free(d->uuid);
free(d->keyfile);
while ((d = hashmap_steal_first(arg_disks))) { free(d->name);
free(d->uuid); free(d->options);
free(d->keyfile); free(d);
free(d->name);
free(d->options);
free(d);
}
hashmap_free(arg_disks);
} }
static crypto_device *get_crypto_device(const char *uuid) { static crypto_device *get_crypto_device(const char *uuid) {
@ -492,7 +486,7 @@ int main(int argc, char *argv[]) {
r = 0; r = 0;
finish: finish:
free_arg_disks(); hashmap_free_with_destructor(arg_disks, crypt_device_free);
free(arg_default_options); free(arg_default_options);
free(arg_default_keyfile); free(arg_default_keyfile);

View File

@ -1043,18 +1043,17 @@ static int remoteserver_init(RemoteServer *s,
return 0; return 0;
} }
static void MHDDaemonWrapper_free(MHDDaemonWrapper *d) {
MHD_stop_daemon(d->daemon);
sd_event_source_unref(d->io_event);
sd_event_source_unref(d->timer_event);
free(d);
}
static void server_destroy(RemoteServer *s) { static void server_destroy(RemoteServer *s) {
size_t i; size_t i;
MHDDaemonWrapper *d;
while ((d = hashmap_steal_first(s->daemons))) { hashmap_free_with_destructor(s->daemons, MHDDaemonWrapper_free);
MHD_stop_daemon(d->daemon);
sd_event_source_unref(d->io_event);
sd_event_source_unref(d->timer_event);
free(d);
}
hashmap_free(s->daemons);
assert(s->sources_size == 0 || s->sources); assert(s->sources_size == 0 || s->sources);
for (i = 0; i < s->sources_size; i++) for (i = 0; i < s->sources_size; i++)

View File

@ -667,7 +667,7 @@ int decompress_stream_lz4(int in, int out, uint64_t max_bytes) {
log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)", log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
total_in, total_out, total_in, total_out,
(double) total_out / total_in * 100); total_in > 0 ? (double) total_out / total_in * 100 : 0.0);
cleanup: cleanup:
munmap(src, st.st_size); munmap(src, st.st_size);
return r; return r;

View File

@ -400,15 +400,6 @@ JournalFile* journal_file_close(JournalFile *f) {
return mfree(f); return mfree(f);
} }
void journal_file_close_set(Set *s) {
JournalFile *f;
assert(s);
while ((f = set_steal_first(s)))
(void) journal_file_close(f);
}
static int journal_file_init_header(JournalFile *f, JournalFile *template) { static int journal_file_init_header(JournalFile *f, JournalFile *template) {
Header h = {}; Header h = {};
ssize_t k; ssize_t k;
@ -3371,8 +3362,7 @@ int journal_file_open(
f->header = h; f->header = h;
if (!newly_created) { if (!newly_created) {
if (deferred_closes) set_clear_with_destructor(deferred_closes, journal_file_close);
journal_file_close_set(deferred_closes);
r = journal_file_verify_header(f); r = journal_file_verify_header(f);
if (r < 0) if (r < 0)

View File

@ -161,7 +161,6 @@ int journal_file_open(
int journal_file_set_offline(JournalFile *f, bool wait); int journal_file_set_offline(JournalFile *f, bool wait);
bool journal_file_is_offlining(JournalFile *f); bool journal_file_is_offlining(JournalFile *f);
JournalFile* journal_file_close(JournalFile *j); JournalFile* journal_file_close(JournalFile *j);
void journal_file_close_set(Set *s);
int journal_file_open_reliably( int journal_file_open_reliably(
const char *fname, const char *fname,

View File

@ -1899,13 +1899,9 @@ void server_maybe_append_tags(Server *s) {
} }
void server_done(Server *s) { void server_done(Server *s) {
JournalFile *f;
assert(s); assert(s);
if (s->deferred_closes) { set_free_with_destructor(s->deferred_closes, journal_file_close);
journal_file_close_set(s->deferred_closes);
set_free(s->deferred_closes);
}
while (s->stdout_streams) while (s->stdout_streams)
stdout_stream_free(s->stdout_streams); stdout_stream_free(s->stdout_streams);
@ -1918,10 +1914,7 @@ void server_done(Server *s) {
if (s->runtime_journal) if (s->runtime_journal)
(void) journal_file_close(s->runtime_journal); (void) journal_file_close(s->runtime_journal);
while ((f = ordered_hashmap_steal_first(s->user_journals))) ordered_hashmap_free_with_destructor(s->user_journals, journal_file_close);
(void) journal_file_close(f);
ordered_hashmap_free(s->user_journals);
sd_event_source_unref(s->syslog_event_source); sd_event_source_unref(s->syslog_event_source);
sd_event_source_unref(s->native_event_source); sd_event_source_unref(s->native_event_source);

View File

@ -1977,18 +1977,13 @@ fail:
_public_ void sd_journal_close(sd_journal *j) { _public_ void sd_journal_close(sd_journal *j) {
Directory *d; Directory *d;
JournalFile *f;
char *p;
if (!j) if (!j)
return; return;
sd_journal_flush_matches(j); sd_journal_flush_matches(j);
while ((f = ordered_hashmap_steal_first(j->files))) ordered_hashmap_free_with_destructor(j->files, journal_file_close);
(void) journal_file_close(f);
ordered_hashmap_free(j->files);
while ((d = hashmap_first(j->directories_by_path))) while ((d = hashmap_first(j->directories_by_path)))
remove_directory(j, d); remove_directory(j, d);
@ -2006,9 +2001,7 @@ _public_ void sd_journal_close(sd_journal *j) {
mmap_cache_unref(j->mmap); mmap_cache_unref(j->mmap);
} }
while ((p = hashmap_steal_first(j->errors))) hashmap_free_free(j->errors);
free(p);
hashmap_free(j->errors);
free(j->path); free(j->path);
free(j->prefix); free(j->prefix);

View File

@ -87,7 +87,6 @@ int sd_dhcp_server_configure_pool(sd_dhcp_server *server, struct in_addr *addres
size = size_max; size = size_max;
if (server->address != address->s_addr || server->netmask != netmask || server->pool_size != size || server->pool_offset != offset) { if (server->address != address->s_addr || server->netmask != netmask || server->pool_size != size || server->pool_offset != offset) {
DHCPLease *lease;
free(server->bound_leases); free(server->bound_leases);
server->bound_leases = new0(DHCPLease*, size); server->bound_leases = new0(DHCPLease*, size);
@ -105,8 +104,7 @@ int sd_dhcp_server_configure_pool(sd_dhcp_server *server, struct in_addr *addres
server->bound_leases[server_off - offset] = &server->invalid_lease; server->bound_leases[server_off - offset] = &server->invalid_lease;
/* Drop any leases associated with the old address range */ /* Drop any leases associated with the old address range */
while ((lease = hashmap_steal_first(server->leases_by_client_id))) hashmap_clear_with_destructor(server->leases_by_client_id, dhcp_lease_free);
dhcp_lease_free(lease);
} }
return 0; return 0;

View File

@ -184,8 +184,6 @@ _public_ sd_bus_track* sd_bus_track_ref(sd_bus_track *track) {
} }
_public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) { _public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) {
struct track_item *i;
if (!track) if (!track)
return NULL; return NULL;
@ -196,14 +194,11 @@ _public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) {
return NULL; return NULL;
} }
while ((i = hashmap_steal_first(track->names)))
track_item_free(i);
if (track->in_list) if (track->in_list)
LIST_REMOVE(tracks, track->bus->tracks, track); LIST_REMOVE(tracks, track->bus->tracks, track);
bus_track_remove_from_queue(track); bus_track_remove_from_queue(track);
hashmap_free(track->names); hashmap_free_with_destructor(track->names, track_item_free);
sd_bus_unref(track->bus); sd_bus_unref(track->bus);
return mfree(track); return mfree(track);
} }
@ -429,8 +424,6 @@ void bus_track_dispatch(sd_bus_track *track) {
} }
void bus_track_close(sd_bus_track *track) { void bus_track_close(sd_bus_track *track) {
struct track_item *i;
assert(track); assert(track);
/* Called whenever our bus connected is closed. If so, and our track object is non-empty, dispatch it /* Called whenever our bus connected is closed. If so, and our track object is non-empty, dispatch it
@ -448,8 +441,7 @@ void bus_track_close(sd_bus_track *track) {
return; return;
/* Let's flush out all names */ /* Let's flush out all names */
while ((i = hashmap_steal_first(track->names))) hashmap_clear_with_destructor(track->names, track_item_free);
track_item_free(i);
/* Invoke handler */ /* Invoke handler */
if (track->handler) if (track->handler)

View File

@ -396,14 +396,11 @@ const sd_bus_vtable image_vtable[] = {
static int image_flush_cache(sd_event_source *s, void *userdata) { static int image_flush_cache(sd_event_source *s, void *userdata) {
Manager *m = userdata; Manager *m = userdata;
Image *i;
assert(s); assert(s);
assert(m); assert(m);
while ((i = hashmap_steal_first(m->image_cache))) hashmap_clear_with_destructor(m->image_cache, image_unref);
image_unref(i);
return 0; return 0;
} }

View File

@ -68,7 +68,6 @@ Manager *manager_new(void) {
void manager_free(Manager *m) { void manager_free(Manager *m) {
Machine *machine; Machine *machine;
Image *i;
assert(m); assert(m);
@ -84,10 +83,7 @@ void manager_free(Manager *m) {
hashmap_free(m->machine_units); hashmap_free(m->machine_units);
hashmap_free(m->machine_leaders); hashmap_free(m->machine_leaders);
while ((i = hashmap_steal_first(m->image_cache))) hashmap_free_with_destructor(m->image_cache, image_unref);
image_unref(i);
hashmap_free(m->image_cache);
sd_event_source_unref(m->image_cache_defer_event); sd_event_source_unref(m->image_cache_defer_event);

View File

@ -1223,7 +1223,6 @@ int manager_new(Manager **ret, sd_event *event) {
} }
void manager_free(Manager *m) { void manager_free(Manager *m) {
RoutingPolicyRule *rule;
Network *network; Network *network;
NetDev *netdev; NetDev *netdev;
Link *link; Link *link;
@ -1253,10 +1252,7 @@ void manager_free(Manager *m) {
set_free(m->rules); set_free(m->rules);
set_free(m->rules_foreign); set_free(m->rules_foreign);
while ((rule = set_steal_first(m->rules_saved))) set_free_with_destructor(m->rules_saved, routing_policy_rule_free);
free(rule);
set_free(m->rules_saved);
sd_netlink_unref(m->rtnl); sd_netlink_unref(m->rtnl);
sd_event_unref(m->event); sd_event_unref(m->event);

View File

@ -34,7 +34,7 @@ static void test_rule_serialization(const char *title, const char *ruleset, cons
const char *cmd; const char *cmd;
int fd, fd2, fd3; int fd, fd2, fd3;
_cleanup_fclose_ FILE *f = NULL, *f2 = NULL, *f3 = NULL; _cleanup_fclose_ FILE *f = NULL, *f2 = NULL, *f3 = NULL;
_cleanup_set_free_free_ Set *rules = NULL; Set *rules = NULL;
_cleanup_free_ char *buf = NULL; _cleanup_free_ char *buf = NULL;
size_t buf_size; size_t buf_size;
@ -64,6 +64,8 @@ static void test_rule_serialization(const char *title, const char *ruleset, cons
cmd = strjoina("diff -u ", pattern3, " ", pattern2); cmd = strjoina("diff -u ", pattern3, " ", pattern2);
log_info("$ %s", cmd); log_info("$ %s", cmd);
assert_se(system(cmd) == 0); assert_se(system(cmd) == 0);
set_free_with_destructor(rules, routing_policy_rule_free);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {

View File

@ -103,8 +103,6 @@ static void dns_scope_abort_transactions(DnsScope *s) {
} }
DnsScope* dns_scope_free(DnsScope *s) { DnsScope* dns_scope_free(DnsScope *s) {
DnsResourceRecord *rr;
if (!s) if (!s)
return NULL; return NULL;
@ -119,10 +117,7 @@ DnsScope* dns_scope_free(DnsScope *s) {
hashmap_free(s->transactions_by_key); hashmap_free(s->transactions_by_key);
while ((rr = ordered_hashmap_steal_first(s->conflict_queue))) ordered_hashmap_free_with_destructor(s->conflict_queue, dns_resource_record_unref);
dns_resource_record_unref(rr);
ordered_hashmap_free(s->conflict_queue);
sd_event_source_unref(s->conflict_event_source); sd_event_source_unref(s->conflict_event_source);
sd_event_source_unref(s->announce_event_source); sd_event_source_unref(s->announce_event_source);

View File

@ -542,19 +542,10 @@ int dns_trust_anchor_load(DnsTrustAnchor *d) {
} }
void dns_trust_anchor_flush(DnsTrustAnchor *d) { void dns_trust_anchor_flush(DnsTrustAnchor *d) {
DnsAnswer *a;
DnsResourceRecord *rr;
assert(d); assert(d);
while ((a = hashmap_steal_first(d->positive_by_key))) d->positive_by_key = hashmap_free_with_destructor(d->positive_by_key, dns_answer_unref);
dns_answer_unref(a); d->revoked_by_rr = set_free_with_destructor(d->revoked_by_rr, dns_resource_record_unref);
d->positive_by_key = hashmap_free(d->positive_by_key);
while ((rr = set_steal_first(d->revoked_by_rr)))
dns_resource_record_unref(rr);
d->revoked_by_rr = set_free(d->revoked_by_rr);
d->negative_by_name = set_free_free(d->negative_by_name); d->negative_by_name = set_free_free(d->negative_by_name);
} }

View File

@ -554,12 +554,7 @@ int bus_verify_polkit_async(
void bus_verify_polkit_async_registry_free(Hashmap *registry) { void bus_verify_polkit_async_registry_free(Hashmap *registry) {
#if ENABLE_POLKIT #if ENABLE_POLKIT
AsyncPolkitQuery *q; hashmap_free_with_destructor(registry, async_polkit_query_free);
while ((q = hashmap_steal_first(registry)))
async_polkit_query_free(q);
hashmap_free(registry);
#endif #endif
} }

View File

@ -992,23 +992,11 @@ static void install_info_free(UnitFileInstallInfo *i) {
free(i); free(i);
} }
static OrderedHashmap* install_info_hashmap_free(OrderedHashmap *m) {
UnitFileInstallInfo *i;
if (!m)
return NULL;
while ((i = ordered_hashmap_steal_first(m)))
install_info_free(i);
return ordered_hashmap_free(m);
}
static void install_context_done(InstallContext *c) { static void install_context_done(InstallContext *c) {
assert(c); assert(c);
c->will_process = install_info_hashmap_free(c->will_process); c->will_process = ordered_hashmap_free_with_destructor(c->will_process, install_info_free);
c->have_processed = install_info_hashmap_free(c->have_processed); c->have_processed = ordered_hashmap_free_with_destructor(c->have_processed, install_info_free);
} }
static UnitFileInstallInfo *install_info_find(InstallContext *c, const char *name) { static UnitFileInstallInfo *install_info_find(InstallContext *c, const char *name) {
@ -3141,12 +3129,7 @@ static void unit_file_list_free_one(UnitFileList *f) {
} }
Hashmap* unit_file_list_free(Hashmap *h) { Hashmap* unit_file_list_free(Hashmap *h) {
UnitFileList *i; return hashmap_free_with_destructor(h, unit_file_list_free_one);
while ((i = hashmap_steal_first(h)))
unit_file_list_free_one(i);
return hashmap_free(h);
} }
DEFINE_TRIVIAL_CLEANUP_FUNC(UnitFileList*, unit_file_list_free_one); DEFINE_TRIVIAL_CLEANUP_FUNC(UnitFileList*, unit_file_list_free_one);

View File

@ -457,15 +457,6 @@ int image_discover(Hashmap *h) {
return 0; return 0;
} }
void image_hashmap_free(Hashmap *map) {
Image *i;
while ((i = hashmap_steal_first(map)))
image_unref(i);
hashmap_free(map);
}
int image_remove(Image *i) { int image_remove(Image *i) {
_cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT; _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
_cleanup_strv_free_ char **settings = NULL; _cleanup_strv_free_ char **settings = NULL;

View File

@ -64,7 +64,9 @@ typedef struct Image {
} Image; } Image;
Image *image_unref(Image *i); Image *image_unref(Image *i);
void image_hashmap_free(Hashmap *map); static inline Hashmap* image_hashmap_free(Hashmap *map) {
return hashmap_free_with_destructor(map, image_unref);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free); DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free);

View File

@ -92,19 +92,10 @@ static void connection_free(Connection *c) {
} }
static void context_free(Context *context) { static void context_free(Context *context) {
sd_event_source *es;
Connection *c;
assert(context); assert(context);
while ((es = set_steal_first(context->listen))) set_free_with_destructor(context->listen, sd_event_source_unref);
sd_event_source_unref(es); set_free_with_destructor(context->connections, connection_free);
while ((c = set_first(context->connections)))
connection_free(c);
set_free(context->listen);
set_free(context->connections);
sd_event_unref(context->event); sd_event_unref(context->event);
sd_resolve_unref(context->resolve); sd_resolve_unref(context->resolve);

View File

@ -699,12 +699,7 @@ static int get_unit_list(
} }
static void message_set_freep(Set **set) { static void message_set_freep(Set **set) {
sd_bus_message *m; set_free_with_destructor(*set, sd_bus_message_unref);
while ((m = set_steal_first(*set)))
sd_bus_message_unref(m);
set_free(*set);
} }
static int get_unit_list_recursive( static int get_unit_list_recursive(

View File

@ -1864,20 +1864,15 @@ int main(int argc, char *argv[]) {
log_error_errno(r, "Failed to write files: %m"); log_error_errno(r, "Failed to write files: %m");
finish: finish:
while ((i = hashmap_steal_first(groups))) hashmap_free_with_destructor(groups, item_free);
item_free(i); hashmap_free_with_destructor(users, item_free);
while ((i = hashmap_steal_first(users)))
item_free(i);
while ((n = hashmap_first_key(members))) { while ((n = hashmap_first_key(members))) {
strv_free(hashmap_steal_first(members)); strv_free(hashmap_steal_first(members));
free(n); free(n);
} }
hashmap_free(groups);
hashmap_free(users);
hashmap_free(members); hashmap_free(members);
hashmap_free(todo_uids); hashmap_free(todo_uids);
hashmap_free(todo_gids); hashmap_free(todo_gids);

View File

@ -95,12 +95,7 @@ static void free_sysvstub(SysvStub *s) {
DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, free_sysvstub); DEFINE_TRIVIAL_CLEANUP_FUNC(SysvStub*, free_sysvstub);
static void free_sysvstub_hashmapp(Hashmap **h) { static void free_sysvstub_hashmapp(Hashmap **h) {
SysvStub *stub; hashmap_free_with_destructor(*h, free_sysvstub);
while ((stub = hashmap_steal_first(*h)))
free_sysvstub(stub);
hashmap_free(*h);
} }
static int add_alias(const char *service, const char *alias) { static int add_alias(const char *service, const char *alias) {

View File

@ -38,6 +38,29 @@ static void test_ordered_hashmap_next(void) {
assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3))); assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3)));
} }
typedef struct Item {
int seen;
} Item;
static void item_seen(Item *item) {
item->seen++;
}
static void test_hashmap_free_with_destructor(void) {
Hashmap *m;
struct Item items[4] = {};
unsigned i;
assert_se(m = hashmap_new(NULL));
for (i = 0; i < ELEMENTSOF(items) - 1; i++)
assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1);
m = hashmap_free_with_destructor(m, item_seen);
assert_se(items[0].seen == 1);
assert_se(items[1].seen == 1);
assert_se(items[2].seen == 1);
assert_se(items[3].seen == 0);
}
static void test_uint64_compare_func(void) { static void test_uint64_compare_func(void) {
const uint64_t a = 0x100, b = 0x101; const uint64_t a = 0x100, b = 0x101;
@ -62,6 +85,7 @@ int main(int argc, const char *argv[]) {
test_ordered_hashmap_funcs(); test_ordered_hashmap_funcs();
test_ordered_hashmap_next(); test_ordered_hashmap_next();
test_hashmap_free_with_destructor();
test_uint64_compare_func(); test_uint64_compare_func();
test_trivial_compare_func(); test_trivial_compare_func();
test_string_compare_func(); test_string_compare_func();

View File

@ -97,10 +97,7 @@ static void test_mnt_id(void) {
assert_se(path_equal_ptr(hashmap_get(h, INT_TO_PTR(mnt_id2)), p)); assert_se(path_equal_ptr(hashmap_get(h, INT_TO_PTR(mnt_id2)), p));
} }
while ((p = hashmap_steal_first(h))) hashmap_free_free(h);
free(p);
hashmap_free(h);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {

View File

@ -40,6 +40,29 @@ static void test_set_steal_first(void) {
assert_se(set_isempty(m)); assert_se(set_isempty(m));
} }
typedef struct Item {
int seen;
} Item;
static void item_seen(Item *item) {
item->seen++;
}
static void test_set_free_with_destructor(void) {
Set *m;
struct Item items[4] = {};
unsigned i;
assert_se(m = set_new(NULL));
for (i = 0; i < ELEMENTSOF(items) - 1; i++)
assert_se(set_put(m, items + i) == 1);
m = set_free_with_destructor(m, item_seen);
assert_se(items[0].seen == 1);
assert_se(items[1].seen == 1);
assert_se(items[2].seen == 1);
assert_se(items[3].seen == 0);
}
static void test_set_put(void) { static void test_set_put(void) {
_cleanup_set_free_ Set *m = NULL; _cleanup_set_free_ Set *m = NULL;
@ -102,6 +125,7 @@ static void test_set_make(void) {
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
test_set_steal_first(); test_set_steal_first();
test_set_free_with_destructor();
test_set_put(); test_set_put();
test_set_make(); test_set_make();

View File

@ -2393,14 +2393,8 @@ int main(int argc, char *argv[]) {
} }
finish: finish:
while ((a = ordered_hashmap_steal_first(items))) ordered_hashmap_free_with_destructor(items, item_array_free);
item_array_free(a); ordered_hashmap_free_with_destructor(globs, item_array_free);
while ((a = ordered_hashmap_steal_first(globs)))
item_array_free(a);
ordered_hashmap_free(items);
ordered_hashmap_free(globs);
free(arg_include_prefixes); free(arg_include_prefixes);
free(arg_exclude_prefixes); free(arg_exclude_prefixes);

View File

@ -135,7 +135,8 @@ static int prepare(char *dir, char *filename)
static int checkout(int fd) static int checkout(int fd)
{ {
int len; int len;
char *buf, *ptr, *word = NULL; _cleanup_free_ char *buf = NULL;
char *ptr, *word = NULL;
struct _mate *him; struct _mate *him;
restart: restart:
@ -155,7 +156,6 @@ static int checkout(int fd)
bufsize = bufsize << 1; bufsize = bufsize << 1;
if (debug) if (debug)
fprintf(stderr, "ID overflow, restarting with size %zu\n", bufsize); fprintf(stderr, "ID overflow, restarting with size %zu\n", bufsize);
free(buf);
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
goto restart; goto restart;
} }
@ -168,13 +168,10 @@ static int checkout(int fd)
if (debug) if (debug)
fprintf(stderr, "Found word %s\n", word); fprintf(stderr, "Found word %s\n", word);
him = malloc(sizeof (struct _mate)); him = malloc(sizeof (struct _mate));
if (!him) { if (!him)
free(buf);
return log_oom(); return log_oom();
}
him->name = strdup(word); him->name = strdup(word);
if (!him->name) { if (!him->name) {
free(buf);
free(him); free(him);
return log_oom(); return log_oom();
} }
@ -188,12 +185,9 @@ static int checkout(int fd)
if (!ptr) if (!ptr)
ptr = word; ptr = word;
if (!ptr)
break;
ptr -= len; ptr -= len;
} }
free(buf);
return 0; return 0;
} }