Skip to content

Commit

Permalink
cgroup: don't use fread in read_cgroup_prop()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
avagin committed Oct 31, 2014
1 parent 5cf6e9b commit 2eba18d
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 2eba18d

Please sign in to comment.