]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-client.git/commitdiff
selftests/bpf: Add simple strscpy() implementation
authorIhor Solodrai <ihor.solodrai@linux.dev>
Mon, 23 Feb 2026 19:07:17 +0000 (11:07 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 24 Feb 2026 02:40:07 +0000 (18:40 -0800)
Replace bpf_strlcpy() in bpf_util.h with a sized_strscpy(), which is a
simplified sized_strscpy() from the kernel (lib/string.c [1]). It:
  * takes a count (destination size) parameter
  * guarantees NULL-termination
  * returns the number of characters copied or -E2BIG

Re-define strscpy macro similar to in-kernel implementation [2]: allow
the count parameter to be optional.

Add #ifdef-s to tools/include/linux/args.h, as they may be defined in
other system headers (for example, __CONCAT in sys/cdefs.h).

Fixup the single existing bpf_strlcpy() call in cgroup_helpers.c

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/string.c?h=v6.19#n113
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/string.h?h=v6.19#n91

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Link: https://lore.kernel.org/r/20260223190736.649171-2-ihor.solodrai@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/include/linux/args.h
tools/testing/selftests/bpf/bpf_util.h
tools/testing/selftests/bpf/cgroup_helpers.c

index 2e8e65d975c77822ab87d77d24861db23ed4d213..14b268f2389acb17b1007703db636dcd3717473e 100644 (file)
 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
 
 /* Concatenate two parameters, but allow them to be expanded beforehand. */
+#ifndef __CONCAT
 #define __CONCAT(a, b) a ## b
+#endif
+#ifndef CONCATENATE
 #define CONCATENATE(a, b) __CONCAT(a, b)
+#endif
 
 #endif /* _LINUX_ARGS_H */
index 4bc2d25f33e18026440f092c7907f10a23b2d00f..6cb56501a505da78bb8c43f648f43987c12c901d 100644 (file)
@@ -8,6 +8,7 @@
 #include <errno.h>
 #include <syscall.h>
 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
+#include <linux/args.h>
 
 static inline unsigned int bpf_num_possible_cpus(void)
 {
@@ -21,25 +22,43 @@ 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;
 }
 
+#define __strscpy0(dst, src, ...)      \
+       sized_strscpy(dst, src, sizeof(dst))
+#define __strscpy1(dst, src, size)     \
+       sized_strscpy(dst, src, size)
+
+#undef strscpy /* Redefine the placeholder from tools/include/linux/string.h */
+#define strscpy(dst, src, ...) \
+       CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
+
 #define __bpf_percpu_val_align __attribute__((__aligned__(8)))
 
 #define BPF_DECLARE_PERCPU(type, name)                         \
index 20cede4db3cee85ce1fd337388cab5c742d58ad4..45cd0b479fe35719b1fc9899eabd9cabd2d66a70 100644 (file)
@@ -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);
        }
 
        snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);