linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 08/12] printk: Replace strncmp with str_has_prefix
@ 2019-07-29 15:15 Chuhong Yuan
  2019-07-30  0:16 ` Joe Perches
  0 siblings, 1 reply; 6+ messages in thread
From: Chuhong Yuan @ 2019-07-29 15:15 UTC (permalink / raw)
  Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, linux-kernel,
	Chuhong Yuan

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

Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
---
 kernel/printk/braille.c | 4 ++--
 kernel/printk/printk.c  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
index 1d21ebacfdb8..64f0fb8ef27d 100644
--- a/kernel/printk/braille.c
+++ b/kernel/printk/braille.c
@@ -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) {
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 1888f6a3b694..9e60dce4bdd5 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -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;
 	}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 08/12] printk: Replace strncmp with str_has_prefix
  2019-07-29 15:15 [PATCH 08/12] printk: Replace strncmp with str_has_prefix Chuhong Yuan
@ 2019-07-30  0:16 ` Joe Perches
  2019-08-01 15:23   ` Chuhong Yuan
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Perches @ 2019-07-30  0:16 UTC (permalink / raw)
  To: Chuhong Yuan
  Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, linux-kernel

On Mon, 2019-07-29 at 23:15 +0800, Chuhong Yuan wrote:
> strncmp(str, const, len) is error-prone.
> We had better use newly introduced
> str_has_prefix() instead of it.
[]
> diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
[]
> @@ -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...

>  		*str = strchr(*brl_options, ',');
>  		if (!*str) {
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
[]
> @@ -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;
>  	}

here too.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 08/12] printk: Replace strncmp with str_has_prefix
  2019-07-30  0:16 ` Joe Perches
@ 2019-08-01 15:23   ` Chuhong Yuan
  2019-08-01 16:19     ` Joe Perches
  2019-08-01 17:51     ` Joe Perches
  0 siblings, 2 replies; 6+ messages in thread
From: Chuhong Yuan @ 2019-08-01 15:23 UTC (permalink / raw)
  To: Joe Perches; +Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, LKML

Joe Perches <joe@perches.com> 于2019年7月30日周二 上午8:16写道:
>
> On Mon, 2019-07-29 at 23:15 +0800, Chuhong Yuan wrote:
> > strncmp(str, const, len) is error-prone.
> > We had better use newly introduced
> > str_has_prefix() instead of it.
> []
> > diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
> []
> > @@ -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...
>

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

> >               *str = strchr(*brl_options, ',');
> >               if (!*str) {
> > diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> []
> > @@ -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;
> >       }
>
> here too.
>
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 08/12] printk: Replace strncmp with str_has_prefix
  2019-08-01 15:23   ` Chuhong Yuan
@ 2019-08-01 16:19     ` Joe Perches
  2019-08-01 16:40       ` Steven Rostedt
  2019-08-01 17:51     ` Joe Perches
  1 sibling, 1 reply; 6+ messages in thread
From: Joe Perches @ 2019-08-01 16:19 UTC (permalink / raw)
  To: Chuhong Yuan; +Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, LKML

On Thu, 2019-08-01 at 23:23 +0800, Chuhong Yuan wrote:
> Joe Perches <joe@perches.com> 于2019年7月30日周二 上午8:16写道:
> > On Mon, 2019-07-29 at 23:15 +0800, Chuhong Yuan wrote:
> > > strncmp(str, const, len) is error-prone.
> > > We had better use newly introduced
> > > str_has_prefix() instead of it.
> > []
> > > diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
> > []
> > > @@ -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...
> > 
> 
> 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.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 08/12] printk: Replace strncmp with str_has_prefix
  2019-08-01 16:19     ` Joe Perches
@ 2019-08-01 16:40       ` Steven Rostedt
  0 siblings, 0 replies; 6+ messages in thread
From: Steven Rostedt @ 2019-08-01 16:40 UTC (permalink / raw)
  To: Joe Perches; +Cc: Chuhong Yuan, Petr Mladek, Sergey Senozhatsky, LKML

On Thu, 01 Aug 2019 09:19:04 -0700
Joe Perches <joe@perches.com> wrote:

> > 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 08/12] printk: Replace strncmp with str_has_prefix
  2019-08-01 15:23   ` Chuhong Yuan
  2019-08-01 16:19     ` Joe Perches
@ 2019-08-01 17:51     ` Joe Perches
  1 sibling, 0 replies; 6+ messages in thread
From: Joe Perches @ 2019-08-01 17:51 UTC (permalink / raw)
  To: Chuhong Yuan; +Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, LKML

On Thu, 2019-08-01 at 23:23 +0800, Chuhong Yuan wrote:
> Joe Perches <joe@perches.com> 于2019年7月30日周二 上午8:16写道:
> > On Mon, 2019-07-29 at 23:15 +0800, Chuhong Yuan wrote:
> > > strncmp(str, const, len) is error-prone.
> > > We had better use newly introduced
> > > str_has_prefix() instead of it.
> > []
> > > diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
> > []
> > > @@ -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...
> > 
> 
> 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(-)

diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
index 1d21ebacfdb8..46dd9fcc7525 100644
--- a/kernel/printk/braille.c
+++ b/kernel/printk/braille.c
@@ -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;
 }
 


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-08-01 17:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-29 15:15 [PATCH 08/12] printk: Replace strncmp with str_has_prefix Chuhong Yuan
2019-07-30  0:16 ` Joe Perches
2019-08-01 15:23   ` Chuhong Yuan
2019-08-01 16:19     ` Joe Perches
2019-08-01 16:40       ` Steven Rostedt
2019-08-01 17:51     ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).