Commit 18936337 authored by Andrey Vagin's avatar Andrey Vagin Committed by Pavel Emelyanov

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 <tycho.andersen@canonical.com>
Signed-off-by: 's avatarAndrey Vagin <avagin@openvz.org>
Acked-by: 's avatarTycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent a19e945f
...@@ -305,31 +305,26 @@ static inline char *strip(char *str) ...@@ -305,31 +305,26 @@ static inline char *strip(char *str)
static int read_cgroup_prop(struct cgroup_prop *property, const char *fullpath) static int read_cgroup_prop(struct cgroup_prop *property, const char *fullpath)
{ {
char buf[1024]; char buf[1024];
FILE *f; int fd, ret;
char *endptr;
f = fopen(fullpath, "r"); fd = open(fullpath, O_RDONLY);
if (!f) { if (fd == -1) {
property->value = NULL; property->value = NULL;
pr_perror("Failed opening %s", fullpath); pr_perror("Failed opening %s", fullpath);
return -1; return -1;
} }
memset(buf, 0, sizeof(buf)); ret = read(fd, buf, sizeof(buf) - 1);
if (fread(buf, sizeof(buf), 1, f) != 1) { if (ret == -1) {
if (!feof(f)) { pr_err("Failed scanning %s\n", fullpath);
pr_err("Failed scanning %s\n", fullpath); close(fd);
fclose(f);
return -1;
}
}
if (fclose(f) != 0) {
pr_err("Failed closing %s\n", fullpath);
return -1; return -1;
} }
close(fd);
buf[ret] = 0;
if (strtoll(buf, &endptr, 10) == LLONG_MAX) if (strtoll(buf, NULL, 10) == LLONG_MAX)
strcpy(buf, "-1"); strcpy(buf, "-1");
property->value = xstrdup(strip(buf)); property->value = xstrdup(strip(buf));
......
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