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

From: Daniel Borkmann
Date: Tue Nov 01 2022 - 10:12:47 EST


On 11/1/22 1:21 PM, Rong Tao wrote:
From: Rong Tao <rongtao@xxxxxxxx>

move libbpf_strlcpy() to bpf_util.h, and replace strncpy() with
libbpf_strlcpy(), fix compile warning.

Compile samples/bpf, warning:
$ cd samples/bpf
$ make
...
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));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


BPF CI now throws a new warning after your patch:

https://github.com/kernel-patches/bpf/actions/runs/3369416153/jobs/5589052613

[...]
xsk.c:536:2: error: call to undeclared function 'libbpf_strlcpy'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
libbpf_strlcpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ);
^
xsk.c:755:2: error: call to undeclared function 'libbpf_strlcpy'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
libbpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
^
xsk.c:942:2: error: call to undeclared function 'libbpf_strlcpy'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
libbpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
^
3 errors generated.
make: *** [Makefile:166: /tmp/work/bpf/bpf/tools/testing/selftests/bpf/xsk.o] Error 1
make: *** Waiting for unfinished jobs....