[PATCH] riscv: pi: replace strlcat with strscpy in get_early_cmdline

Thorsten Blum
2026-05-04T15:50:38Z

Use the return value of strscpy() instead of calling strlen(fdt_cmdline)
again and return early on string truncation. Drop the explicit size
argument since early_cmdline has a fixed length, which strscpy()
determines using sizeof() when the argument is omitted.

Replace strlcat() with strscpy() to append CONFIG_CMDLINE.

Also remove the unnecessary fdt_cmdline NULL initialization.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
arch/riscv/kernel/pi/cmdline_early.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cmdline_early.c
index 389d086a0718..0afbe4077cb8 100644
--- a/arch/riscv/kernel/pi/cmdline_early.c
+++ b/arch/riscv/kernel/pi/cmdline_early.c
@@ -12,8 +12,8 @@ static char early_cmdline[COMMAND_LINE_SIZE];

static char *get_early_cmdline(uintptr_t dtb_pa)
{
- const char *fdt_cmdline = NULL;
- unsigned int fdt_cmdline_size = 0;
+ const char *fdt_cmdline;
+ ssize_t fdt_cmdline_size = 0;
int chosen_node;

if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
@@ -22,18 +22,18 @@ static char *get_early_cmdline(uintptr_t dtb_pa)
fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
"bootargs", NULL);
if (fdt_cmdline) {
- fdt_cmdline_size = strlen(fdt_cmdline);
- strscpy(early_cmdline, fdt_cmdline,
- COMMAND_LINE_SIZE);
+ fdt_cmdline_size = strscpy(early_cmdline, fdt_cmdline);
+ if (fdt_cmdline_size < 0)
+ return early_cmdline;
}
}
}

if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
- fdt_cmdline_size == 0 /* CONFIG_CMDLINE_FALLBACK */) {
- strlcat(early_cmdline, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
- }
+ fdt_cmdline_size == 0 /* CONFIG_CMDLINE_FALLBACK */)
+ strscpy(early_cmdline + fdt_cmdline_size, CONFIG_CMDLINE,
+ COMMAND_LINE_SIZE - fdt_cmdline_size);

return early_cmdline;
}