From edf59d999e38bbc9f54a51ae74b2a66a404c0c39 Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Thu, 20 Jul 2023 19:54:16 +0530 Subject: [PATCH] cgsnapshot: Fix compiler truncation warning MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/tools/cgsnapshot.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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'; -- 2.47.2