diff mbox series

bpftool: Replace strncpy with strscpy

Message ID 20250228181827.90436-1-michaelestner@web.de (mailing list archive)
State Rejected
Delegated to: BPF
Headers show
Series bpftool: Replace strncpy with strscpy | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test
bpf/vmtest-bpf-next-VM_Test-18 fail Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-17 success Logs for x86_64-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-18 / test
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-18 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-31 fail Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-30 fail Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-25 fail Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-llvm-17 / test
bpf/vmtest-bpf-next-VM_Test-24 fail Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-llvm-17 / GCC BPF
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 fail Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-18 / GCC BPF
bpf/vmtest-bpf-next-VM_Test-10 success Logs for s390x-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / veristat-kernel

Commit Message

Michael Estner Feb. 28, 2025, 6:18 p.m. UTC
strncpy() is deprecated for NUL-terminated destination buffers. Use
strscpy() instead and remove the manual NUL-termination.

Compile-tested only.

Link: https://github.com/KSPP/linux/issues/90

Signed-off-by: Michael Estner <michaelestner@web.de>
---
 tools/bpf/bpftool/xlated_dumper.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

--
2.25.1

Comments

Quentin Monnet Feb. 28, 2025, 9:37 p.m. UTC | #1
2025-02-28 19:18 UTC+0100 ~ Michael Estner <michaelestner@web.de>
> strncpy() is deprecated for NUL-terminated destination buffers. Use
> strscpy() instead and remove the manual NUL-termination.
> 
> Compile-tested only.


How? The change does _not_ compile in my case:

	$ cd tool/bpf/bpftool
	$ make -j
	[...]
	/usr/bin/ld: xlated_dumper.o: in function `print_insn_json':
	xlated_dumper.c:(.text+0x1f6): undefined reference to `strscpy'
	collect2: error: ld returned 1 exit status
	make: *** [Makefile:254: bpftool] Error 1

(Besides, this code should be rather easy to test, so running it is
appreciated.)

strscpy() has been proposed for bpftool a few times in the past, but
bpftool is a user space utility and does not currently #include header
linux/string.h. If we wanted to use strscpy(), we'd likely need to use
this header, and also to copy the definition of the function to the
GitHub mirror. Given that - as far as I know - the current use of
strncpy() is not broken, I'm not sure this is worth the effort.


> Link: https://github.com/KSPP/linux/issues/90


I note that this Issue provides a command for looking for strncpy()
instances to replace, but this command filters out occurrences that are
under tools/:

	"git grep ... | grep -vE '^(Documentation|tools|...) ..."

Thanks,
Quentin
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
index d0094345fb2b..60dbe48a91a3 100644
--- a/tools/bpf/bpftool/xlated_dumper.c
+++ b/tools/bpf/bpftool/xlated_dumper.c
@@ -135,8 +135,7 @@  print_insn_json(void *private_data, const char *fmt, ...)

 	va_start(args, fmt);
 	if (l > 0) {
-		strncpy(chomped_fmt, fmt, l - 1);
-		chomped_fmt[l - 1] = '\0';
+		strscpy(chomped_fmt, fmt);
 	}
 	jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
 	va_end(args);