]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
samples/read_stats: terminate cgrp_path buffers
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Sat, 31 Jan 2026 06:49:40 +0000 (12:19 +0530)
committerKamalesh Babulal <kamalesh.babulal@oracle.com>
Tue, 10 Mar 2026 16:38:04 +0000 (22:08 +0530)
This adds an explicit '\0' after each strncpy() in the sample so the
subsequent strcat calls stay within bounds.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
samples/c/read_stats.c

index 5f8f5be7c9e3ff42728d64d3835be1ee862a78ba..8b14c4a1b564e46cf470e530455b90f70d8d0b51 100644 (file)
@@ -55,7 +55,6 @@ int main(int argc, char *argv[])
        }
 
        ret = cgroup_walk_tree_begin(controller, "/", 0, &handle, &info, &lvl);
-
        if (ret != 0) {
                fprintf(stderr, "Walk failed\n");
                exit(EXIT_FAILURE);
@@ -63,6 +62,8 @@ int main(int argc, char *argv[])
 
        root_len = strlen(info.full_path) - 1;
        strncpy(cgrp_path, info.path, FILENAME_MAX - 1);
+       cgrp_path[FILENAME_MAX - 1] = '\0';
+
        ret = read_stats(cgrp_path, controller);
        if (ret < 0)
                exit(EXIT_FAILURE);
@@ -71,8 +72,11 @@ int main(int argc, char *argv[])
                        ECGEOF) {
                if (info.type != CGROUP_FILE_TYPE_DIR)
                        continue;
+
                strncpy(cgrp_path, info.full_path + root_len, FILENAME_MAX - 1);
+               cgrp_path[FILENAME_MAX - 1] = '\0';
                strcat(cgrp_path, "/");
+
                ret = read_stats(cgrp_path, controller);
                if (ret < 0)
                        exit(EXIT_FAILURE);