From: Wolfgang Bumiller Date: Thu, 7 Jan 2016 11:59:55 +0000 (+0100) Subject: cgfs: improve read_file and append_line X-Git-Tag: lxcfs-0.14~5 X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=336e326af86ce6a67774a8d5e6e72df53f040a4a;p=mirror_lxcfs.git cgfs: improve read_file and append_line getline() returns the length which can be passed to append_line to avoid a strlen() call. Additionally with the length already known memcpy() can be used instead of strcpy(). A +1 to the length will include the terminating null byte as it is included in getline(3)'s output. Signed-off-by: Wolfgang Bumiller Signed-off-by: Serge Hallyn --- diff --git a/cgfs.c b/cgfs.c index 681a478..ec0f630 100644 --- a/cgfs.c +++ b/cgfs.c @@ -90,11 +90,11 @@ static void dorealloc(char **mem, size_t oldlen, size_t newlen) *mem = tmp; } } -static void append_line(char **contents, char *line, size_t *len) +static void append_line(char **contents, size_t *len, char *line, ssize_t linelen) { - size_t newlen = *len + strlen(line); + size_t newlen = *len + linelen; dorealloc(contents, *len, newlen + 1); - strcpy(*contents + *len, line); + memcpy(*contents + *len, line, linelen+1); *len = newlen; } @@ -104,12 +104,13 @@ static char *read_file(const char *from) char *contents = NULL; FILE *f = fopen(from, "r"); size_t len = 0, fulllen = 0; + ssize_t linelen; if (!f) return NULL; - while (getline(&line, &len, f) != -1) { - append_line(&contents, line, &fulllen); + while ((linelen = getline(&line, &len, f)) != -1) { + append_line(&contents, &fulllen, line, linelen); } fclose(f);