From: Serge Hallyn Date: Wed, 9 Mar 2016 07:04:46 +0000 (-0800) Subject: cgfsng: fix real bug and fake libc realloc bug X-Git-Tag: lxc-2.0.0.rc9~1^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b4ffcca8e83ec6b70bfe5d72da6f0dd930113679;p=thirdparty%2Flxc.git cgfsng: fix real bug and fake libc realloc bug read_file was using the wrong value for the string length. Also, realloc on i386 is wonky with small sizes - so use a batch size to avoid small reallocs. Signed-off-by: Serge Hallyn --- diff --git a/src/lxc/cgfsng.c b/src/lxc/cgfsng.c index 913070e3d..820cd0bd4 100644 --- a/src/lxc/cgfsng.c +++ b/src/lxc/cgfsng.c @@ -621,13 +621,24 @@ static char *get_current_cgroup(char *basecginfo, char *controller) } } +#define BATCH_SIZE 50 +static void batch_realloc(char **mem, size_t oldlen, size_t newlen) +{ + int newbatches = (newlen / BATCH_SIZE) + 1; + int oldbatches = (oldlen / BATCH_SIZE) + 1; + + if (!*mem || newbatches > oldbatches) { + *mem = must_realloc(*mem, newbatches * BATCH_SIZE); + } +} + static void append_line(char **dest, size_t oldlen, char *new, size_t newlen) { size_t full = oldlen + newlen; - *dest = must_realloc(*dest, full + 1); + batch_realloc(dest, oldlen, full + 1); - strcat(*dest, new); + memcpy(*dest + oldlen, new, newlen + 1); } /* Slurp in a whole file */ @@ -636,13 +647,14 @@ static char *read_file(char *fnam) FILE *f; char *line = NULL, *buf = NULL; size_t len = 0, fulllen = 0; + int linelen; f = fopen(fnam, "r"); if (!f) return NULL; - while (getline(&line, &len, f) != -1) { - append_line(&buf, fulllen, line, len); - fulllen += len; + while ((linelen = getline(&line, &len, f)) != -1) { + append_line(&buf, fulllen, line, linelen); + fulllen += linelen; } fclose(f); free(line);