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/locale/localectl.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'src/locale/localectl.c') diff --git a/src/locale/localectl.c b/src/locale/localectl.c index 8c60339e3e..601839d5dc 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -69,14 +69,27 @@ static void polkit_agent_open_if_enabled(void) { typedef struct StatusInfo { char **locale; - const char *vconsole_keymap; - const char *vconsole_keymap_toggle; - const char *x11_layout; - const char *x11_model; - const char *x11_variant; - const char *x11_options; + char *vconsole_keymap; + char *vconsole_keymap_toggle; + char *x11_layout; + char *x11_model; + char *x11_variant; + char *x11_options; } StatusInfo; +static void status_info_clear(StatusInfo *info) { + if (info) { + strv_free(info->locale); + free(info->vconsole_keymap); + free(info->vconsole_keymap_toggle); + free(info->x11_layout); + free(info->x11_model); + free(info->x11_variant); + free(info->x11_options); + zero(*info); + } +} + static void print_overridden_variables(void) { int r; char *variables[_VARIABLE_LC_MAX] = {}; @@ -150,7 +163,7 @@ static void print_status_info(StatusInfo *i) { } static int show_status(sd_bus *bus, char **args, unsigned n) { - StatusInfo info = {}; + _cleanup_(status_info_clear) StatusInfo info = {}; static const struct bus_properties_map map[] = { { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) }, { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) }, @@ -171,16 +184,12 @@ static int show_status(sd_bus *bus, char **args, unsigned n) { "/org/freedesktop/locale1", map, &info); - if (r < 0) { - log_error_errno(r, "Could not get properties: %m"); - goto fail; - } + if (r < 0) + return log_error_errno(r, "Could not get properties: %m"); print_overridden_variables(); print_status_info(&info); -fail: - strv_free(info.locale); return r; } -- cgit v1.2.3-54-g00ecf