From e7e55dbdc38f929805ab2407fbd50886043a9e7c Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Sun, 14 Jun 2015 15:08:52 +0200 Subject: tree-wide: fix memory leaks in users of bus_map_all_properties() If you use bus_map_all_properties(), you must be aware that it might touch output variables even though it may fail. This is, because we parse many different bus-properties and cannot tell how to clean them up, in case we fail deep down in the parser. Fix all callers of bus_map_all_properties() to correctly cleanup any context structures at all times. --- src/systemctl/systemctl.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/systemctl') diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 5075e4e176..a2eb435fce 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -1678,17 +1678,23 @@ static const struct bus_properties_map machine_info_property_map[] = { {} }; +static void machine_info_clear(struct machine_info *info) { + if (info) { + free(info->name); + free(info->state); + free(info->control_group); + zero(*info); + } +} + static void free_machines_list(struct machine_info *machine_infos, int n) { int i; if (!machine_infos) return; - for (i = 0; i < n; i++) { - free(machine_infos[i].name); - free(machine_infos[i].state); - free(machine_infos[i].control_group); - } + for (i = 0; i < n; i++) + machine_info_clear(&machine_infos[i]); free(machine_infos); } @@ -4402,7 +4408,7 @@ static int show_all( static int show_system_status(sd_bus *bus) { char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; _cleanup_free_ char *hn = NULL; - struct machine_info mi = {}; + _cleanup_(machine_info_clear) struct machine_info mi = {}; const char *on, *off; int r; @@ -4449,9 +4455,6 @@ static int show_system_status(sd_bus *bus) { show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, false, get_output_flags()); } - free(mi.state); - free(mi.control_group); - return 0; } -- cgit v1.2.3-54-g00ecf