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>
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;