chiark / gitweb /
Add _cleanup_globfree_
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 24 Mar 2013 23:09:19 +0000 (19:09 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 5 Apr 2013 23:58:53 +0000 (19:58 -0400)
Fixes a memleak in error path in exec_context_load_environment.

src/core/execute.c
src/shared/util.c
src/shared/util.h
src/tmpfiles/tmpfiles.c

index c51049767d9e25524a7032837b0ce9a20f3f69c9..61369cdc93b158b57e16311af4d3a87fb313a94e 100644 (file)
@@ -1698,7 +1698,7 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
                 int k;
                 bool ignore = false;
                 char **p;
-                glob_t pglob = {};
+                glob_t _cleanup_globfree_ pglob = {};
                 int count, n;
 
                 fn = *i;
@@ -1709,7 +1709,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
                 }
 
                 if (!path_is_absolute(fn)) {
-
                         if (ignore)
                                 continue;
 
@@ -1720,7 +1719,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
                 /* Filename supports globbing, take all matching files */
                 errno = 0;
                 if (glob(fn, 0, NULL, &pglob) != 0) {
-                        globfree(&pglob);
                         if (ignore)
                                 continue;
 
@@ -1729,7 +1727,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
                 }
                 count = pglob.gl_pathc;
                 if (count == 0) {
-                        globfree(&pglob);
                         if (ignore)
                                 continue;
 
@@ -1743,7 +1740,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
                                         continue;
 
                                 strv_free(r);
-                                globfree(&pglob);
                                 return k;
                          }
 
@@ -1755,16 +1751,12 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
                                 m = strv_env_merge(2, r, p);
                                 strv_free(r);
                                 strv_free(p);
-
-                                if (!m) {
-                                        globfree(&pglob);
+                                if (!m)
                                         return -ENOMEM;
-                                }
 
                                 r = m;
                         }
                 }
-                globfree(&pglob);
         }
 
         *l = r;
index 2f66597de30c71e1ffda578a83a7cf1e98ef7c99..52867a177981bd6206c27ea92b4b65f192595f54 100644 (file)
@@ -4322,7 +4322,7 @@ int in_group(const char *name) {
 }
 
 int glob_exists(const char *path) {
-        glob_t g;
+        glob_t _cleanup_globfree_ g = {};
         int r, k;
 
         assert(path);
@@ -4339,8 +4339,6 @@ int glob_exists(const char *path) {
         else
                 r = errno ? -errno : -EIO;
 
-        globfree(&g);
-
         return r;
 }
 
index 4c4aed583e9afafc159b7da9a4c40eeadb9d1d59..bea43fc4bcb5aba47da812ad4f1147666d5bb104 100644 (file)
@@ -37,8 +37,8 @@
 #include <sys/resource.h>
 #include <stddef.h>
 #include <unistd.h>
-#include <systemd/sd-journal.h>
 
+#include <systemd/sd-journal.h>
 #include "macro.h"
 #include "time-util.h"
 
@@ -539,6 +539,8 @@ static inline void journal_closep(sd_journal **j) {
         sd_journal_close(*j);
 }
 
+#define _cleanup_globfree_ __attribute__((cleanup(globfree)))
+
 _malloc_  static inline void *malloc_multiply(size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
                 return NULL;
index 51827f01afdd77fb23c97b5f8e817c75ae1276ca..5d32bd19758493d51a392776e16f14e165e31264 100644 (file)
@@ -601,12 +601,12 @@ static int recursive_relabel(Item *i, const char *path) {
 
 static int glob_item(Item *i, int (*action)(Item *, const char *)) {
         int r = 0, k;
-        glob_t g = {};
+        glob_t _cleanup_globfree_ g = {};
         char **fn;
 
         errno = 0;
-        if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
-
+        k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
+        if (k != 0)
                 if (k != GLOB_NOMATCH) {
                         if (errno > 0)
                                 errno = EIO;
@@ -614,13 +614,13 @@ static int glob_item(Item *i, int (*action)(Item *, const char *)) {
                         log_error("glob(%s) failed: %m", i->path);
                         return -errno;
                 }
-        }
 
-        STRV_FOREACH(fn, g.gl_pathv)
-                if ((k = action(i, *fn)) < 0)
+        STRV_FOREACH(fn, g.gl_pathv) {
+                k = action(i, *fn);
+                if (k < 0)
                         r = k;
+        }
 
-        globfree(&g);
         return r;
 }