Commit b4ffcca8 by Serge Hallyn

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: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com>
parent 5d5c5694
...@@ -621,13 +621,24 @@ static char *get_current_cgroup(char *basecginfo, char *controller) ...@@ -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) static void append_line(char **dest, size_t oldlen, char *new, size_t newlen)
{ {
size_t full = oldlen + 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 */ /* Slurp in a whole file */
...@@ -636,13 +647,14 @@ static char *read_file(char *fnam) ...@@ -636,13 +647,14 @@ static char *read_file(char *fnam)
FILE *f; FILE *f;
char *line = NULL, *buf = NULL; char *line = NULL, *buf = NULL;
size_t len = 0, fulllen = 0; size_t len = 0, fulllen = 0;
int linelen;
f = fopen(fnam, "r"); f = fopen(fnam, "r");
if (!f) if (!f)
return NULL; return NULL;
while (getline(&line, &len, f) != -1) { while ((linelen = getline(&line, &len, f)) != -1) {
append_line(&buf, fulllen, line, len); append_line(&buf, fulllen, line, linelen);
fulllen += len; fulllen += linelen;
} }
fclose(f); fclose(f);
free(line); free(line);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment