selftests/bpf: Add and use strscpy()
Subsystems (4)
Ihor Solodrai
reviewercover selftests/bpf: Add and use strscpy()
This series introduces a simplified implementation of strscpy() to selftests/bpf and then replaces strcpy/strncpy usages across the tests with it.
For context see a discussion that prompted this series [1].
[1] https://lore.kernel.org/bpf/CAADnVQ+9uw2_o388j43EWiAPdMB=3FLx2jq-9zRSvqrv-wgRag@mail.gmail.com/
Ihor Solodrai (7):
selftests/bpf: Add simple strscpy() implementation selftests/bpf: Add strscpy_cat()
selftests/bpf: Replace strcpy() calls with strscpy() selftests/bpf: Replace strncpy() with strscpy() selftests/bpf: Use strscpy_cat() in the test_loader selftests/bpf: Use strscpy in bpftool_helpers.c selftests/bpf: Use memcpy() for bounded non-NULL-terminated copies
tools/testing/selftests/bpf/bpf_util.h | 66 +++++++++++++++---- tools/testing/selftests/bpf/bpftool_helpers.c | 16 +++-- tools/testing/selftests/bpf/cgroup_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.c | 5 +- .../testing/selftests/bpf/prog_tests/align.c | 2 +- .../selftests/bpf/prog_tests/bpf_iter.c | 3 +- .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 2 +- .../selftests/bpf/prog_tests/ctx_rewrite.c | 6 +- .../selftests/bpf/prog_tests/flow_dissector.c | 4 +- .../bpf/prog_tests/queue_stack_map.c | 4 +- .../selftests/bpf/prog_tests/setget_sockopt.c | 2 +- .../bpf/prog_tests/skc_to_unix_sock.c | 2 +- .../selftests/bpf/prog_tests/sockopt_sk.c | 2 +- .../bpf/prog_tests/task_local_data.h | 2 +- .../selftests/bpf/prog_tests/tc_redirect.c | 2 +- .../selftests/bpf/prog_tests/test_veristat.c | 4 +- tools/testing/selftests/bpf/test_loader.c | 8 +-- tools/testing/selftests/bpf/test_progs.c | 2 +- tools/testing/selftests/bpf/test_verifier.c | 2 +- tools/testing/selftests/bpf/xdp_features.c | 3 +- tools/testing/selftests/bpf/xdp_hw_metadata.c | 4 +- 21 files changed, 93 insertions(+), 50 deletions(-)
Signature · 1 line
2.53.0
Ihor Solodrai
reviewerPATCH 1/7 selftests/bpf: Add simple strscpy() implementation
Replace bpf_strlcpy() in bpf_util.h with a sized_strscpy(), which is a simplified sized_strscpy() from the kernel (lib/string.c). It:
* takes a count (destination size) parameter
* guarantees NULL-termination
* returns the number of characters copied or -E2BIG
Re-define strscpy macro to sized_strscpy.
Fixup the single existing bpf_strlcpy() call in cgroup_helpers.c
--- tools/testing/selftests/bpf/bpf_util.h | 38 +++++++++++++------- tools/testing/selftests/bpf/cgroup_helpers.c | 2 +- 2 files changed, 26 insertions(+), 14 deletions(-)
tools/testing/selftests/bpf/bpf_util.h +25 -13
@@ -21,25 +21,37 @@ 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. +/* +* Simplified strscpy() implementation. The kernel one is in lib/string.c */ -static inline void bpf_strlcpy (char *dst,const char *src,size_t sz) +static inline ssize_t sized_strscpy (char *dest,const char *src,size_t count) { -size_t i; +long res =0 ; -if (sz ==0 ) -return ; +if (count ==0 ) +return -E2BIG ; - sz--; -for (i =0 ; i < sz && src[i]; i++) - dst[i] = src[i]; - dst[i] ='\0' ; +while (count >1 ) { +char c; + + c = src[res]; + dest[res] = c; +if (!c) +return res; + res++; + count--; + } + +/* Force NUL-termination. */ + dest[res] ='\0' ; + +/* Return E2BIG if the source didn't stop */ +return src[res] ? -E2BIG : res; } +#undef strscpy/* Redefine the placeholder from tools/include/linux/string.h */ +#define strscpy sized_strscpy +#define __bpf_percpu_val_align __attribute__((__aligned__(8)))#define BPF_DECLARE_PERCPU (type, name) \
tools/testing/selftests/bpf/cgroup_helpers.c +1 -1
@@ -86,7 +86,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers enable[len] =0 ;close (fd); }else { -bpf_strlcpy (enable, controllers,sizeof (enable)); +strscpy (enable, controllers,sizeof (enable)); }snprintf (path,sizeof (path),"%s/cgroup.subtree_control" , cgroup_path);
Signature · 1 line
2.53.0
Ihor Solodrai
reviewerPATCH 2/7 selftests/bpf: Add strscpy_cat()
In reply to Ihor SolodraiAdd sized_strscpy_cat() to bpf_util.h, which concatenates multiple strings into a destination buffer with strscpy().
Add strscpy_cat() macro with varargs.
This is a convenient helper that provides the same guarantees as strscpy(), but for a case when multiple strings need to be concatenated into destination.
--- tools/testing/selftests/bpf/bpf_util.h | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
tools/testing/selftests/bpf/bpf_util.h +28 -0
@@ -52,6 +52,34 @@ static inline ssize_t sized_strscpy(char *dest, const char *src, size_t count)#undef strscpy/* Redefine the placeholder from tools/include/linux/string.h */ #define strscpy sized_strscpy +/* +* strscpy() analogue that concatenates multiple strings into a buffer +*/ +static inline ssize_t sized_strscpy_cat (char *dest,size_t dest_sz, +const char *const *srcs,size_t n) +{ +ssize_t pos =0 ; + +if (dest_sz ==0 ) +return -E2BIG ; + +for (size_t i =0 ; i < n; i++) { +ssize_t res =strscpy (dest + pos, srcs[i], dest_sz - pos); + +if (res <0 ) +return res; + pos += res; + } + +return pos; +} + +#define strscpy_cat (dest, count, ...) \ + sized_strscpy_cat(dest, count, \ + (const char * const[]){ __VA_ARGS__ }, \ + ARRAY_SIZE(((const char * const[]){ __VA_ARGS__ }))) + +#define __bpf_percpu_val_align __attribute__((__aligned__(8)))#define BPF_DECLARE_PERCPU (type, name) \
Signature · 1 line
2.53.0
Ihor Solodrai
reviewerPATCH 3/7 selftests/bpf: Replace strcpy() calls with strscpy()
In reply to Ihor Solodraistrcpy() does not perform bounds checking and is considered deprecated [1]. Replace strcpy() calls with strscpy(), passing size of the destination buffer.
[1] https://docs.kernel.org/process/deprecated.html#strcpy
--- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c | 2 +- tools/testing/selftests/bpf/prog_tests/setget_sockopt.c | 2 +- tools/testing/selftests/bpf/prog_tests/sockopt_sk.c | 2 +- tools/testing/selftests/bpf/prog_tests/test_veristat.c | 4 ++-- tools/testing/selftests/bpf/xdp_features.c | 3 ++- 6 files changed, 8 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +1 -1
@@ -432,7 +432,7 @@ int make_sockaddr(int family, const char *addr_str, __u16 port,memset (addr,0 ,sizeof (*sun)); sun->sun_family = family; sun->sun_path[0 ] =0 ; -strcpy (sun->sun_path +1 , addr_str); +strscpy (sun->sun_path +1 , addr_str,sizeof (sun->sun_path) -1 );if (len) *len = offsetof(struct sockaddr_un , sun_path) +1 +strlen (addr_str);return 0 ;
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +1 -1
@@ -281,7 +281,7 @@ static void test_dctcp_fallback(void) dctcp_skel =bpf_dctcp__open ();if (!ASSERT_OK_PTR (dctcp_skel,"dctcp_skel" ))return ; -strcpy (dctcp_skel->rodata->fallback_cc,"cubic" ); +strscpy (dctcp_skel->rodata->fallback_cc,"cubic" ,sizeof (dctcp_skel->rodata->fallback_cc));if (!ASSERT_OK (bpf_dctcp__load (dctcp_skel),"bpf_dctcp__load" )) goto done;
tools/testing/selftests/bpf/prog_tests/setget_sockopt.c +1 -1
@@ -212,7 +212,7 @@ void test_setget_sockopt(void)if (!ASSERT_OK_PTR (skel,"open skel" )) goto done; -strcpy (skel->rodata->veth,"binddevtest1" ); +strscpy (skel->rodata->veth,"binddevtest1" ,sizeof (skel->rodata->veth)); skel->rodata->veth_ifindex =if_nametoindex ("binddevtest1" );if (!ASSERT_GT (skel->rodata->veth_ifindex,0 ,"if_nametoindex" )) goto done;
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +1 -1
@@ -142,7 +142,7 @@ static int getsetsockopt(void)/* TCP_CONGESTION can extend the string */ -strcpy (buf.cc,"nv" ); +strscpy (buf.cc,"nv" ,sizeof (buf.cc)); err =setsockopt (fd,SOL_TCP ,TCP_CONGESTION , &buf,strlen ("nv" ));if (err) {log_err ("Failed to call setsockopt(TCP_CONGESTION)" );
tools/testing/selftests/bpf/prog_tests/test_veristat.c +2 -2
@@ -24,9 +24,9 @@ static struct fixture *init_fixture(void)/* for no_alu32 and cpuv4 veristat is in parent folder */ if (access ("./veristat" ,F_OK ) ==0 ) -strcpy (fix->veristat,"./veristat" ); +strscpy (fix->veristat,"./veristat" ,sizeof (fix->veristat));else if (access ("../veristat" ,F_OK ) ==0 ) -strcpy (fix->veristat,"../veristat" ); +strscpy (fix->veristat,"../veristat" ,sizeof (fix->veristat));else PRINT_FAIL ("Can't find veristat binary" );
tools/testing/selftests/bpf/xdp_features.c +2 -1
@@ -16,6 +16,7 @@#include <network_helpers.h> +#include "bpf_util.h" #include "xdp_features.skel.h" #include "xdp_features.h"
@@ -212,7 +213,7 @@ static void set_env_default(void) env.feature.drv_feature =NETDEV_XDP_ACT_NDO_XMIT ; env.feature.action = -EINVAL ; env.ifindex = -ENODEV ; -strcpy (env.ifname,"unknown" ); +strscpy (env.ifname,"unknown" ,sizeof (env.ifname));make_sockaddr (AF_INET6 ,"::ffff:127.0.0.1" ,DUT_CTRL_PORT , &env.dut_ctrl_addr,NULL ); make_sockaddr(AF_INET6, "::ffff:127.0.0.1", DUT_ECHO_PORT,
Signature · 1 line
2.53.0
Ihor Solodrai
reviewerPATCH 4/7 selftests/bpf: Replace strncpy() with strscpy()
In reply to Ihor Solodraistrncpy() does not guarantee NUL-termination and is considered deprecated [1]. Replace strncpy() calls with strscpy().
[1] https://docs.kernel.org/process/deprecated.html#strncpy-on-nul-terminated-strings
--- tools/testing/selftests/bpf/network_helpers.c | 3 +-- tools/testing/selftests/bpf/prog_tests/align.c | 2 +- tools/testing/selftests/bpf/prog_tests/bpf_iter.c | 3 +-- tools/testing/selftests/bpf/prog_tests/flow_dissector.c | 4 ++-- tools/testing/selftests/bpf/prog_tests/queue_stack_map.c | 4 ++-- tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c | 2 +- tools/testing/selftests/bpf/prog_tests/task_local_data.h | 2 +- tools/testing/selftests/bpf/prog_tests/tc_redirect.c | 2 +- tools/testing/selftests/bpf/test_progs.c | 2 +- tools/testing/selftests/bpf/xdp_hw_metadata.c | 4 ++-- 10 files changed, 13 insertions(+), 15 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +1 -2
@@ -581,8 +581,7 @@ int open_tuntap(const char *dev_name, bool need_mac)return -1 ; ifr.ifr_flags =IFF_NO_PI | (need_mac ?IFF_TAP :IFF_TUN ); -strncpy (ifr.ifr_name, dev_name,IFNAMSIZ -1 ); - ifr.ifr_name[IFNAMSIZ -1 ] ='\0' ; +strscpy (ifr.ifr_name, dev_name,sizeof (ifr.ifr_name)); err =ioctl (fd,TUNSETIFF , &ifr);if (!ASSERT_OK (err,"ioctl(TUNSETIFF)" )) {
tools/testing/selftests/bpf/prog_tests/align.c +1 -1
@@ -633,7 +633,7 @@ static int do_test_single(struct bpf_align_test *test) }else { ret =0 ;/* We make a local copy so that we can strtok() it */ -strncpy (bpf_vlog_copy, bpf_vlog,sizeof (bpf_vlog_copy)); +strscpy (bpf_vlog_copy, bpf_vlog,sizeof (bpf_vlog_copy)); start =strstr (bpf_vlog_copy, main_pass_start);if (!start) { ret =1 ;
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +1 -2
@@ -346,8 +346,7 @@ static void test_task_sleepable(void)close (finish_pipe[1 ]); test_data =malloc (sizeof (char ) *10 ); -strncpy (test_data,"test_data" ,10 ); - test_data[9 ] ='\0' ; +strscpy (test_data,"test_data" ,10 ); test_data_long =malloc (sizeof (char ) *5000 );for (int i =0 ; i <5000 ; ++i) {
tools/testing/selftests/bpf/prog_tests/flow_dissector.c +2 -2
@@ -570,7 +570,7 @@ static int create_tap(const char *ifname) };int fd, ret; -strncpy (ifr.ifr_name, ifname,sizeof (ifr.ifr_name)); +strscpy (ifr.ifr_name, ifname,sizeof (ifr.ifr_name)); fd =open ("/dev/net/tun" ,O_RDWR );if (fd <0 )
@@ -599,7 +599,7 @@ static int ifup(const char *ifname)struct ifreq ifr = {};int sk, ret; -strncpy (ifr.ifr_name, ifname,sizeof (ifr.ifr_name)); +strscpy (ifr.ifr_name, ifname,sizeof (ifr.ifr_name)); sk =socket (PF_INET ,SOCK_DGRAM ,0 );if (sk <0 )
tools/testing/selftests/bpf/prog_tests/queue_stack_map.c +2 -2
@@ -28,9 +28,9 @@ static void test_queue_stack_map_by_type(int type) vals[i] =rand ();if (type ==QUEUE ) -strncpy (file,"./test_queue_map.bpf.o" ,sizeof (file)); +strscpy (file,"./test_queue_map.bpf.o" ,sizeof (file));else if (type ==STACK ) -strncpy (file,"./test_stack_map.bpf.o" ,sizeof (file)); +strscpy (file,"./test_stack_map.bpf.o" ,sizeof (file));else return ;
tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c +1 -1
@@ -34,7 +34,7 @@ void test_skc_to_unix_sock(void)memset (&sockaddr,0 ,sizeof (sockaddr)); sockaddr.sun_family =AF_UNIX ; -strncpy (sockaddr.sun_path, sock_path,strlen (sock_path)); +strscpy (sockaddr.sun_path, sock_path,sizeof (sockaddr.sun_path)); sockaddr.sun_path[0 ] ='\0' ; err =bind (sockfd, (struct sockaddr *)&sockaddr,sizeof (sockaddr));
tools/testing/selftests/bpf/prog_tests/task_local_data.h +1 -1
@@ -262,7 +262,7 @@ static tld_key_t __tld_create_key(const char *name, size_t size, bool dyn_data)if (!atomic_compare_exchange_strong (&tld_meta_p->cnt, &cnt, cnt +1 )) goto retry; -strncpy (tld_meta_p->metadata[i].name, name,TLD_NAME_LEN ); +strscpy (tld_meta_p->metadata[i].name, name,TLD_NAME_LEN );atomic_store (&tld_meta_p->metadata[i].size, size);return (tld_key_t ){(__s16 )off}; }
tools/testing/selftests/bpf/prog_tests/tc_redirect.c +1 -1
@@ -1095,7 +1095,7 @@ static int tun_open(char *name) ifr.ifr_flags =IFF_TUN |IFF_NO_PI ;if (*name) -strncpy (ifr.ifr_name, name,IFNAMSIZ ); +strscpy (ifr.ifr_name, name,IFNAMSIZ ); err =ioctl (fd,TUNSETIFF , &ifr);if (!ASSERT_OK (err,"ioctl TUNSETIFF" ))
tools/testing/selftests/bpf/test_progs.c +1 -1
@@ -1799,7 +1799,7 @@ static int worker_main_send_subtests(int sock, struct test_state *state) msg.subtest_done.num = i; -strncpy (msg.subtest_done.name, subtest_state->name,MAX_SUBTEST_NAME ); +strscpy (msg.subtest_done.name, subtest_state->name,MAX_SUBTEST_NAME ); msg.subtest_done.error_cnt = subtest_state->error_cnt; msg.subtest_done.skipped = subtest_state->skipped;
tools/testing/selftests/bpf/xdp_hw_metadata.c +2 -2
@@ -550,7 +550,7 @@ static int rxq_num(const char *ifname)struct ifreq ifr = { .ifr_data = (void *)&ch, }; -strncpy (ifr.ifr_name, ifname,IF_NAMESIZE -1 ); +strscpy (ifr.ifr_name, ifname,sizeof (ifr.ifr_name));int fd, ret; fd =socket (AF_UNIX ,SOCK_DGRAM ,0 );
@@ -571,7 +571,7 @@ static void hwtstamp_ioctl(int op, const char *ifname, struct hwtstamp_config *cstruct ifreq ifr = { .ifr_data = (void *)cfg, }; -strncpy (ifr.ifr_name, ifname,IF_NAMESIZE -1 ); +strscpy (ifr.ifr_name, ifname,sizeof (ifr.ifr_name));int fd, ret; fd =socket (AF_UNIX ,SOCK_DGRAM ,0 );
Signature · 1 line
2.53.0
Ihor Solodrai
reviewerPATCH 5/7 selftests/bpf: Use strscpy_cat() in the test_loader
In reply to Ihor SolodraiReplace a pair of strcpy() calls that construct the unpriv test name with a single strscpy_cat() call. This simplifies the code and adds bounds checking.
--- tools/testing/selftests/bpf/test_loader.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/test_loader.c +4 -4
@@ -673,18 +673,18 @@ static int parse_test_spec(struct test_loader *tester,if (spec->mode_mask &UNPRIV ) {int descr_len =strlen (description); -const char *suffix =" @unpriv" ; +static const char suffix[] =" @unpriv" ; +int name_len = descr_len +sizeof (suffix) +1 ;char *name; - name =malloc (descr_len +strlen (suffix) +1 ); + name =malloc (name_len);if (!name) {PRINT_FAIL ("failed to allocate memory for unpriv.name\n" ); err = -ENOMEM ; goto cleanup; } -strcpy (name, description); -strcpy (&name[descr_len], suffix); +strscpy_cat (name, name_len, description, suffix); spec->unpriv.name = name; }
Signature · 1 line
2.53.0
Ihor Solodrai
reviewerPATCH 6/7 selftests/bpf: Use strscpy in bpftool_helpers.c
In reply to Ihor SolodraiReplace strncpy() and snprintf() calls in bpftool_helpers.c with strscpy() and strscpy_cat() respectively.
Pass the destination buffer size to detect_bpftool_path() instead of hardcoding BPFTOOL_PATH_MAX_LEN.
--- tools/testing/selftests/bpf/bpftool_helpers.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/bpftool_helpers.c +9 -7
@@ -1,33 +1,35 @@// SPDX-License-Identifier: GPL-2.0-only -#include "bpftool_helpers.h" #include <unistd.h> #include <string.h> #include <stdbool.h> +#include "bpf_util.h" +#include "bpftool_helpers.h" +#define BPFTOOL_PATH_MAX_LEN 64#define BPFTOOL_FULL_CMD_MAX_LEN 512#define BPFTOOL_DEFAULT_PATH "tools/sbin/bpftool" -static int detect_bpftool_path (char *buffer) +static int detect_bpftool_path (char *buffer,size_t size) {char tmp[BPFTOOL_PATH_MAX_LEN ];/* Check default bpftool location (will work if we are running the * default flavor of test_progs) */ -snprintf (tmp,BPFTOOL_PATH_MAX_LEN ,"./%s" ,BPFTOOL_DEFAULT_PATH ); +strscpy_cat (tmp,sizeof (tmp),"./" ,BPFTOOL_DEFAULT_PATH );if (access (tmp,X_OK ) ==0 ) { -strncpy (buffer, tmp,BPFTOOL_PATH_MAX_LEN ); +strscpy (buffer, tmp, size);return 0 ; }/* Check alternate bpftool location (will work if we are running a * specific flavor of test_progs, e.g. cpuv4 or no_alu32) */ -snprintf (tmp,BPFTOOL_PATH_MAX_LEN ,"../%s" ,BPFTOOL_DEFAULT_PATH ); +strscpy_cat (tmp,sizeof (tmp),"../" ,BPFTOOL_DEFAULT_PATH );if (access (tmp,X_OK ) ==0 ) { -strncpy (buffer, tmp,BPFTOOL_PATH_MAX_LEN ); +strscpy (buffer, tmp, size);return 0 ; }
@@ -44,7 +46,7 @@ static int run_command(char *args, char *output_buf, size_t output_max_len)int ret;/* Detect and cache bpftool binary location */ -if (bpftool_path[0 ] ==0 &&detect_bpftool_path (bpftool_path)) +if (bpftool_path[0 ] ==0 &&detect_bpftool_path (bpftool_path,sizeof (bpftool_path)))return 1 ; ret = snprintf(command, BPFTOOL_FULL_CMD_MAX_LEN, "%s %s%s",
Signature · 1 line
2.53.0
Ihor Solodrai
reviewerPATCH 7/7 selftests/bpf: Use memcpy() for bounded non-NULL-terminated copies
In reply to Ihor SolodraiReplace strncpy() with memcpy() in cases where the source is non-NULL-terminated and the copy length is known.
--- tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c | 6 ++++-- tools/testing/selftests/bpf/test_verifier.c | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c +4 -2
@@ -308,8 +308,10 @@ static int find_field_offset(struct btf *btf, char *pattern, regmatch_t *matchesreturn -1 ; } -strncpy (type_str, type, type_sz); -strncpy (field_str, field, field_sz); +memcpy (type_str, type, type_sz); + type_str[type_sz] ='\0' ; +memcpy (field_str, field, field_sz); + field_str[field_sz] ='\0' ; btf_id =btf__find_by_name (btf, type_str);if (btf_id <0 ) {PRINT_FAIL ("No BTF info for type %s\n" , type_str);
tools/testing/selftests/bpf/test_verifier.c +1 -1
@@ -1320,7 +1320,7 @@ static bool cmp_str_seq(const char *log, const char *exp)printf ("FAIL\nTestcase bug\n" );return false; } -strncpy (needle, exp, len); +memcpy (needle, exp, len); needle[len] =0 ; q =strstr (log, needle);if (!q) {
Signature · 1 line
2.53.0
Re: [PATCH bpf v1 2/7] selftests/bpf: Add strscpy_cat()
In reply to Ihor SolodraiQuoted 39 lines · Ihor Solodrai
Add
sized_strscpy_cat()tobpf_util.h, which concatenates multiple strings into a destination buffer withstrscpy().Add
strscpy_cat()macro with varargs.This is a convenient helper that provides the same guarantees as
strscpy(), but for a case when multiple strings need to be concatenated into destination.Signed-off-by Ihor Solodrai <[email protected]>--- tools/testing/selftests/bpf/bpf_util.h | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)tools/testing/selftests/bpf/bpf_util.h +16
-0@@ -52,6 +52,34 @@ static inline ssize_t sized_strscpy(char *dest, const char *src, size_t count)#undef strscpy/* Redefine the placeholder from tools/include/linux/string.h */ #define strscpy sized_strscpy +/* +* strscpy() analogue that concatenates multiple strings into a buffer +*/ +static inline ssize_t sized_strscpy_cat (char *dest,size_t dest_sz, +const char *const *srcs,size_t n) +{ +ssize_t pos =0 ; + +if (dest_sz ==0 ) +return -E2BIG ; + +for (size_t i =0 ; i < n; i++) { +ssize_t res =strscpy (dest + pos, srcs[i], dest_sz - pos); + +if (res <0 ) +return res;
tools/testing/selftests/bpf/bpf_util.h static inline ssize_t sized_strscpy(char *dest, const char *src, size_t count) · line 80
Quoted 10 lines
tools/testing/selftests/bpf/bpf_util.h +10
-0+ pos += res; + } + +return pos; +} + +#define strscpy_cat (dest, count, ...) \ + sized_strscpy_cat(dest, count, \ + (const char * const[]){ __VA_ARGS__ }, \ + ARRAY_SIZE(((const char * const[]){ __VA_ARGS__ })))
Interesting idea, but this is not bpf material anymore, and api needs to be discussed.
Let's drop it for now and corresponding changes in later patches.
Re: patch 1.
I think we can do kernel's trick:
CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
We have ___bpf_apply and ___bpf_narg in bpf_helpers.h so strscpy() can be 2 args and it will make patch 3 less churny.
Also combine both patch sets, since the ASAN one depends on this one.
pw-bot: cr