Re: [PATCH bpf-next] selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning

From: Andrii Nakryiko
Date: Thu Nov 03 2022 - 14:46:14 EST


On Wed, Nov 2, 2022 at 5:59 AM Rong Tao <rtoax@xxxxxxxxxxx> wrote:
>
> From: Rong Tao <rongtao@xxxxxxxx>
>
> Move libbpf_strlcpy() to libbpf_common.h, and replace strncpy() with
> libbpf_strlcpy(), fix compile warning.
>
> We can't use libbpf_internal.h directly, because it introduces a lot of
> header dependency issues. So move libbpf_strlcpy() into libbpf_common.h,
> and if you need to use the libbpf_strlcpy() function, you need to include
> the header file libbpf.h

well, no, we should make libbpf_strlcpy as a public API, it's internal
helper. libbpf_common.h is part of libbpf's UAPI.

So don't touch libbpf, please. Name the function as _strlcpy or
something like that, put it into bpf_util.h and use that from
selftests.

>
> 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@xxxxxxxx>
> ---
> tools/lib/bpf/libbpf_common.h | 19 +++++++++++++++++++
> tools/lib/bpf/libbpf_internal.h | 19 -------------------
> tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++-
> tools/testing/selftests/bpf/xsk.c | 19 -------------------
> 4 files changed, 21 insertions(+), 39 deletions(-)
>

[...]