From e1631d6320cf6b47c3b46f0b56ae970986c9c20c Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 31 Aug 2011 14:44:48 +0200 Subject: [PATCH] properly free memory/close fd upon errors (Thanks xeen) Found with the static analyzer cppcheck --- src/cfgparse.y | 1 + src/ipc.c | 4 +++- src/util.c | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cfgparse.y b/src/cfgparse.y index c0bf92af..5b2b4b5c 100644 --- a/src/cfgparse.y +++ b/src/cfgparse.y @@ -188,6 +188,7 @@ static char *migrate_config(char *input, off_t size) { ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes); if (ret == -1) { warn("Cannot read from pipe"); + FREE(converted); return NULL; } read_bytes += ret; diff --git a/src/ipc.c b/src/ipc.c index dfb1fd68..5f4d59cc 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -59,8 +59,10 @@ static bool mkdirp(const char *path) { copy[strlen(copy)-1] = '\0'; char *sep = strrchr(copy, '/'); - if (sep == NULL) + if (sep == NULL) { + FREE(copy); return false; + } *sep = '\0'; bool result = false; if (mkdirp(copy)) diff --git a/src/util.c b/src/util.c index fc0a5ceb..2d6c3e14 100644 --- a/src/util.c +++ b/src/util.c @@ -374,11 +374,13 @@ char *store_restart_layout() { if (n == -1) { perror("write()"); free(filename); + close(fd); return NULL; } if (n == 0) { printf("write == 0?\n"); free(filename); + close(fd); return NULL; } written += n; -- 2.39.5