We should move away from using strncpy because it is deprecated [1]. Since these buffers want to be NUL-terminated, let's use strscpy() which guarantees this behavior.
The code in question enables the visual autocomplete when using kdb tab completion. After pressing tab a couple times when sitting on a partial symbol it will attempt to fill it in. In my testing, strscpy() provides the exact same autocomplete behavior that strncpy() provides here (i.e: it fills in the same number of characters for the user). You can confirm this by enabling kdb [3] and booting up the kernel. I performed my tests with qemu with this incantation (wow these get long): $ /usr/bin/qemu-system-x86_64 -display none -nodefaults -cpu Nehalem -append 'console=ttyS0,115200 earlycon=uart8250,io,0x3f8 rdinit=/bin/sh kgdboc=ttyS0,115200 nokaslr' -kernel $BUILD_DIR/arch/x86/boot/bzImage -initrd $REPOS/boot-utils/images/x86_64/rootfs.cpio -m 512m -serial mon:stdio ... then you can type some symbols and see that autocomplete still kicks in and performs exactly the same. For example: tes <tab><tab> gives you "test", then "test_ap" <tab><tab> gives you "test_aperfmperf" Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: https://github.com/KSPP/linux/issues/90 [2] Link: https://www.kernel.org/doc/html/v5.0/dev-tools/kgdb.html#using-kdb [3] Cc: linux-harden...@vger.kernel.org Signed-off-by: Justin Stitt <justinst...@google.com> --- Changes in v2: - use strscpy over memcpy (thanks Daniel T.) - Link to v1: https://lore.kernel.org/r/20240403-strncpy-kernel-debug-kdb-kdb_io-c-v1-1-7f78a08e9...@google.com --- --- kernel/debug/kdb/kdb_io.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 9443bc63c5a2..60be22132020 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -368,9 +368,9 @@ static char *kdb_read(char *buffer, size_t bufsize) kdb_printf("%s", buffer); } else if (tab != 2 && count > 0) { len_tmp = strlen(p_tmp); - strncpy(p_tmp+len_tmp, cp, lastchar-cp+1); + strscpy(p_tmp+len_tmp, cp, lastchar-cp+1); len_tmp = strlen(p_tmp); - strncpy(cp, p_tmp+len, len_tmp-len + 1); + strscpy(cp, p_tmp+len, len_tmp-len + 1); len = len_tmp - len; kdb_printf("%s", cp); cp += len; --- base-commit: 026e680b0a08a62b1d948e5a8ca78700bfac0e6e change-id: 20240402-strncpy-kernel-debug-kdb-kdb_io-c-53e5ed26da3d Best regards, -- Justin Stitt <justinst...@google.com>