From 2eba18dfb6cf0ae23298acfd73d03c704e35cbff Mon Sep 17 00:00:00 2001 From: Andrey Vagin Date: Fri, 31 Oct 2014 16:57:42 +0300 Subject: [PATCH] cgroup: don't use fread in read_cgroup_prop() I think this version of code is a bit more readable. It doesn't do memcpy and doesn't allocate FILE. Everyone knows arguments for read(), but only a few of us know arguments for fread(). CID 73345 (#1 of 1): String not null terminated (STRING_NULL) 2. string_null_argument: Function fread does not terminate string *buf. [Note: The source code implementation of the function has been overridden by a builtin model.] Cc: Tycho Andersen --- cgroup.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/cgroup.c b/cgroup.c index fb5c8acac7..f5da3edfae 100644 --- a/cgroup.c +++ b/cgroup.c @@ -305,31 +305,26 @@ static inline char *strip(char *str) static int read_cgroup_prop(struct cgroup_prop *property, const char *fullpath) { char buf[1024]; - FILE *f; - char *endptr; + int fd, ret; - f = fopen(fullpath, "r"); - if (!f) { + fd = open(fullpath, O_RDONLY); + if (fd == -1) { property->value = NULL; pr_perror("Failed opening %s", fullpath); return -1; } - memset(buf, 0, sizeof(buf)); - if (fread(buf, sizeof(buf), 1, f) != 1) { - if (!feof(f)) { - pr_err("Failed scanning %s\n", fullpath); - fclose(f); - return -1; - } - } - - if (fclose(f) != 0) { - pr_err("Failed closing %s\n", fullpath); + ret = read(fd, buf, sizeof(buf) - 1); + if (ret == -1) { + pr_err("Failed scanning %s\n", fullpath); + close(fd); return -1; } + close(fd); + + buf[ret] = 0; - if (strtoll(buf, &endptr, 10) == LLONG_MAX) + if (strtoll(buf, NULL, 10) == LLONG_MAX) strcpy(buf, "-1"); property->value = xstrdup(strip(buf));