diff options
| author | Jim Meyering <meyering@redhat.com> | 2012-08-09 14:29:57 +0200 | 
|---|---|---|
| committer | Anand Avati <avati@redhat.com> | 2012-08-19 09:42:00 -0700 | 
| commit | d22726cf8a76167acd63a9af7233b798a1e484f2 (patch) | |
| tree | 36f908f1a1c14a628fec13ccb4b0ffe0d3c95690 | |
| parent | 602b0193775bc78768390be08488c3b0aa5601fb (diff) | |
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 <meyering@redhat.com>
Reviewed-on: http://review.gluster.com/3804
Tested-by: Gluster Build System <jenkins@build.gluster.com>
Reviewed-by: Anand Avati <avati@redhat.com>
| -rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-utils.c | 29 | 
1 files changed, 24 insertions, 5 deletions
diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c index e10de75c63c..f7d91d77ae7 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;  | 
