]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: fixed some usages of realloc leading to memory leak
authorHubert Verstraete <hubs@users.sourceforge.net>
Tue, 28 Jun 2016 20:44:26 +0000 (22:44 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 29 Jun 2016 08:45:18 +0000 (10:45 +0200)
Changed all the cases where the pointer passed to realloc is overwritten
by the pointer returned by realloc. The new function my_realloc2 has
been used except in function register_name. If register_name fails to
add a new variable because of an "out of memory" error, all the existing
variables remain valid. If we had used my_realloc2, the array of variables
would have been freed.

src/cfgparse.c
src/chunk.c
src/standard.c
src/vars.c

index d9afd843f7f5105b128d82becfbea7b2df3cee45..4e4775a6e553f00b9fd6d090625351b0ba693294 100644 (file)
@@ -1592,10 +1592,10 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
 
                if (logsrv->maxlen > global.max_syslog_len) {
                        global.max_syslog_len = logsrv->maxlen;
-                       logheader = realloc(logheader, global.max_syslog_len + 1);
-                       logheader_rfc5424 = realloc(logheader_rfc5424, global.max_syslog_len + 1);
-                       logline = realloc(logline, global.max_syslog_len + 1);
-                       logline_rfc5424 = realloc(logline_rfc5424, global.max_syslog_len + 1);
+                       logheader = my_realloc2(logheader, global.max_syslog_len + 1);
+                       logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
+                       logline = my_realloc2(logline, global.max_syslog_len + 1);
+                       logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
                }
 
                /* after the length, a format may be specified */
@@ -6078,10 +6078,10 @@ stats_error_parsing:
 
                        if (logsrv->maxlen > global.max_syslog_len) {
                                global.max_syslog_len = logsrv->maxlen;
-                               logheader = realloc(logheader, global.max_syslog_len + 1);
-                               logheader_rfc5424 = realloc(logheader_rfc5424, global.max_syslog_len + 1);
-                               logline = realloc(logline, global.max_syslog_len + 1);
-                               logline_rfc5424 = realloc(logline_rfc5424, global.max_syslog_len + 1);
+                               logheader = my_realloc2(logheader, global.max_syslog_len + 1);
+                               logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1);
+                               logline = my_realloc2(logline, global.max_syslog_len + 1);
+                               logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
                        }
 
                        /* after the length, a format may be specified */
index e2511076ba57381611e965d9345aff6431361df8..7134fead3503620bf3213cc52620798c2f374674 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <common/config.h>
 #include <common/chunk.h>
+#include <common/standard.h>
 
 /* trash chunks used for various conversions */
 static struct chunk *trash_chunk;
@@ -60,8 +61,8 @@ struct chunk *get_trash_chunk(void)
 int alloc_trash_buffers(int bufsize)
 {
        trash_size = bufsize;
-       trash_buf1 = (char *)realloc(trash_buf1, bufsize);
-       trash_buf2 = (char *)realloc(trash_buf2, bufsize);
+       trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize);
+       trash_buf2 = (char *)my_realloc2(trash_buf2, bufsize);
        return trash_buf1 && trash_buf2;
 }
 
index cfed94d24fbe01c830e2f1978925bec042197d6f..c9f68b5448b84ab85ff363231646b0d22e556533 100644 (file)
@@ -3104,7 +3104,7 @@ char *memprintf(char **out, const char *format, ...)
                }
 
                allocated = needed + 1;
-               ret = realloc(ret, allocated);
+               ret = my_realloc2(ret, allocated);
        } while (ret);
 
        if (needed < 0) {
@@ -3252,7 +3252,7 @@ char *env_expand(char *in)
                        val_len = value ? strlen(value) : 0;
                }
 
-               out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1);
+               out = my_realloc2(out, out_len + (txt_end - txt_beg) + val_len + 1);
                if (txt_end > txt_beg) {
                        memcpy(out + out_len, txt_beg, txt_end - txt_beg);
                        out_len += txt_end - txt_beg;
index d79f317b98e25bc1c0557c0174c8da1eaf5433f9..56fade5d9ffab1fb043f31fb17842345f02a81e0 100644 (file)
@@ -151,6 +151,7 @@ void vars_init(struct vars *vars, enum vars_scope scope)
 static char *register_name(const char *name, int len, enum vars_scope *scope, char **err)
 {
        int i;
+       char **var_names2;
        const char *tmp;
 
        /* Check length. */
@@ -191,13 +192,14 @@ static char *register_name(const char *name, int len, enum vars_scope *scope, ch
                if (strncmp(var_names[i], name, len) == 0)
                        return var_names[i];
 
-       /* Store variable name. */
-       var_names_nb++;
-       var_names = realloc(var_names, var_names_nb * sizeof(*var_names));
-       if (!var_names) {
+       /* Store variable name. If realloc fails, var_names remains valid */
+       var_names2 = realloc(var_names, (var_names_nb + 1) * sizeof(*var_names));
+       if (!var_names2) {
                memprintf(err, "out of memory error");
                return NULL;
        }
+       var_names_nb++;
+       var_names = var_names2;
        var_names[var_names_nb - 1] = malloc(len + 1);
        if (!var_names[var_names_nb - 1]) {
                memprintf(err, "out of memory error");