]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
cgfsng: fix real bug and fake libc realloc bug
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Wed, 9 Mar 2016 07:04:46 +0000 (23:04 -0800)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Wed, 9 Mar 2016 07:18:11 +0000 (23:18 -0800)
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 <serge.hallyn@ubuntu.com>
src/lxc/cgfsng.c

index 913070e3d0e6b8db1a66359954eaf026dd08b47f..820cd0bd4c61a77d6b1b3774ad37f36298c5d6aa 100644 (file)
@@ -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);