[CRIU] [PATCH 5/8] cgroup: don't use fread in read_cgroup_prop()
Andrey Vagin
avagin at openvz.org
Fri Oct 31 09:22:36 PDT 2014
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 at canonical.com>
Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
cgroup.c | 27 +++++++++++----------------
1 file changed, 11 insertions(+), 16 deletions(-)
diff --git a/cgroup.c b/cgroup.c
index fb5c8ac..f5da3ed 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));
--
1.9.3
More information about the CRIU
mailing list