Add _cleanup_globfree_

Fixes a memleak in error path in exec_context_load_environment.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-03-24 19:09:19 -04:00
parent b92bea5d2a
commit c84a948831
4 changed files with 14 additions and 22 deletions

View file

@ -1698,7 +1698,7 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
int k; int k;
bool ignore = false; bool ignore = false;
char **p; char **p;
glob_t pglob = {}; glob_t _cleanup_globfree_ pglob = {};
int count, n; int count, n;
fn = *i; fn = *i;
@ -1709,7 +1709,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
} }
if (!path_is_absolute(fn)) { if (!path_is_absolute(fn)) {
if (ignore) if (ignore)
continue; continue;
@ -1720,7 +1719,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
/* Filename supports globbing, take all matching files */ /* Filename supports globbing, take all matching files */
errno = 0; errno = 0;
if (glob(fn, 0, NULL, &pglob) != 0) { if (glob(fn, 0, NULL, &pglob) != 0) {
globfree(&pglob);
if (ignore) if (ignore)
continue; continue;
@ -1729,7 +1727,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
} }
count = pglob.gl_pathc; count = pglob.gl_pathc;
if (count == 0) { if (count == 0) {
globfree(&pglob);
if (ignore) if (ignore)
continue; continue;
@ -1743,7 +1740,6 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
continue; continue;
strv_free(r); strv_free(r);
globfree(&pglob);
return k; return k;
} }
@ -1755,16 +1751,12 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
m = strv_env_merge(2, r, p); m = strv_env_merge(2, r, p);
strv_free(r); strv_free(r);
strv_free(p); strv_free(p);
if (!m)
if (!m) {
globfree(&pglob);
return -ENOMEM; return -ENOMEM;
}
r = m; r = m;
} }
} }
globfree(&pglob);
} }
*l = r; *l = r;

View file

@ -4322,7 +4322,7 @@ int in_group(const char *name) {
} }
int glob_exists(const char *path) { int glob_exists(const char *path) {
glob_t g; glob_t _cleanup_globfree_ g = {};
int r, k; int r, k;
assert(path); assert(path);
@ -4339,8 +4339,6 @@ int glob_exists(const char *path) {
else else
r = errno ? -errno : -EIO; r = errno ? -errno : -EIO;
globfree(&g);
return r; return r;
} }

View file

@ -37,8 +37,8 @@
#include <sys/resource.h> #include <sys/resource.h>
#include <stddef.h> #include <stddef.h>
#include <unistd.h> #include <unistd.h>
#include <systemd/sd-journal.h>
#include <systemd/sd-journal.h>
#include "macro.h" #include "macro.h"
#include "time-util.h" #include "time-util.h"
@ -539,6 +539,8 @@ static inline void journal_closep(sd_journal **j) {
sd_journal_close(*j); sd_journal_close(*j);
} }
#define _cleanup_globfree_ __attribute__((cleanup(globfree)))
_malloc_ static inline void *malloc_multiply(size_t a, size_t b) { _malloc_ static inline void *malloc_multiply(size_t a, size_t b) {
if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
return NULL; return NULL;

View 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 *)) { static int glob_item(Item *i, int (*action)(Item *, const char *)) {
int r = 0, k; int r = 0, k;
glob_t g = {}; glob_t _cleanup_globfree_ g = {};
char **fn; char **fn;
errno = 0; 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 (k != GLOB_NOMATCH) {
if (errno > 0) if (errno > 0)
errno = EIO; 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); log_error("glob(%s) failed: %m", i->path);
return -errno; return -errno;
} }
STRV_FOREACH(fn, g.gl_pathv) {
k = action(i, *fn);
if (k < 0)
r = k;
} }
STRV_FOREACH(fn, g.gl_pathv)
if ((k = action(i, *fn)) < 0)
r = k;
globfree(&g);
return r; return r;
} }