From 3cf1e562fe5c2c8176a3eb627ba3a68ae27e5970 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 2 Sep 2021 17:01:20 +0200 Subject: [PATCH] tree-wide: fix type comparisons Signed-off-by: Christian Brauner --- src/bindings.c | 4 ++-- src/cgroup_fuse.c | 30 +++++++++++++++++++----------- src/lxcfs.c | 4 ++-- src/proc_cpuview.c | 22 +++++++++++----------- src/proc_fuse.c | 28 ++++++++++++++-------------- src/proc_loadavg.c | 4 ++-- src/sysfs_fuse.c | 14 ++++++++------ src/utils.c | 10 ++++++---- 8 files changed, 64 insertions(+), 52 deletions(-) diff --git a/src/bindings.c b/src/bindings.c index f6fde1f..dc4da14 100644 --- a/src/bindings.c +++ b/src/bindings.c @@ -899,8 +899,8 @@ static void __attribute__((constructor)) lxcfs_init(void) lxcfs_info("Kernel does not support swap accounting"); lxcfs_info("api_extensions:"); - for (i = 0; i < nr_api_extensions; i++) - lxcfs_info("- %s", api_extensions[i]); + for (size_t nr = 0; nr < nr_api_extensions; nr++) + lxcfs_info("- %s", api_extensions[nr]); root_fd = open("/", O_PATH | O_CLOEXEC); if (root_fd < 0) diff --git a/src/cgroup_fuse.c b/src/cgroup_fuse.c index d24b4c7..cfdf770 100644 --- a/src/cgroup_fuse.c +++ b/src/cgroup_fuse.c @@ -346,7 +346,7 @@ static struct cgfs_files *cgfs_get_key(const char *controller, * pid's namespace. * Returns the mapped id, or -1 on error. */ -static unsigned int convert_id_to_ns(FILE *idfile, unsigned int in_id) +static int convert_id_to_ns(FILE *idfile, unsigned int in_id) { unsigned int nsuid, // base id for a range in the idfile's namespace hostuid, // base id for a range in the caller's namespace @@ -395,12 +395,13 @@ static unsigned int convert_id_to_ns(FILE *idfile, unsigned int in_id) static bool is_privileged_over(pid_t pid, uid_t uid, uid_t victim, bool req_ns_root) { + FILE *f; char fpath[PROCLEN]; int ret; bool answer = false; uid_t nsuid; - if (victim == -1 || uid == -1) + if (victim == (uid_t)-1 || uid == (uid_t)-1) return false; /* @@ -414,7 +415,8 @@ static bool is_privileged_over(pid_t pid, uid_t uid, uid_t victim, bool req_ns_r ret = snprintf(fpath, PROCLEN, "/proc/%d/uid_map", pid); if (ret < 0 || ret >= PROCLEN) return false; - FILE *f = fopen(fpath, "re"); + + f = fopen(fpath, "re"); if (!f) return false; @@ -429,7 +431,7 @@ static bool is_privileged_over(pid_t pid, uid_t uid, uid_t victim, bool req_ns_r * will be sending requests where the vfs has converted */ nsuid = convert_id_to_ns(f, victim); - if (nsuid == -1) + if (nsuid == (uid_t)-1) goto out; answer = true; @@ -1207,7 +1209,7 @@ static void pid_to_ns_wrapper(int sock, pid_t tpid) char v; ret = snprintf(fnam, sizeof(fnam), "/proc/%d/ns/pid", tpid); - if (ret < 0 || ret >= sizeof(fnam)) + if (ret < 0 || (size_t)ret >= sizeof(fnam)) _exit(1); newnsfd = open(fnam, O_RDONLY); if (newnsfd < 0) @@ -1350,7 +1352,8 @@ __lxcfs_fuse_ops int cg_read(const char *path, char *buf, size_t size, struct file_info *f = INTTYPE_TO_PTR(fi->fh); struct cgfs_files *k = NULL; char *data = NULL; - int ret, s; + int ret; + size_t s; bool r; if (!liblxcfs_functional()) @@ -1403,7 +1406,7 @@ __lxcfs_fuse_ops int cg_read(const char *path, char *buf, size_t size, if (s > size) s = size; memcpy(buf, data, s); - if (s > 0 && s < size && data[s-1] != '\n') + if ((s > 0) && (s < size) && (data[s - 1] != '\n')) buf[s++] = '\n'; ret = s; @@ -1544,7 +1547,7 @@ static void pid_from_ns_wrapper(int sock, pid_t tpid) char v; ret = snprintf(fnam, sizeof(fnam), "/proc/%d/ns/pid", tpid); - if (ret < 0 || ret >= sizeof(fnam)) + if (ret < 0 || (size_t)ret >= sizeof(fnam)) _exit(1); newnsfd = open(fnam, O_RDONLY); if (newnsfd < 0) @@ -1636,7 +1639,7 @@ static bool hostuid_to_ns(uid_t uid, pid_t pid, uid_t *answer) *answer = convert_id_to_ns(f, uid); fclose(f); - if (*answer == -1) + if (*answer == (uid_t)-1) return false; return true; } @@ -1753,6 +1756,7 @@ static bool cgfs_set_value(const char *controller, const char *cgroup, __do_free char *path = NULL; int cfd; size_t len; + ssize_t ret; cfd = get_cgroup_fd_handle_named(controller); if (cfd < 0) @@ -1765,7 +1769,11 @@ static bool cgfs_set_value(const char *controller, const char *cgroup, return false; len = strlen(value); - return write_nointr(fd, value, len) == len; + ret = write_nointr(fd, value, len); + if (ret < 0) + return false; + + return (size_t)ret == len; } __lxcfs_fuse_ops int cg_write(const char *path, const char *buf, size_t size, @@ -1861,7 +1869,7 @@ static bool cgfs_iterate_cgroup(const char *controller, const char *cgroup, continue; ret = snprintf(pathname, sizeof(pathname), "%s/%s", path, dirent->d_name); - if (ret < 0 || ret >= sizeof(pathname)) { + if (ret < 0 || (size_t)ret >= sizeof(pathname)) { lxcfs_error("Pathname too long under %s\n", path); continue; } diff --git a/src/lxcfs.c b/src/lxcfs.c index 8e73ea2..172140b 100644 --- a/src/lxcfs.c +++ b/src/lxcfs.c @@ -145,7 +145,7 @@ static void do_reload(void) #else ret = snprintf(lxcfs_lib_path, sizeof(lxcfs_lib_path), "/usr/local/lib/lxcfs/liblxcfs.so"); #endif - if (ret < 0 || ret >= sizeof(lxcfs_lib_path)) + if (ret < 0 || (size_t)ret >= sizeof(lxcfs_lib_path)) log_exit("Failed to create path to open liblxcfs"); dlopen_handle = dlopen(lxcfs_lib_path, RTLD_LAZY); @@ -1116,7 +1116,7 @@ static int set_pidfile(char *pidfile) return log_error(-1, "Error truncating PID file '%s': %m", pidfile); ret = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid()); - if (ret < 0 || ret >= sizeof(buf)) + if (ret < 0 || (size_t)ret >= sizeof(buf)) return log_error(-1, "Failed to convert pid to string %m"); if (write(fd, buf, ret) != ret) diff --git a/src/proc_cpuview.c b/src/proc_cpuview.c index f8af1e2..29d0eef 100644 --- a/src/proc_cpuview.c +++ b/src/proc_cpuview.c @@ -755,7 +755,7 @@ int cpuview_proc_stat(const char *cg, const char *cpuset, total_len = 0; goto out_pthread_mutex_unlock; } - if (l >= buf_size) { + if ((size_t)l >= buf_size) { lxcfs_error("Write to cache was truncated"); total_len = 0; goto out_pthread_mutex_unlock; @@ -786,7 +786,7 @@ int cpuview_proc_stat(const char *cg, const char *cpuset, total_len = 0; goto out_pthread_mutex_unlock; } - if (l >= buf_size) { + if ((size_t)l >= buf_size) { lxcfs_error("Write to cache was truncated"); total_len = 0; goto out_pthread_mutex_unlock; @@ -804,7 +804,7 @@ int cpuview_proc_stat(const char *cg, const char *cpuset, total_len = 0; goto out_pthread_mutex_unlock; } - if (l >= buf_size) { + if ((size_t)l >= buf_size) { lxcfs_error("Write to cache was truncated"); total_len = 0; goto out_pthread_mutex_unlock; @@ -822,7 +822,7 @@ int cpuview_proc_stat(const char *cg, const char *cpuset, total_len = 0; goto out_pthread_mutex_unlock; } - if (l >= buf_size) { + if ((size_t)l >= buf_size) { lxcfs_error("Write to cache was truncated"); total_len = 0; goto out_pthread_mutex_unlock; @@ -876,7 +876,7 @@ int proc_cpuinfo_read(char *buf, size_t size, off_t offset, size_t cache_size = d->buflen; if (offset) { - int left; + size_t left; if (offset > d->size) return -EINVAL; @@ -939,7 +939,7 @@ int proc_cpuinfo_read(char *buf, size_t size, off_t offset, l = snprintf(cache, cache_size, "processor : %d\n", curcpu); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; cache_size -= l; @@ -964,7 +964,7 @@ int proc_cpuinfo_read(char *buf, size_t size, off_t offset, l = snprintf(cache, cache_size, "processor %d:%s", curcpu, p); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; @@ -977,7 +977,7 @@ int proc_cpuinfo_read(char *buf, size_t size, off_t offset, l = snprintf(cache, cache_size, "%s", line); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; @@ -1000,21 +1000,21 @@ int proc_cpuinfo_read(char *buf, size_t size, off_t offset, cache_size = d->buflen; total_len = 0; l = snprintf(cache, cache_size, "vendor_id : IBM/S390\n"); - if (l < 0 || l >= cache_size) + if (l < 0 || (size_t)l >= cache_size) return 0; cache_size -= l; cache += l; total_len += l; l = snprintf(cache, cache_size, "# processors : %d\n", curcpu + 1); - if (l < 0 || l >= cache_size) + if (l < 0 || (size_t)l >= cache_size) return 0; cache_size -= l; cache += l; total_len += l; l = snprintf(cache, cache_size, "%s", origcache); - if (l < 0 || l >= cache_size) + if (l < 0 || (size_t)l >= cache_size) return 0; total_len += l; } diff --git a/src/proc_fuse.c b/src/proc_fuse.c index cbbcad2..e47d5fb 100644 --- a/src/proc_fuse.c +++ b/src/proc_fuse.c @@ -329,7 +329,7 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset, size_t linelen = 0; if (offset) { - int left; + size_t left; if (offset > d->size) return -EINVAL; @@ -426,7 +426,7 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset, d->cached = 1; d->size = (int)total_len; - if (total_len > size) + if ((size_t)total_len > size) total_len = size; memcpy(buf, d->buf, total_len); @@ -500,7 +500,7 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset, int ret; if (offset) { - int left; + size_t left; if (offset > d->size) return -EINVAL; @@ -631,7 +631,7 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset, l = snprintf(cache, cache_size, "%s", lbuf); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; @@ -808,7 +808,7 @@ static int proc_uptime_read(char *buf, size_t size, off_t offset, #endif if (offset) { - int left; + size_t left; if (offset > d->size) return -EINVAL; @@ -840,7 +840,7 @@ static int proc_uptime_read(char *buf, size_t size, off_t offset, d->cached = 1; d->size = total_len; - if (total_len > size) + if ((size_t)total_len > size) total_len = size; memcpy(buf, d->buf, total_len); @@ -873,7 +873,7 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, int cg_cpu_usage_size = 0; if (offset) { - int left; + size_t left; if (offset > d->size) return -EINVAL; @@ -947,7 +947,7 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, l = snprintf(cache, cache_size, "%s", line); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; @@ -984,7 +984,7 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, l = snprintf(cache, cache_size, "cpu%d%s", curcpu, c); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; @@ -1016,7 +1016,7 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, cg_cpu_usage[physcpu].system, new_idle); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; @@ -1160,7 +1160,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset, int ret; if (offset) { - int left; + size_t left; if (offset > d->size) return -EINVAL; @@ -1370,7 +1370,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset, l = snprintf(cache, cache_size, "%s", printme); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; @@ -1402,7 +1402,7 @@ static int proc_slabinfo_read(char *buf, size_t size, off_t offset, pid_t initpid; if (offset) { - int left; + size_t left; if (offset > d->size) return -EINVAL; @@ -1439,7 +1439,7 @@ static int proc_slabinfo_read(char *buf, size_t size, off_t offset, ssize_t l = snprintf(cache, cache_size, "%s", line); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; diff --git a/src/proc_loadavg.c b/src/proc_loadavg.c index 05c0659..b70437a 100644 --- a/src/proc_loadavg.c +++ b/src/proc_loadavg.c @@ -174,7 +174,7 @@ int proc_loadavg_read(char *buf, size_t size, off_t offset, uint64_t a, b, c; if (offset) { - int left; + size_t left; if (offset > d->size) return -EINVAL; @@ -252,7 +252,7 @@ int proc_loadavg_read(char *buf, size_t size, off_t offset, d->size = (int)total_len; d->cached = 1; - if (total_len > size) + if ((size_t)total_len > size) total_len = size; memcpy(buf, d->buf, total_len); diff --git a/src/sysfs_fuse.c b/src/sysfs_fuse.c index 2f20ef2..4525b69 100644 --- a/src/sysfs_fuse.c +++ b/src/sysfs_fuse.c @@ -147,7 +147,7 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size, ssize_t total_len = 0; if (offset) { - int left; + size_t left; if (!d->cached) return 0; @@ -197,7 +197,7 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size, d->size = (int)total_len; d->cached = 1; - if (total_len > size) + if ((size_t)total_len > size) total_len = size; memcpy(buf, d->buf, total_len); @@ -238,7 +238,7 @@ static int filler_sys_devices_system_cpu(const char *path, void *buf, if (!cpumask) return -errno; - for (size_t i = 0; i < max_cpus; i++) { + for (ssize_t i = 0; i < max_cpus; i++) { int ret; char cpu[100]; @@ -246,7 +246,7 @@ static int filler_sys_devices_system_cpu(const char *path, void *buf, continue; ret = snprintf(cpu, sizeof(cpu), "cpu%ld", i); - if (ret < 0 || ret >= sizeof(cpu)) + if (ret < 0 || (size_t)ret >= sizeof(cpu)) continue; if (DIR_FILLER(filler, buf, cpu, NULL, 0) != 0) @@ -531,14 +531,16 @@ __lxcfs_fuse_ops int sys_readdir(const char *path, void *buf, __lxcfs_fuse_ops int sys_readlink(const char *path, char *buf, size_t size) { - int ret = readlink(path, buf, size); + ssize_t ret; if (!liblxcfs_functional()) return -EIO; + ret = readlink(path, buf, size); if (ret < 0) return -errno; - if (ret > size) + + if ((size_t)ret > size) return -1; buf[ret] = '\0'; diff --git a/src/utils.c b/src/utils.c index fc5271b..9927185 100644 --- a/src/utils.c +++ b/src/utils.c @@ -320,10 +320,12 @@ int read_file_fuse(const char *path, char *buf, size_t size, struct file_info *d return 0; while (getline(&line, &linelen, f) != -1) { - ssize_t l = snprintf(cache, cache_size, "%s", line); + ssize_t l; + + l = snprintf(cache, cache_size, "%s", line); if (l < 0) return log_error(0, "Failed to write cache"); - if (l >= cache_size) + if ((size_t)l >= cache_size) return log_error(0, "Write to cache was truncated"); cache += l; @@ -338,7 +340,7 @@ int read_file_fuse(const char *path, char *buf, size_t size, struct file_info *d /* read from off 0 */ memcpy(buf, d->buf, total_len); - if (d->size > total_len) + if (d->size > (int)total_len) d->cached = d->size - total_len; return total_len; @@ -350,7 +352,7 @@ int read_file_fuse_with_offset(const char *path, char *buf, size_t size, if (offset) { ssize_t total_len = 0; char *cache = d->buf; - int left; + size_t left; if (offset > d->size) return -EINVAL; -- 2.39.5