From: Tomas Mraz Date: Fri, 11 Mar 2011 17:08:45 +0000 (+0100) Subject: Fix memory leaks in load_user. X-Git-Tag: cronie1.4.7~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6add3dab040370ebe8b689cdec1d0fd3ea0fc42a;p=cronie Fix memory leaks in load_user. --- diff --git a/src/database.c b/src/database.c index 94396b3..d1d97ed 100644 --- a/src/database.c +++ b/src/database.c @@ -146,6 +146,7 @@ process_crontab(const char *uname, const char *fname, const char *tabname, } u = load_user(crontab_fd, pw, uname, fname, tabname); /* read the file */ + crontab_fd = -1; /* load_user takes care of closing the file */ if (u != NULL) { u->mtime = mtime; link_user(new_db, u); diff --git a/src/user.c b/src/user.c index 8551aca..fb9d3b2 100644 --- a/src/user.c +++ b/src/user.c @@ -53,13 +53,14 @@ load_user (int crontab_fd, struct passwd *pw, const char *uname, FILE *file; user *u; entry *e; - int status, save_errno; - char **envp, **tenvp; + int status, save_errno = errno; + char **envp = NULL, **tenvp; if (!(file = fdopen(crontab_fd, "r"))) { int save_errno = errno; log_it(uname, getpid (), "FAILED", "fdopen on crontab_fd in load_user", save_errno); + close(crontab_fd); return (NULL); } @@ -67,26 +68,25 @@ load_user (int crontab_fd, struct passwd *pw, const char *uname, /* file is open. build user entry, then read the crontab file. */ if ((u = (user *) malloc (sizeof (user))) == NULL) - return (NULL); + goto done; + memset(u, 0, sizeof(*u)); if (((u->name = strdup(fname)) == NULL) || ((u->tabname = strdup(tabname)) == NULL)) { save_errno = errno; - free(u); - errno = save_errno; - return (NULL); + free_user(u); + u = NULL; + goto done; } - u->crontab = NULL; /* init environment. this will be copied/augmented for each entry. */ if ((envp = env_init()) == NULL) { save_errno = errno; - free(u->name); - free(u); - errno = save_errno; - return (NULL); + free_user(u); + u = NULL; + goto done; } if (get_security_context(pw == NULL ? NULL : uname, @@ -101,6 +101,7 @@ load_user (int crontab_fd, struct passwd *pw, const char *uname, while ((status = load_env (envstr, file)) >= OK) { switch (status) { case ERR: + save_errno = errno; free_user(u); u = NULL; goto done; @@ -117,7 +118,6 @@ load_user (int crontab_fd, struct passwd *pw, const char *uname, save_errno = errno; free_user(u); u = NULL; - errno = save_errno; goto done; } envp = tenvp; @@ -126,7 +126,10 @@ load_user (int crontab_fd, struct passwd *pw, const char *uname, } done: - env_free(envp); + if (envp) + env_free(envp); fclose(file); - Debug(DPARS, ("...load_user() done\n")) return (u); + Debug(DPARS, ("...load_user() done\n")) + errno = save_errno; + return (u); }