mirror of
https://github.com/checkpoint-restore/criu.git
synced 2026-01-23 02:14:37 +00:00
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: Andrey Vagin <avagin@openvz.org> Acked-by: Tycho Andersen <tycho.andersen@canonical.com> Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
This commit is contained in:
parent
a19e945f6e
commit
1893633702
1 changed files with 11 additions and 16 deletions
27
cgroup.c
27
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);
|
||||
|
||||
if (strtoll(buf, &endptr, 10) == LLONG_MAX)
|
||||
buf[ret] = 0;
|
||||
|
||||
if (strtoll(buf, NULL, 10) == LLONG_MAX)
|
||||
strcpy(buf, "-1");
|
||||
|
||||
property->value = xstrdup(strip(buf));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue