chiark / gitweb /
Fix a few resource leaks in error paths
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 15 Dec 2013 21:25:04 +0000 (16:25 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 15 Dec 2013 22:49:28 +0000 (17:49 -0500)
https://bugzilla.redhat.com/show_bug.cgi?id=1043304

src/libsystemd-bus/bus-objects.c
src/sleep/sleep.c
src/udev/net/link-config.c

index 8c81ea664149633ea48379b6a81d9ab1fc2baea8..5aa83a44262c4952156b1c254b3e13dde5e20a06 100644 (file)
@@ -1338,7 +1338,8 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) {
 static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
         struct node *n, *parent;
         const char *e;
-        char *s, *p;
+        _cleanup_free_ char *s = NULL;
+        char *p;
         int r;
 
         assert(bus);
@@ -1366,10 +1367,8 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
                 p = strndupa(path, MAX(1, path - e));
 
                 parent = bus_node_allocate(bus, p);
-                if (!parent) {
-                        free(s);
+                if (!parent)
                         return NULL;
-                }
         }
 
         n = new0(struct node, 1);
@@ -1378,10 +1377,11 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
 
         n->parent = parent;
         n->path = s;
+        s = NULL; /* do not free */
 
         r = hashmap_put(bus->nodes, s, n);
         if (r < 0) {
-                free(s);
+                free(n->path);
                 free(n);
                 return NULL;
         }
index b6a6f60d03452fb9680a156717938e70d7018039..bf673549f1a835114fd4b9e096a7c330c5f93de2 100644 (file)
@@ -57,15 +57,14 @@ static int write_mode(char **modes) {
         return r;
 }
 
-static int write_state(FILE *f0, char **states) {
-        FILE _cleanup_fclose_ *f = f0;
+static int write_state(FILE **f, char **states) {
         char **state;
         int r = 0;
 
         STRV_FOREACH(state, states) {
                 int k;
 
-                k = write_string_to_file(f, *state);
+                k = write_string_to_file(*f, *state);
                 if (k == 0)
                         return 0;
                 log_debug("Failed to write '%s' to /sys/power/state: %s",
@@ -73,9 +72,9 @@ static int write_state(FILE *f0, char **states) {
                 if (r == 0)
                         r = k;
 
-                fclose(f);
-                f = fopen("/sys/power/state", "we");
-                if (!f) {
+                fclose(*f);
+                *f = fopen("/sys/power/state", "we");
+                if (!*f) {
                         log_error("Failed to open /sys/power/state: %m");
                         return -errno;
                 }
@@ -87,7 +86,7 @@ static int write_state(FILE *f0, char **states) {
 static int execute(char **modes, char **states) {
         char* arguments[4];
         int r;
-        FILE *f;
+        _cleanup_fclose_ FILE *f = NULL;
         const char* note = strappenda("SLEEP=", arg_verb);
 
         /* This file is opened first, so that if we hit an error,
@@ -115,7 +114,7 @@ static int execute(char **modes, char **states) {
                    note,
                    NULL);
 
-        r = write_state(f, states);
+        r = write_state(&f, states);
         if (r < 0)
                 return r;
 
index f25afa60c791e8bc356675a7ceb2d773bf56edcc..1a9d780a67bca80b16f4362774001efaef67a307 100644 (file)
@@ -149,7 +149,7 @@ void link_config_ctx_free(link_config_ctx *ctx) {
 
 static int load_link(link_config_ctx *ctx, const char *filename) {
         link_config *link;
-        FILE *file;
+        _cleanup_fclose_ FILE *file;
         int r;
 
         file = fopen(filename, "re");