Skip to contentSkip to conversation

sh: machvec: clean up and use strscpy in early_parse_mv

PATCH RESEND New Thorsten Blum 1 participant 1 message May 18 SuperH

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 sh: machvec: clean up and use strscpy in early_parse_mv

Use strscpy() to copy the NUL-terminated early parameter to the destination buffer instead of using memcpy() followed by a manual NUL termination. If the early parameter contains a space, use min() to copy only up to that delimiter, otherwise copy the entire string. Drop the now unused mv_len variable.

Remove the redundant mv_name initialization because it is immediately populated by strscpy(). Drop mv_comma, which has been unused since commit 9655ad03af2d ("sh: Fixup machvec support."), and remove the dead from = mv_end assignment. And since panic() is a __noreturn function, remove the else branch as well.

Change machvec_selected from unsigned int to bool and move __initdata after the variable name to silence a checkpatch warning.

---
 arch/sh/kernel/machvec.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)
arch/sh/kernel/machvec.c +10 -17
index 57efaf5b82ae..d737a5a560cd 100644
@@ -8,6 +8,7 @@
  *  Copyright (C) 2002 - 2007 Paul Mundt
  */
 #include <linux/init.h>
+#include <linux/minmax.h>
 #include <linux/string.h>
 #include <asm/machvec.h>
 #include <asm/sections.h>
@@ -35,29 +36,21 @@ static struct sh_machine_vector * __init get_mv_byname(const char *name)
 	return NULL;
 }
 
-static unsigned int __initdata machvec_selected;
+static bool machvec_selected __initdata;
 
 static int __init early_parse_mv(char *from)
 {
-	char mv_name[MV_NAME_SIZE] = "";
+	char mv_name[MV_NAME_SIZE];
 	char *mv_end;
-	char *mv_comma;
-	int mv_len;
 	struct sh_machine_vector *mvp;
 
 	mv_end = strchr(from, ' ');
-	if (mv_end == NULL)
-		mv_end = from + strlen(from);
+	if (mv_end)
+		strscpy(mv_name, from, min(mv_end - from + 1, MV_NAME_SIZE));
+	else
+		strscpy(mv_name, from);
 
-	mv_comma = strchr(from, ',');
-	mv_len = mv_end - from;
-	if (mv_len > (MV_NAME_SIZE-1))
-		mv_len = MV_NAME_SIZE-1;
-	memcpy(mv_name, from, mv_len);
-	mv_name[mv_len] = '\0';
-	from = mv_end;
-
-	machvec_selected = 1;
+	machvec_selected = true;
 
 	/* Boot with the generic vector */
 	if (strcmp(mv_name, "generic") == 0)
@@ -71,8 +64,8 @@ static int __init early_parse_mv(char *from)
 		pr_cont("\n\n");
 		panic("Failed to select machvec '%s' -- halting.\n",
 		      mv_name);
-	} else
-		sh_mv = *mvp;
+	}
+	sh_mv = *mvp;
 
 	return 0;
 }