From d22726cf8a76167acd63a9af7233b798a1e484f2 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 9 Aug 2012 14:29:57 +0200 Subject: utils: don't leak after failed GF_REALLOC This is primarily to avoid a leak upon failed GF_REALLOC when glusterd_readin_file reads a file that is too large. Also, remove unnecessary memset-0 of PATH_MAX+256-byte buffer. Change-Id: Id06bd5faef024e1d865f6f0f56bfbb837c9c6168 BUG: 846755 Signed-off-by: Jim Meyering Reviewed-on: http://review.gluster.com/3804 Tested-by: Gluster Build System Reviewed-by: Anand Avati --- xlators/mgmt/glusterd/src/glusterd-utils.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c index e10de75c6..f7d91d77a 100644 --- a/xlators/mgmt/glusterd/src/glusterd-utils.c +++ b/xlators/mgmt/glusterd/src/glusterd-utils.c @@ -1421,6 +1421,16 @@ out: return ret; } +/* Free LINE[0..N-1] and then the LINE buffer. */ +static void +free_lines (char **line, size_t n) +{ + size_t i; + for (i = 0; i < n; i++) + GF_FREE (line[i]); + GF_FREE (line); +} + char ** glusterd_readin_file (const char *filepath, int *line_count) { @@ -1430,6 +1440,7 @@ glusterd_readin_file (const char *filepath, int *line_count) char buffer[PATH_MAX + 256] = {0}; char **lines = NULL; FILE *fp = NULL; + void *p; fp = fopen (filepath, "r"); if (!fp) @@ -1443,19 +1454,27 @@ glusterd_readin_file (const char *filepath, int *line_count) if (counter == n-1) { n *= 2; - lines = GF_REALLOC (lines, n * sizeof (char *)); - if (!lines) + p = GF_REALLOC (lines, n * sizeof (char *)); + if (!p) { + free_lines (lines, n/2); + lines = NULL; goto out; + } + lines = p; } lines[counter] = gf_strdup (buffer); - memset (buffer, 0, sizeof (buffer)); } lines[counter] = NULL; - lines = GF_REALLOC (lines, (counter + 1) * sizeof (char *)); - if (!lines) + /* Reduce allocation to minimal size. */ + p = GF_REALLOC (lines, (counter + 1) * sizeof (char *)); + if (!p) { + free_lines (lines, counter); + lines = NULL; goto out; + } + lines = p; *line_count = counter; ret = 0; -- cgit