From: Tom Hromatka Date: Thu, 20 Jul 2023 14:24:16 +0000 (+0530) Subject: cgsnapshot: Fix compiler truncation warning X-Git-Tag: v3.1.0~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=edf59d999e38bbc9f54a51ae74b2a66a404c0c39;p=thirdparty%2Flibcgroup.git cgsnapshot: Fix compiler truncation warning 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 Signed-off-by: Tom Hromatka --- diff --git a/src/tools/cgsnapshot.c b/src/tools/cgsnapshot.c index ff5cc108..58de4fa2 100644 --- a/src/tools/cgsnapshot.c +++ b/src/tools/cgsnapshot.c @@ -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';