Skip to contentSkip to conversation

printk: Replace strncmp with str_has_prefix

PATCH Applied Chuhong Yuan 2 participants 2 messages Aug 2, 2019 Printk

v2 of 4 Applied Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 6/10 printk: Replace strncmp with str_has_prefix

strncmp(str, const, len) is error-prone because len is easy to have typo.
The example is the hard-coded len has counting error or sizeof(const) forgets - 1.
So we prefer using newly introduced str_has_prefix to substitute such strncmp.

---
Changes in v2:
- Revise the description.
- Utilize str_has_prefix's return value to eliminate some hard codes.

 kernel/printk/braille.c | 10 ++++++----
 kernel/printk/printk.c  | 14 ++++++++------
 2 files changed, 14 insertions(+), 10 deletions(-)
kernel/printk/braille.c +6 -4
index 1d21ebacfdb8..e451b8b1d3d5 100644
@@ -11,11 +11,13 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-	if (!strncmp(*str, "brl,", 4)) {
+	size_t len;
+
+	if ((len = str_has_prefix(*str, "brl,"))) {
 		*brl_options = "";
-		*str += 4;
-	} else if (!strncmp(*str, "brl=", 4)) {
-		*brl_options = *str + 4;
+		*str += len;
+	} else if ((len = str_has_prefix(*str, "brl="))) {
+		*brl_options = *str + len;
 		*str = strchr(*brl_options, ',');
 		if (!*str) {
 			pr_err("need port name after brl=\n");
kernel/printk/printk.c +8 -6
index 1888f6a3b694..21b28c7dd18f 100644
@@ -118,18 +118,20 @@ static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
 
 static int __control_devkmsg(char *str)
 {
+	size_t len;
+
 	if (!str)
 		return -EINVAL;
 
-	if (!strncmp(str, "on", 2)) {
+	if ((len = str_has_prefix(str, "on"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_ON;
-		return 2;
-	} else if (!strncmp(str, "off", 3)) {
+		return len;
+	} else if ((len = str_has_prefix(str, "off"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_OFF;
-		return 3;
-	} else if (!strncmp(str, "ratelimit", 9)) {
+		return len;
+	} else if ((len = str_has_prefix(str, "ratelimit"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
-		return 9;
+		return len;
 	}
 	return -EINVAL;
 }
Signature · 1 line
2.20.1
#1 lore raw
Quoted 6 lines · Chuhong Yuan

strncmp(str, const, len) is error-prone because len is easy to have typo.
The example is the hard-coded len has counting error or sizeof(const) forgets - 1.
So we prefer using newly introduced str_has_prefix to substitute such strncmp.

[]

kernel/printk/printk.c

Quoted 1 line
kernel/printk/printk.c +0 -0

[]

Quoted 17 lines
kernel/printk/printk.c +5 -3
@@ -118,18 +118,20 @@ static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
 
 static int __control_devkmsg(char *str)
 {
+	size_t len;
+
 	if (!str)
 		return -EINVAL;
 
-	if (!strncmp(str, "on", 2)) {
+	if ((len = str_has_prefix(str, "on"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_ON;
-		return 2;
-	} else if (!strncmp(str, "off", 3)) {
+		return len;
+	} else if ((len = str_has_prefix(str, "off"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_OFF;

kernel/printk/printk.c static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; · line 137

Quoted 10 lines
kernel/printk/printk.c +3 -3
-		return 3;
-	} else if (!strncmp(str, "ratelimit", 9)) {
+		return len;
+	} else if ((len = str_has_prefix(str, "ratelimit"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
-		return 9;
+		return len;
 	}
 	return -EINVAL;
 }

All of the else uses above could also be removed.