Message ID | tencent_630BF3724BC5EA157B341EB1C7604EE83705@qq.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | [bpf-next] selftests/bpf: Fix strncpy() fortify warning | expand |
On 11/1/22 3:23 PM, Rong Tao wrote: > From: Rong Tao <rongtao@cestc.cn> > > 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)); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > Signed-off-by: Rong Tao <rongtao@cestc.cn> Nope, BPF CI fails once again with: https://github.com/kernel-patches/bpf/actions/runs/3370230622/jobs/5590876906 [...] TEST-OBJ [test_progs-no_alu32] connect_ping.test.o TEST-OBJ [test_progs-no_alu32] map_kptr.test.o In file included from /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/perf_branches.c:7: /tmp/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf_internal.h:200:20: error: redefinition of 'libbpf_strlcpy' static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz) ^ /tmp/work/bpf/bpf/tools/testing/selftests/bpf/bpf_util.h:29:20: note: previous definition is here static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz) ^ 1 error generated. TEST-OBJ [test_progs-no_alu32] hash_large_key.test.o make: *** [Makefile:539: /tmp/work/bpf/bpf/tools/testing/selftests/bpf/no_alu32/perf_branches.test.o] Error 1 make: *** Waiting for unfinished jobs.... make: Leaving directory '/tmp/work/bpf/bpf/tools/testing/selftests/bpf' Error: Process completed with exit code 2. Please do not send broken stuff that was not even compile tested. Here's a small howto for running BPF selftests: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/README.rst#n48
diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h index a3352a64c067..bf78212ff6e9 100644 --- a/tools/testing/selftests/bpf/bpf_util.h +++ b/tools/testing/selftests/bpf/bpf_util.h @@ -20,6 +20,25 @@ static inline unsigned int bpf_num_possible_cpus(void) return possible_cpus; } +/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst + * is zero-terminated string no matter what (unless sz == 0, in which case + * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs + * in what is returned. Given this is internal helper, it's trivial to extend + * this, when necessary. Use this instead of strncpy inside libbpf source code. + */ +static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz) +{ + size_t i; + + if (sz == 0) + return; + + sz--; + for (i = 0; i < sz && src[i]; i++) + dst[i] = src[i]; + dst[i] = '\0'; +} + #define __bpf_percpu_val_align __attribute__((__aligned__(8))) #define BPF_DECLARE_PERCPU(type, name) \ diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index e914cc45b766..e33b70e509da 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -13,6 +13,7 @@ #include <ftw.h> #include "cgroup_helpers.h" +#include "bpf_util.h" /* * To avoid relying on the system setup, when setup_cgroup_env is called @@ -77,7 +78,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers enable[len] = 0; close(fd); } else { - strncpy(enable, controllers, sizeof(enable)); + libbpf_strlcpy(enable, controllers, sizeof(enable)); } snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path); diff --git a/tools/testing/selftests/bpf/xsk.c b/tools/testing/selftests/bpf/xsk.c index 0b3ff49c740d..cf6e9ab37b1b 100644 --- a/tools/testing/selftests/bpf/xsk.c +++ b/tools/testing/selftests/bpf/xsk.c @@ -33,6 +33,7 @@ #include <bpf/bpf.h> #include <bpf/libbpf.h> #include "xsk.h" +#include "bpf_util.h" #ifndef SOL_XDP #define SOL_XDP 283 @@ -521,25 +522,6 @@ static int xsk_create_bpf_link(struct xsk_socket *xsk) return 0; } -/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst - * is zero-terminated string no matter what (unless sz == 0, in which case - * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs - * in what is returned. Given this is internal helper, it's trivial to extend - * this, when necessary. Use this instead of strncpy inside libbpf source code. - */ -static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz) -{ - size_t i; - - if (sz == 0) - return; - - sz--; - for (i = 0; i < sz && src[i]; i++) - dst[i] = src[i]; - dst[i] = '\0'; -} - static int xsk_get_max_queues(struct xsk_socket *xsk) { struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };