From a7f9759ef8ce354ab2ad420d4a8c5b4260d491fc Mon Sep 17 00:00:00 2001 From: vegbir Date: Thu, 27 Jul 2023 07:24:18 +0000 Subject: [PATCH 05/15] fix concurrency problem Signed-off-by: vegbir --- src/cgroups/cgroup_utils.c | 21 +++++++++++++-------- src/cgroups/cgroup_utils.h | 2 +- src/proc_fuse.c | 3 +++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/cgroups/cgroup_utils.c b/src/cgroups/cgroup_utils.c index 83e4cbf..1224971 100644 --- a/src/cgroups/cgroup_utils.c +++ b/src/cgroups/cgroup_utils.c @@ -512,23 +512,28 @@ static char *copy_to_eol(char *p) return sret; } -static void batch_realloc(char **mem, size_t oldlen, size_t newlen) +char *batch_realloc(char *mem, size_t oldlen, size_t newlen) { int newbatches = (newlen / BATCH_SIZE) + 1; int oldbatches = (oldlen / BATCH_SIZE) + 1; - if (!*mem || newbatches > oldbatches) { - *mem = must_realloc(*mem, newbatches * BATCH_SIZE); + if (!mem || newbatches > oldbatches) { + char *tmp; + tmp = must_realloc(mem, newbatches * BATCH_SIZE); + return tmp; } + return mem; } -void append_line(char **dest, size_t oldlen, char *new, size_t newlen) +char *append_line(char *dest, size_t oldlen, char *new, size_t newlen) { + char *tmp; size_t full = oldlen + newlen; - batch_realloc(dest, oldlen, full + 1); + tmp = batch_realloc(dest, oldlen, full + 1); - memcpy(*dest + oldlen, new, newlen + 1); + memcpy(tmp + oldlen, new, newlen + 1); + return tmp; } static inline void drop_trailing_newlines(char *s) @@ -552,7 +557,7 @@ char *read_file(const char *fnam) if (!f) return NULL; while ((linelen = getline(&line, &len, f)) != -1) { - append_line(&buf, fulllen, line, linelen); + buf = append_line(buf, fulllen, line, linelen); fulllen += linelen; } return buf; @@ -682,7 +687,7 @@ char *readat_file(int dirfd, const char *path) move_fd(fd); while ((linelen = getline(&line, &len, f)) != -1) { - append_line(&buf, fulllen, line, linelen); + buf = append_line(buf, fulllen, line, linelen); fulllen += linelen; } diff --git a/src/cgroups/cgroup_utils.h b/src/cgroups/cgroup_utils.h index f431686..d4c8598 100644 --- a/src/cgroups/cgroup_utils.h +++ b/src/cgroups/cgroup_utils.h @@ -70,7 +70,7 @@ extern size_t strlcat(char *d, const char *s, size_t n); #endif extern FILE *fopen_cloexec(const char *path, const char *mode); -extern void append_line(char **dest, size_t oldlen, char *new, size_t newlen); +extern char *append_line(char *dest, size_t oldlen, char *new, size_t newlen); extern char *read_file(const char *fnam); extern char *readat_file(int fd, const char *path); extern char *read_file_strip_newline(const char *fnam); diff --git a/src/proc_fuse.c b/src/proc_fuse.c index ed70ea7..0af559f 100644 --- a/src/proc_fuse.c +++ b/src/proc_fuse.c @@ -1747,6 +1747,9 @@ __lxcfs_fuse_ops int proc_read(const char *path, char *buf, size_t size, { struct file_info *f = INTTYPE_TO_PTR(fi->fh); + if (!f->buf) + return -EINVAL; + switch (f->type) { case LXC_TYPE_PROC_MEMINFO: if (liblxcfs_functional()) -- 2.41.0