summaryrefslogtreecommitdiff
path: root/src/machine/machinectl.c
diff options
context:
space:
mode:
authorDavid Herrmann <dh.herrmann@gmail.com>2015-06-14 15:08:52 +0200
committerDavid Herrmann <dh.herrmann@gmail.com>2015-06-14 15:08:52 +0200
commite7e55dbdc38f929805ab2407fbd50886043a9e7c (patch)
tree8b159323816ae571765dbbd883f3f0d7a0ba3a4e /src/machine/machinectl.c
parentaa75494ad5cdf7bede947212ad8c8356d78580fa (diff)
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.
Diffstat (limited to 'src/machine/machinectl.c')
-rw-r--r--src/machine/machinectl.c48
1 files changed, 33 insertions, 15 deletions
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index c86c36c2de..719eb10932 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -500,6 +500,18 @@ typedef struct MachineStatusInfo {
unsigned n_netif;
} MachineStatusInfo;
+static void machine_status_info_clear(MachineStatusInfo *info) {
+ if (info) {
+ free(info->name);
+ free(info->class);
+ free(info->service);
+ free(info->unit);
+ free(info->root_directory);
+ free(info->netif);
+ zero(*info);
+ }
+}
+
static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char since2[FORMAT_TIMESTAMP_MAX], *s2;
@@ -636,7 +648,7 @@ static int show_machine_info(const char *verb, sd_bus *bus, const char *path, bo
{}
};
- MachineStatusInfo info = {};
+ _cleanup_(machine_status_info_clear) MachineStatusInfo info = {};
int r;
assert(verb);
@@ -658,13 +670,6 @@ static int show_machine_info(const char *verb, sd_bus *bus, const char *path, bo
print_machine_status_info(bus, &info);
- free(info.name);
- free(info.class);
- free(info.service);
- free(info.unit);
- free(info.root_directory);
- free(info.netif);
-
return r;
}
@@ -753,6 +758,15 @@ typedef struct ImageStatusInfo {
uint64_t limit_exclusive;
} ImageStatusInfo;
+static void image_status_info_clear(ImageStatusInfo *info) {
+ if (info) {
+ free(info->name);
+ free(info->path);
+ free(info->type);
+ zero(*info);
+ }
+}
+
static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
char ts_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char ts_absolute[FORMAT_TIMESTAMP_MAX], *s2;
@@ -823,7 +837,7 @@ static int show_image_info(sd_bus *bus, const char *path, bool *new_line) {
{}
};
- ImageStatusInfo info = {};
+ _cleanup_(image_status_info_clear) ImageStatusInfo info = {};
int r;
assert(bus);
@@ -844,10 +858,6 @@ static int show_image_info(sd_bus *bus, const char *path, bool *new_line) {
print_image_status_info(bus, &info);
- free(info.name);
- free(info.path);
- free(info.type);
-
return r;
}
@@ -857,6 +867,15 @@ typedef struct PoolStatusInfo {
uint64_t limit;
} PoolStatusInfo;
+static void pool_status_info_clear(PoolStatusInfo *info) {
+ if (info) {
+ free(info->path);
+ zero(*info);
+ info->usage = -1;
+ info->limit = -1;
+ }
+}
+
static void print_pool_status_info(sd_bus *bus, PoolStatusInfo *i) {
char bs[FORMAT_BYTES_MAX], *s;
@@ -881,7 +900,7 @@ static int show_pool_info(sd_bus *bus) {
{}
};
- PoolStatusInfo info = {
+ _cleanup_(pool_status_info_clear) PoolStatusInfo info = {
.usage = (uint64_t) -1,
.limit = (uint64_t) -1,
};
@@ -899,7 +918,6 @@ static int show_pool_info(sd_bus *bus) {
print_pool_status_info(bus, &info);
- free(info.path);
return 0;
}