Skip to contentSkip to conversation

m68k: Use strscpy to replace strlcpy

PATCH In review 陈学兵 2 participants 2 messages May 22, 2022 – Jul 6, 2022 m68k architecture

lore
Share & tools
lore

陈学兵

author
#0 lore raw

PATCH 1/1 m68k: Use strscpy to replace strlcpy

The strlcpy should not be used because it doesn't limit the source length. Preferred is strscpy.

---
 arch/m68k/emu/natfeat.c     | 2 +-
 arch/m68k/kernel/setup_mm.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/m68k/emu/natfeat.c +1 -1
index 71b78ecee75c..a17637b2b237 100644
@@ -43,7 +43,7 @@ long nf_get_id(const char *feature_name)
 	char name_copy[32];
 	size_t n;
 
-	n = strlcpy(name_copy, feature_name, sizeof(name_copy));
+	n = strscpy(name_copy, feature_name, sizeof(name_copy));
 	if (n >= sizeof(name_copy))
 		return 0;
 
arch/m68k/kernel/setup_mm.c +1 -1
index 8f94feed969c..526bd1781d73 100644
@@ -158,7 +158,7 @@ static void __init m68k_parse_bootinfo(const struct bi_record *record)
 			break;
 
 		case BI_COMMAND_LINE:
-			strlcpy(m68k_command_line, data,
+			strscpy(m68k_command_line, data,
 				sizeof(m68k_command_line));
 			break;
 
Signature · 1 line
2.36.1
#1 lore raw

Hi Xuebing,

Quoted 4 lines · 陈学兵

The strlcpy should not be used because it doesn't limit the source length. Preferred is strscpy.

Thanks for your patch!

arch/m68k/emu/natfeat.c long nf_get_id(const char *feature_name) · line 47

Quoted 9 lines
arch/m68k/emu/natfeat.c +1 -1
@@ -43,7 +43,7 @@ long nf_get_id(const char *feature_name)
        char name_copy[32];
        size_t n;
 
-       n = strlcpy(name_copy, feature_name, sizeof(name_copy));
+       n = strscpy(name_copy, feature_name, sizeof(name_copy));
        if (n >= sizeof(name_copy))

You forgot to update the error checking: on failure, strscpy() returns a negative error code. This is assigned to (unsigned) size_t, hence will be larger than sizeof(name_copy). So the check will still still work, but it is non-obvious.

arch/m68k/emu/natfeat.c long nf_get_id(const char *feature_name) · line 49

Quoted 2 lines
arch/m68k/emu/natfeat.c +0 -0
                return 0;
 

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds