Skip to contentSkip to conversation

printk: Replace strncmp with str_has_prefix

PATCH Applied Chuhong Yuan 3 participants 6 messages Aug 9, 2019 – Aug 16, 2019 Printk

lore
Share & tools
lore
#0 lore raw

PATCH v4 4/8 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 to make code better.

---
Changes in v4:
- Eliminate assignments in if conditions.

 kernel/printk/braille.c | 15 +++++++++++----
 kernel/printk/printk.c  | 22 ++++++++++++++++------
 2 files changed, 27 insertions(+), 10 deletions(-)
kernel/printk/braille.c +11 -4
index 1d21ebacfdb8..17a9591e54ff 100644
@@ -11,11 +11,18 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-	if (!strncmp(*str, "brl,", 4)) {
+	size_t len;
+
+	len = str_has_prefix(*str, "brl,");
+	if (len) {
 		*brl_options = "";
-		*str += 4;
-	} else if (!strncmp(*str, "brl=", 4)) {
-		*brl_options = *str + 4;
+		*str += len;
+		return 0;
+	}
+
+	len = str_has_prefix(*str, "brl=");
+	if (len) {
+		*brl_options = *str + len;
 		*str = strchr(*brl_options, ',');
 		if (!*str) {
 			pr_err("need port name after brl=\n");
kernel/printk/printk.c +16 -6
index 1888f6a3b694..43a31015ec93 100644
@@ -118,19 +118,29 @@ 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)) {
+	len = str_has_prefix(str, "on");
+	if (len) {
 		devkmsg_log = DEVKMSG_LOG_MASK_ON;
-		return 2;
-	} else if (!strncmp(str, "off", 3)) {
+		return len;
+	}
+
+	len = str_has_prefix(str, "off");
+	if (len) {
 		devkmsg_log = DEVKMSG_LOG_MASK_OFF;
-		return 3;
-	} else if (!strncmp(str, "ratelimit", 9)) {
+		return len;
+	}
+
+	len = str_has_prefix(str, "ratelimit");
+	if (len) {
 		devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
-		return 9;
+		return len;
 	}
+
 	return -EINVAL;
 }
 
Signature · 1 line
2.20.1
maintainer Reviewed-by on v4 4/8
#1 lore raw
Quoted 8 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 to make code better.

Best Regards,
Petr

Reviewed-by
#2 lore raw
Quoted 11 lines · Petr Mladek
Quoted 8 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 to make code better.

	-ss
maintainer
#3 lore raw
Quoted 9 lines · Sergey Senozhatsky
Quoted 8 lines · Petr Mladek
Quoted 7 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 to make code better.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Chuong Yuan, should I take this patch via printk.git? Or will the entire series go via some other tree, please?

Best Regards,
Petr

#4 lore raw
Quoted 11 lines · Petr Mladek
Quoted 9 lines · Sergey Senozhatsky
Quoted 8 lines · Petr Mladek
Quoted 7 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 to make code better.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Chuong Yuan, should I take this patch via printk.git? Or will the entire series go via some other tree, please?

I think that it goes via printk.git is okay, thanks!

Quoted 2 lines

Best Regards,
Petr

maintainer
#5 lore raw
Quoted 16 lines · Chuhong Yuan
Quoted 15 lines · Petr Mladek
Quoted 13 lines · Sergey Senozhatsky
Quoted 11 lines · Petr Mladek
Quoted 8 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 to make code better.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Chuong Yuan, should I take this patch via printk.git? Or will the entire series go via some other tree, please?

I think that it goes via printk.git is okay, thanks!

The patch is commited into printk.git, branch for-5.4.

Best Regards,
Petr