]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgsnapshot: Fix compiler truncation warning
authorTom Hromatka <tom.hromatka@oracle.com>
Thu, 20 Jul 2023 14:24:16 +0000 (19:54 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 25 Jul 2023 18:13:28 +0000 (12:13 -0600)
Fix the following warning when running make distcheck

../../../../src/tools/cgsnapshot.c: In function ‘is_ctlr_on_list’:
../../../../src/tools/cgsnapshot.c:532:57: warning: ‘%s’ directive output may be truncated writing up to 409599 bytes into a region of size 4096 [-Wformat-truncation=]
532 |                 snprintf(controllers[i], FILENAME_MAX, "%s", tmp_controllers[i]);
    |                                                         ^~

Suggested-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/tools/cgsnapshot.c

index ff5cc108f2cfe32942c92b8b2aa05245309dc94b..58de4fa26758c1f6630711a31c21aecfa5ffe46c 100644 (file)
@@ -529,7 +529,12 @@ static int is_ctlr_on_list(char controllers[CG_CONTROLLER_MAX][FILENAME_MAX],
 
        /* Lets reset the controllers to intersection of controller ∩ wanted_conts */
        for (i = 0; tmp_controllers[i][0] != '\0'; i++) {
-               snprintf(controllers[i], FILENAME_MAX, "%s", tmp_controllers[i]);
+               /*
+                * gcc complains about truncation when using snprintf() and
+                * and coverity complains about truncation when using strncpy().
+                * Avoid both these warnings by directly invoking memcpy()
+                */
+               memcpy(controllers[i], tmp_controllers[i], sizeof(controllers[i]));
                ret = 1;
        }
        (controllers[i])[0] = '\0';