]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
Fix issues discovered by Coverity scan
authorNikola Forró <nforro@redhat.com>
Fri, 23 Nov 2018 15:50:43 +0000 (16:50 +0100)
committerNikola Forró <nforro@redhat.com>
Fri, 23 Nov 2018 15:50:43 +0000 (16:50 +0100)
Signed-off-by: Nikola Forró <nforro@redhat.com>
src/api.c
src/config.c
src/tools/cgsnapshot.c
src/wrapper.c
tests/libcgrouptest01.c
tests/read_stats.c
tests/test_functions.c

index d705d6955da45544e4ac0596b0736e777e9500bf..41daa11a0fca7decb1782a2bdbdb0fa707692191 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -3770,14 +3770,14 @@ static int cg_read_stat(FILE *fp, struct cgroup_stat *cgroup_stat)
                ret = ECGINVAL;
                goto out_free;
        }
-       strncpy(cgroup_stat->name, token, FILENAME_MAX);
+       strncpy(cgroup_stat->name, token, FILENAME_MAX - 1);
 
        token = strtok_r(NULL, " ", &saveptr);
        if (!token) {
                ret = ECGINVAL;
                goto out_free;
        }
-       strncpy(cgroup_stat->value, token, CG_VALUE_MAX);
+       strncpy(cgroup_stat->value, token, CG_VALUE_MAX - 1);
 
 out_free:
        free(line);
@@ -4034,9 +4034,9 @@ int cgroup_get_controller_next(void **handle, struct cgroup_mount_point *info)
                goto out_unlock;
        }
 
-       strncpy(info->name, cg_mount_table[*pos].name, FILENAME_MAX);
+       strncpy(info->name, cg_mount_table[*pos].name, FILENAME_MAX - 1);
 
-       strncpy(info->path, cg_mount_table[*pos].mount.path, FILENAME_MAX);
+       strncpy(info->path, cg_mount_table[*pos].mount.path, FILENAME_MAX - 1);
 
        (*pos)++;
        *handle = pos;
index 5a0b39ba0412f68c7f6ac86de01b723c396c216d..13c7188c86d676f76c2ab06826a9bf4d170ee086 100644 (file)
@@ -171,7 +171,7 @@ int config_insert_cgroup(char *cg_name, int flag)
        }
 
        config_cgroup = &config_table[*table_index];
-       strncpy(config_cgroup->name, cg_name, FILENAME_MAX);
+       strncpy(config_cgroup->name, cg_name, FILENAME_MAX - 1);
 
        /*
         * Since this will be the last part to be parsed.
@@ -1772,7 +1772,7 @@ int cgroup_config_create_template_group(struct cgroup *cgroup,
        int i, j, k;
        struct cgroup *t_cgroup;
        char buffer[FILENAME_MAX];
-       struct cgroup *aux_cgroup;
+       struct cgroup *aux_cgroup = NULL;
        struct cgroup_controller *cgc;
        int found;
 
@@ -1881,5 +1881,6 @@ int cgroup_config_create_template_group(struct cgroup *cgroup,
        }
 
 end:
+       cgroup_free(&aux_cgroup);
        return ret;
 }
index a0599c11f6a8f6b93e119ea882a9aceb8f6ab9cd..66e3bbcac9a89b34fd7029caa3be44570192a498 100644 (file)
@@ -428,6 +428,7 @@ static int display_cgroup_data(struct cgroup *group,
                                goto err;
                        }
                        fprintf(of, "\t\t%s=\"%s\";\n", output_name, value);
+                       free(value);
                }
                fprintf(of, "\t}\n");
        }
index 3a9331fef148f5b66b2282fdc7af8b9c41c136b4..4f238367bfae6b2ad443a57e5bc78f12c726e897 100644 (file)
@@ -48,7 +48,7 @@ struct cgroup *cgroup_new_cgroup(const char *name)
                return NULL;
 
        init_cgroup(cgroup);
-       strncpy(cgroup->name, name, sizeof(cgroup->name));
+       strncpy(cgroup->name, name, sizeof(cgroup->name) - 1);
 
        return cgroup;
 }
@@ -82,7 +82,7 @@ struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup,
        if (!controller)
                return NULL;
 
-       strncpy(controller->name, name, sizeof(controller->name));
+       strncpy(controller->name, name, sizeof(controller->name) - 1);
        controller->cgroup = cgroup;
        controller->index = 0;
 
@@ -603,7 +603,7 @@ struct cgroup *create_cgroup_from_name_value_pairs(const char *name,
                        goto scgroup_err;
                }
 
-               strncpy(con, name_value[i].name, FILENAME_MAX);
+               strncpy(con, name_value[i].name, FILENAME_MAX - 1);
                strtok(con, ".");
 
                /* find out whether we have to add the controller or
index acc229af5d118b36e719520e5d6af0159b047eb0..d7770a09e80050064843bcfafda9e78073666bd2 100644 (file)
@@ -60,11 +60,11 @@ int main(int argc, char *argv[])
        if (fs_mounted) {
                ctl1 = atoi(argv[2]);
                ctl2 = atoi(argv[3]);
-               strncpy(mountpoint, argv[4], sizeof(mountpoint));
+               strncpy(mountpoint, argv[4], sizeof(mountpoint) - 1);
                cgroup_dbg("C:DBG: mountpoint1 as recieved from script=%s\n",
                                                                 mountpoint);
                if (fs_mounted == FS_MULTI_MOUNTED) {
-                       strncpy(mountpoint2, argv[5], sizeof(mountpoint2));
+                       strncpy(mountpoint2, argv[5], sizeof(mountpoint2) - 1);
                        cgroup_dbg("C:DBG: mountpoint2 as recieved from "
                                        "script=%s\n", mountpoint2);
                }
index a1bc24445bd0600b8a33d706009dbd79926603a6..0583039da95e6234c48f2ed1ee51f02bfb7d9e7f 100644 (file)
@@ -63,7 +63,7 @@ int main(int argc, char *argv[])
        }
 
        root_len = strlen(info.full_path) - 1;
-       strncpy(cgroup_path, info.path, FILENAME_MAX);
+       strncpy(cgroup_path, info.path, FILENAME_MAX - 1);
        ret = read_stats(cgroup_path, controller);
        if (ret < 0)
                exit(EXIT_FAILURE);
@@ -72,7 +72,7 @@ int main(int argc, char *argv[])
                        ECGEOF) {
                if (info.type != CGROUP_FILE_TYPE_DIR)
                        continue;
-               strncpy(cgroup_path, info.full_path + root_len, FILENAME_MAX);
+               strncpy(cgroup_path, info.full_path + root_len, FILENAME_MAX - 1);
                strcat(cgroup_path, "/");
                ret = read_stats(cgroup_path, controller);
                if (ret < 0)
index 8f1d59e1d27af6dc0cc170fb3492570859c3e16c..f357ab2f230dfeb3dd46fa91a24fef7082bc85a3 100644 (file)
@@ -139,7 +139,7 @@ struct cgroup *create_new_cgroup_ds(int ctl, const char *grpname,
        char group[FILENAME_MAX];
        char controller_name[FILENAME_MAX], control_file[FILENAME_MAX];
 
-       strncpy(group, grpname, sizeof(group));
+       strncpy(group, grpname, sizeof(group) - 1);
        retval = set_controller(ctl, controller_name, control_file);
        if (retval) {
                fprintf(stderr, "Setting controller failled\n");