]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tools/lssubsys: fix strncpy() warning
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Mon, 28 Feb 2022 14:11:43 +0000 (19:41 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 4 Mar 2022 16:07:08 +0000 (09:07 -0700)
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 <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/tools/lssubsys.c

index bf73b9d2d8897fb718ae1a78c418d766366332d0..b86986dac93a982707cd1c7c2bd35baa94ed3061 100644 (file)
@@ -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;