selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning
authorRong Tao <rongtao@cestc.cn>
Fri, 4 Nov 2022 01:27:54 +0000 (09:27 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 4 Nov 2022 22:46:40 +0000 (15:46 -0700)
commitb3c09fdca1135167bd0682128cd51864c5aa5625
tree175a04d369dd2e6d01d043dca7b3f2d9240570a9
parent1baa7e38002111aee62b489ac343960ae75ce2e9
selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning

Copy libbpf_strlcpy() from libbpf_internal.h to bpf_util.h, and rename it
to bpf_strlcpy(), then replace selftests strncpy()/libbpf_strlcpy() with
bpf_strlcpy(), fix compile warning.

The libbpf_internal.h header cannot be used directly here, because
references to cgroup_helpers.c in samples/bpf will generate compilation
errors. We also can't add libbpf_strlcpy() directly to bpf_util.h,
because the definition of libbpf_strlcpy() in libbpf_internal.h is
duplicated. In order not to modify the libbpf code, add a new function
bpf_strlcpy() to selftests bpf_util.h.

How to reproduce this compilation warning:

$ make -C samples/bpf
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/tencent_469D8AF32BD56816A29981BED06E96D22506@qq.com
tools/testing/selftests/bpf/bpf_util.h
tools/testing/selftests/bpf/cgroup_helpers.c
tools/testing/selftests/bpf/xsk.c