Skip to contentSkip to conversation

printk: Replace strncmp with str_has_prefix

PATCH In review Chuhong Yuan 3 participants 6 messages Jul 29, 2019 – Aug 1, 2019 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 8/12 printk: Replace strncmp with str_has_prefix

strncmp(str, const, len) is error-prone.
We had better use newly introduced
str_has_prefix() instead of it.

---
 kernel/printk/braille.c | 4 ++--
 kernel/printk/printk.c  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
kernel/printk/braille.c +2 -2
index 1d21ebacfdb8..64f0fb8ef27d 100644
@@ -11,10 +11,10 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-	if (!strncmp(*str, "brl,", 4)) {
+	if (str_has_prefix(*str, "brl,")) {
 		*brl_options = "";
 		*str += 4;
-	} else if (!strncmp(*str, "brl=", 4)) {
+	} else if (str_has_prefix(*str, "brl=")) {
 		*brl_options = *str + 4;
 		*str = strchr(*brl_options, ',');
 		if (!*str) {
kernel/printk/printk.c +3 -3
index 1888f6a3b694..9e60dce4bdd5 100644
@@ -121,13 +121,13 @@ static int __control_devkmsg(char *str)
 	if (!str)
 		return -EINVAL;
 
-	if (!strncmp(str, "on", 2)) {
+	if (str_has_prefix(str, "on")) {
 		devkmsg_log = DEVKMSG_LOG_MASK_ON;
 		return 2;
-	} else if (!strncmp(str, "off", 3)) {
+	} else if (str_has_prefix(str, "off")) {
 		devkmsg_log = DEVKMSG_LOG_MASK_OFF;
 		return 3;
-	} else if (!strncmp(str, "ratelimit", 9)) {
+	} else if (str_has_prefix(str, "ratelimit")) {
 		devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
 		return 9;
 	}
Signature · 1 line
2.20.1
#1 lore raw
Quoted 3 lines · Chuhong Yuan

strncmp(str, const, len) is error-prone.
We had better use newly introduced
str_has_prefix() instead of it.

[]

kernel/printk/braille.c

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

[]

kernel/printk/braille.c line 18

Quoted 11 lines
kernel/printk/braille.c +2 -2
@@ -11,10 +11,10 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-	if (!strncmp(*str, "brl,", 4)) {
+	if (str_has_prefix(*str, "brl,")) {
 		*brl_options = "";
 		*str += 4;
-	} else if (!strncmp(*str, "brl=", 4)) {
+	} else if (str_has_prefix(*str, "brl=")) {
 		*brl_options = *str + 4;

Better to get rid of the += 4 uses too by storing the result of str_has_prefix and using that as the addend.

Perhaps

	size_t len;

	if ((len = str_has_prefix(*str, "brl,"))) {
		*brl_options = "";
		*str += len;
	} else if ((len = str_has_prefix(*str, "brl="))) {
		etc...

kernel/printk/printk.c

Quoted 3 lines
kernel/printk/braille.c +0 -0
 		*str = strchr(*brl_options, ',');
 		if (!*str) {
kernel/printk/printk.c +0 -0

[]

Quoted 7 lines
kernel/printk/printk.c +1 -1
@@ -121,13 +121,13 @@ static int __control_devkmsg(char *str)
 	if (!str)
 		return -EINVAL;
 
-	if (!strncmp(str, "on", 2)) {
+	if (str_has_prefix(str, "on")) {
 		devkmsg_log = DEVKMSG_LOG_MASK_ON;

kernel/printk/printk.c static int __control_devkmsg(char *str) · line 133

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

here too.

#2 lore raw
Quoted 23 lines · Joe Perches <[email protected]> 于2019年7月30日周二 上午8:16写道:
Quoted 3 lines · Chuhong Yuan

strncmp(str, const, len) is error-prone.
We had better use newly introduced
str_has_prefix() instead of it.

[]

kernel/printk/braille.c

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

[]

kernel/printk/braille.c line 18

Quoted 11 lines
kernel/printk/braille.c +2 -2
@@ -11,10 +11,10 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-     if (!strncmp(*str, "brl,", 4)) {
+     if (str_has_prefix(*str, "brl,")) {
              *brl_options = "";
              *str += 4;
-     } else if (!strncmp(*str, "brl=", 4)) {
+     } else if (str_has_prefix(*str, "brl=")) {
              *brl_options = *str + 4;

Better to get rid of the += 4 uses too by storing the result of str_has_prefix and using that as the addend.

Quoted 9 lines

Perhaps

        size_t len;

        if ((len = str_has_prefix(*str, "brl,"))) {
                *brl_options = "";
                *str += len;
        } else if ((len = str_has_prefix(*str, "brl="))) {
                etc...

I find that checkpatch.pl forbids assignment in if condition. So this seems to be infeasible...

Quoted 25 lines

kernel/printk/printk.c

Quoted 3 lines
kernel/printk/braille.c +0 -0
              *str = strchr(*brl_options, ',');
              if (!*str) {
kernel/printk/printk.c +0 -0

[]

Quoted 7 lines
kernel/printk/printk.c +1 -1
@@ -121,13 +121,13 @@ static int __control_devkmsg(char *str)
      if (!str)
              return -EINVAL;
 
-     if (!strncmp(str, "on", 2)) {
+     if (str_has_prefix(str, "on")) {
              devkmsg_log = DEVKMSG_LOG_MASK_ON;

kernel/printk/printk.c static int __control_devkmsg(char *str) · line 133

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

here too.

#3 lore raw
Quoted 33 lines · Chuhong Yuan
Quoted 22 lines · Joe Perches <[email protected]> 于2019年7月30日周二 上午8:16写道:
Quoted 3 lines · Chuhong Yuan

strncmp(str, const, len) is error-prone.
We had better use newly introduced
str_has_prefix() instead of it.

[]

kernel/printk/braille.c

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

[]

kernel/printk/braille.c line 18

Quoted 11 lines
kernel/printk/braille.c +2 -2
@@ -11,10 +11,10 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-     if (!strncmp(*str, "brl,", 4)) {
+     if (str_has_prefix(*str, "brl,")) {
              *brl_options = "";
              *str += 4;
-     } else if (!strncmp(*str, "brl=", 4)) {
+     } else if (str_has_prefix(*str, "brl=")) {
              *brl_options = *str + 4;

Better to get rid of the += 4 uses too by storing the result of str_has_prefix and using that as the addend.

Quoted 9 lines

Perhaps

        size_t len;

        if ((len = str_has_prefix(*str, "brl,"))) {
                *brl_options = "";
                *str += len;
        } else if ((len = str_has_prefix(*str, "brl="))) {
                etc...
Quoted 2 lines

I find that checkpatch.pl forbids assignment in if condition. So this seems to be infeasible...

checkpatch is a stupid script and doesn't forbid
anything. It's just a suggestion guide.

Ignore checkpatch when you know better.

#4 lore raw
Quoted 7 lines · On Thu, 01 Aug 2019 09:19:04 -0700 Joe Perches <[email protected]> wrote:
Quoted 2 lines

I find that checkpatch.pl forbids assignment in if condition. So this seems to be infeasible...

checkpatch is a stupid script and doesn't forbid
anything. It's just a suggestion guide.

Ignore checkpatch when you know better.

And this is coming from the checkpatch.pl maintainer ;-)

-- Steve

#5 lore raw
In reply to Chuhong Yuan
Quoted 33 lines · Chuhong Yuan
Quoted 22 lines · Joe Perches <[email protected]> 于2019年7月30日周二 上午8:16写道:
Quoted 3 lines · Chuhong Yuan

strncmp(str, const, len) is error-prone.
We had better use newly introduced
str_has_prefix() instead of it.

[]

kernel/printk/braille.c

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

[]

kernel/printk/braille.c line 18

Quoted 11 lines
kernel/printk/braille.c +2 -2
@@ -11,10 +11,10 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-     if (!strncmp(*str, "brl,", 4)) {
+     if (str_has_prefix(*str, "brl,")) {
              *brl_options = "";
              *str += 4;
-     } else if (!strncmp(*str, "brl=", 4)) {
+     } else if (str_has_prefix(*str, "brl=")) {
              *brl_options = *str + 4;

Better to get rid of the += 4 uses too by storing the result of str_has_prefix and using that as the addend.

Quoted 9 lines

Perhaps

        size_t len;

        if ((len = str_has_prefix(*str, "brl,"))) {
                *brl_options = "";
                *str += len;
        } else if ((len = str_has_prefix(*str, "brl="))) {
                etc...
Quoted 2 lines

I find that checkpatch.pl forbids assignment in if condition. So this seems to be infeasible...

So the code could be rewritten like below:
(though it's trivial as-is)

---
 kernel/printk/braille.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)
kernel/printk/braille.c +20 -10
index 1d21ebacfdb8..46dd9fcc7525 100644
@@ -11,19 +11,29 @@
 
 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 = strchr(*brl_options, ',');
-		if (!*str) {
-			pr_err("need port name after brl=\n");
-			return -EINVAL;
-		}
-		*((*str)++) = 0;
+		*str += len;
+		return 0;
+	}
+
+	len = str_has_prefix(*str, "brl=");
+	if (!len)
+		return 0;
+
+	*brl_options = *str + len;
+
+	*str = strchr(*brl_options, ',');
+	if (!*str) {
+		pr_err("need port name after brl=\n");
+		return -EINVAL;
 	}
 
+	*((*str)++) = 0;
+
 	return 0;
 }