[PATCH bpf v1 2/7] selftests/bpf: Add strscpy_cat()

Ihor Solodrai posted 7 patches 1 month, 1 week ago
[PATCH bpf v1 2/7] selftests/bpf: Add strscpy_cat()
Posted by Ihor Solodrai 1 month, 1 week ago
Add 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.

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
 tools/testing/selftests/bpf/bpf_util.h | 28 ++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
index 8c95ef7ed7c0..61bb22c3e517 100644
--- a/tools/testing/selftests/bpf/bpf_util.h
+++ b/tools/testing/selftests/bpf/bpf_util.h
@@ -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)				\
-- 
2.53.0
Re: [PATCH bpf v1 2/7] selftests/bpf: Add strscpy_cat()
Posted by Alexei Starovoitov 1 month, 1 week ago
On Fri, Feb 20, 2026 at 10:20 AM Ihor Solodrai <ihor.solodrai@linux.dev> wrote:
>
> Add 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.
>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
> ---
>  tools/testing/selftests/bpf/bpf_util.h | 28 ++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
> index 8c95ef7ed7c0..61bb22c3e517 100644
> --- a/tools/testing/selftests/bpf/bpf_util.h
> +++ b/tools/testing/selftests/bpf/bpf_util.h
> @@ -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__ })))

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