From 6793ae4d06e6c6087639dfd7ac6cd62eac9b0273 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 5 Aug 2016 13:17:48 +0200 Subject: Fix memory leaks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/servers/kernel.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/servers/kernel.c b/src/servers/kernel.c index 85f9eab..de64e04 100644 --- a/src/servers/kernel.c +++ b/src/servers/kernel.c @@ -90,6 +90,7 @@ static char* get_pathname(const char* restrict suffix) goto fail; sprintf(rc, "%s/.coopgammad/~%s/%i%s%s%s", rundir, username, site.method, name ? "." : "", name ? name : "", suffix); + free(name); return rc; fail: @@ -240,9 +241,9 @@ static int is_pidfile_reusable(const char* restrict pidpath, const char* restric */ int create_pidfile(char* pidpath) { - int fd, r, saved_errno; + int fd = -1, r, saved_errno; char* p; - char* restrict token; + char* restrict token = NULL; /* Create token used to validate the service. */ token = malloc(sizeof("COOPGAMMAD_PIDFILE_TOKEN=") + strlen(pidpath)); @@ -250,7 +251,7 @@ int create_pidfile(char* pidpath) return -1; sprintf(token, "COOPGAMMAD_PIDFILE_TOKEN=%s", pidpath); if (putenv(token)) - return -1; + goto fail; /* Create PID file's directory. */ for (p = pidpath; *p == '/'; p++); @@ -259,7 +260,10 @@ int create_pidfile(char* pidpath) *p = '\0'; if (mkdir(pidpath, 0755) < 0) if (errno != EEXIST) - return -1; + { + *p = '/'; + goto fail; + } *p++ = '/'; } @@ -290,14 +294,19 @@ int create_pidfile(char* pidpath) goto fail; /* Done */ + free(token); if (close(fd) < 0) if (errno != EINTR) return -1; return 0; fail: saved_errno = errno; - close(fd); - unlink(pidpath); + free(token); + if (fd >= 0) + { + close(fd); + unlink(pidpath); + } errno = saved_errno; return -1; } -- cgit v1.2.3-70-g09d2