]> git.proxmox.com Git - mirror_lxcfs.git/commitdiff
tree-wide: fix type comparisons
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 2 Sep 2021 15:01:20 +0000 (17:01 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 2 Sep 2021 15:03:19 +0000 (17:03 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/bindings.c
src/cgroup_fuse.c
src/lxcfs.c
src/proc_cpuview.c
src/proc_fuse.c
src/proc_loadavg.c
src/sysfs_fuse.c
src/utils.c

index f6fde1fd5bc582146cc334fc20159d9eeeac48a2..dc4da14281ec69ef2eda8c03b3a9be093356534f 100644 (file)
@@ -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)
index d24b4c7bd614b90f65d63492433d1c1a250a2abe..cfdf77013408dc14b7c8c2e5ecfc9202a21cff5b 100644 (file)
@@ -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;
                }
index 8e73ea29ea86313eee03346e1215d74924257568..172140b84141971cc95df24053b9676041aa56a4 100644 (file)
@@ -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)
index f8af1e24ed2f9f577a826c4160ee58ca00eb1500..29d0eef9c76e82e448575b8eaac72b49392c92f8 100644 (file)
@@ -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;
        }
index cbbcad2d21f6cb3713e0ebc8e56b649c14a29041..e47d5fb6999d390e3174d1cb4508399182cd2f65 100644 (file)
@@ -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;
index 05c0659e32ab2d82de2283a3551f1938cc7b557d..b70437a930239739c29a4659b3fa5079ba329005 100644 (file)
@@ -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);
index 2f20ef21fcb6b3a82d492192e2eb5668052cec20..4525b69e92612ecb9e75df66bafc48c73f677bd4 100644 (file)
@@ -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';
index fc5271b61b880ae7449ae121ae5a3426c0615d30..99271854ebb8ee23e1d592b50bfd4f26fe753334 100644 (file)
@@ -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;