From: Kamalesh Babulal Date: Mon, 28 Feb 2022 14:11:43 +0000 (+0530) Subject: tools/lssubsys: fix strncpy() warning X-Git-Tag: v3.0~190 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b275d89b489234f3dc2eef9784873a7903508a5c;p=thirdparty%2Flibcgroup.git tools/lssubsys: fix strncpy() warning GCC complains about string truncations in print_all_controllers_in_hierarchy: In function 'print_all_controllers_in_hierarchy', inlined from 'cgroup_list_all_controllers' at lssubsys.c:226:9, inlined from 'main' at lssubsys.c:294:8: lssubsys.c:127:25: warning: 'strncpy' output may be truncated copying 4095 bytes from a string of length 4095 [-Wstringop-truncation] 127 | strncpy(cont_name, info.name, FILENAME_MAX-1); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lssubsys.c:131:25: warning: 'strncpy' output may be truncated copying 4095 bytes from a string of length 4095 [-Wstringop-truncation] 131 | strncpy(cont_names, info.name, FILENAME_MAX-1); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ this warning seems to be a little odd when the current code is already explicitly truncating the string. Digging deeper, it looks like the memset(), followed by strncpy() with -O >= 2 triggers this warning on GCC 11. A simple and safe fix would be removing the memset(). As per the standard's strncpy() will fill the destination string with '\0', if the source string length is lesser than that of the destination. This fix, take this approach. Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- diff --git a/src/tools/lssubsys.c b/src/tools/lssubsys.c index bf73b9d2..b86986da 100644 --- a/src/tools/lssubsys.c +++ b/src/tools/lssubsys.c @@ -123,11 +123,9 @@ static int print_all_controllers_in_hierarchy(const char *tname, if (first) { /* the first controller in the hierarchy */ - memset(cont_name, 0, FILENAME_MAX); strncpy(cont_name, info.name, FILENAME_MAX-1); cont_name[sizeof(cont_name) - 1] = '\0'; - memset(cont_names, 0, FILENAME_MAX); strncpy(cont_names, info.name, FILENAME_MAX-1); cont_names[sizeof(cont_names) - 1] = '\0'; first = 0;