Skip to content

Commit 63c49ef

Browse files
theihorAlexei Starovoitov
authored andcommitted
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 [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>
1 parent 7dff99b commit 63c49ef

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

tools/include/linux/args.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
2323

2424
/* Concatenate two parameters, but allow them to be expanded beforehand. */
25+
#ifndef __CONCAT
2526
#define __CONCAT(a, b) a ## b
27+
#endif
28+
#ifndef CONCATENATE
2629
#define CONCATENATE(a, b) __CONCAT(a, b)
30+
#endif
2731

2832
#endif /* _LINUX_ARGS_H */

tools/testing/selftests/bpf/bpf_util.h

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <errno.h>
99
#include <syscall.h>
1010
#include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
11+
#include <linux/args.h>
1112

1213
static inline unsigned int bpf_num_possible_cpus(void)
1314
{
@@ -21,25 +22,43 @@ static inline unsigned int bpf_num_possible_cpus(void)
2122
return possible_cpus;
2223
}
2324

24-
/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
25-
* is zero-terminated string no matter what (unless sz == 0, in which case
26-
* it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
27-
* in what is returned. Given this is internal helper, it's trivial to extend
28-
* this, when necessary. Use this instead of strncpy inside libbpf source code.
25+
/*
26+
* Simplified strscpy() implementation. The kernel one is in lib/string.c
2927
*/
30-
static inline void bpf_strlcpy(char *dst, const char *src, size_t sz)
28+
static inline ssize_t sized_strscpy(char *dest, const char *src, size_t count)
3129
{
32-
size_t i;
30+
long res = 0;
3331

34-
if (sz == 0)
35-
return;
32+
if (count == 0)
33+
return -E2BIG;
3634

37-
sz--;
38-
for (i = 0; i < sz && src[i]; i++)
39-
dst[i] = src[i];
40-
dst[i] = '\0';
35+
while (count > 1) {
36+
char c;
37+
38+
c = src[res];
39+
dest[res] = c;
40+
if (!c)
41+
return res;
42+
res++;
43+
count--;
44+
}
45+
46+
/* Force NUL-termination. */
47+
dest[res] = '\0';
48+
49+
/* Return E2BIG if the source didn't stop */
50+
return src[res] ? -E2BIG : res;
4151
}
4252

53+
#define __strscpy0(dst, src, ...) \
54+
sized_strscpy(dst, src, sizeof(dst))
55+
#define __strscpy1(dst, src, size) \
56+
sized_strscpy(dst, src, size)
57+
58+
#undef strscpy /* Redefine the placeholder from tools/include/linux/string.h */
59+
#define strscpy(dst, src, ...) \
60+
CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
61+
4362
#define __bpf_percpu_val_align __attribute__((__aligned__(8)))
4463

4564
#define BPF_DECLARE_PERCPU(type, name) \

tools/testing/selftests/bpf/cgroup_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
8686
enable[len] = 0;
8787
close(fd);
8888
} else {
89-
bpf_strlcpy(enable, controllers, sizeof(enable));
89+
strscpy(enable, controllers);
9090
}
9191

9292
snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);

0 commit comments

Comments
 (0)