linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix
@ 2019-08-09  7:10 Chuhong Yuan
  2019-08-14 10:49 ` Petr Mladek
  0 siblings, 1 reply; 6+ messages in thread
From: Chuhong Yuan @ 2019-08-09  7:10 UTC (permalink / raw)
  Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, linux-kernel,
	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.

Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
---
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(-)

diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
index 1d21ebacfdb8..17a9591e54ff 100644
--- a/kernel/printk/braille.c
+++ b/kernel/printk/braille.c
@@ -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");
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 1888f6a3b694..43a31015ec93 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -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;
 }
 
-- 
2.20.1


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

* Re: [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix
  2019-08-09  7:10 [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix Chuhong Yuan
@ 2019-08-14 10:49 ` Petr Mladek
  2019-08-15  7:50   ` Sergey Senozhatsky
  0 siblings, 1 reply; 6+ messages in thread
From: Petr Mladek @ 2019-08-14 10:49 UTC (permalink / raw)
  To: Chuhong Yuan; +Cc: Sergey Senozhatsky, Steven Rostedt, linux-kernel

On Fri 2019-08-09 15:10:34, Chuhong Yuan wrote:
> 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.
> 
> Signed-off-by: Chuhong Yuan <hslester96@gmail.com>

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr

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

* Re: [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix
  2019-08-14 10:49 ` Petr Mladek
@ 2019-08-15  7:50   ` Sergey Senozhatsky
  2019-08-15 13:52     ` Petr Mladek
  0 siblings, 1 reply; 6+ messages in thread
From: Sergey Senozhatsky @ 2019-08-15  7:50 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Chuhong Yuan, Sergey Senozhatsky, Steven Rostedt, linux-kernel

On (08/14/19 12:49), Petr Mladek wrote:
> On Fri 2019-08-09 15:10:34, Chuhong Yuan wrote:
> > 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.
> > 
> > Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
> 
> Reviewed-by: Petr Mladek <pmladek@suse.com>

Reviewed-by:  Sergey Senozhatsky <sergey.senozhatsky@gmail.com>

	-ss

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

* Re: [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix
  2019-08-15  7:50   ` Sergey Senozhatsky
@ 2019-08-15 13:52     ` Petr Mladek
  2019-08-15 15:33       ` Chuhong Yuan
  0 siblings, 1 reply; 6+ messages in thread
From: Petr Mladek @ 2019-08-15 13:52 UTC (permalink / raw)
  To: Chuhong Yuan; +Cc: Sergey Senozhatsky, Steven Rostedt, linux-kernel

On Thu 2019-08-15 16:50:33, Sergey Senozhatsky wrote:
> On (08/14/19 12:49), Petr Mladek wrote:
> > On Fri 2019-08-09 15:10:34, Chuhong Yuan wrote:
> > > 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.
> > > 
> > > Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
> > 
> > Reviewed-by: Petr Mladek <pmladek@suse.com>
> 
> Reviewed-by:  Sergey Senozhatsky <sergey.senozhatsky@gmail.com>

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

Best Regards,
Petr

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

* Re: [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix
  2019-08-15 13:52     ` Petr Mladek
@ 2019-08-15 15:33       ` Chuhong Yuan
  2019-08-16  8:27         ` Petr Mladek
  0 siblings, 1 reply; 6+ messages in thread
From: Chuhong Yuan @ 2019-08-15 15:33 UTC (permalink / raw)
  To: Petr Mladek; +Cc: Sergey Senozhatsky, Steven Rostedt, LKML

On Thu, Aug 15, 2019 at 9:52 PM Petr Mladek <pmladek@suse.com> wrote:
>
> On Thu 2019-08-15 16:50:33, Sergey Senozhatsky wrote:
> > On (08/14/19 12:49), Petr Mladek wrote:
> > > On Fri 2019-08-09 15:10:34, Chuhong Yuan wrote:
> > > > 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.
> > > >
> > > > Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
> > >
> > > Reviewed-by: Petr Mladek <pmladek@suse.com>
> >
> > Reviewed-by:  Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
>
> 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!

> Best Regards,
> Petr

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

* Re: [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix
  2019-08-15 15:33       ` Chuhong Yuan
@ 2019-08-16  8:27         ` Petr Mladek
  0 siblings, 0 replies; 6+ messages in thread
From: Petr Mladek @ 2019-08-16  8:27 UTC (permalink / raw)
  To: Chuhong Yuan; +Cc: Sergey Senozhatsky, Steven Rostedt, LKML

On Thu 2019-08-15 23:33:01, Chuhong Yuan wrote:
> On Thu, Aug 15, 2019 at 9:52 PM Petr Mladek <pmladek@suse.com> wrote:
> >
> > On Thu 2019-08-15 16:50:33, Sergey Senozhatsky wrote:
> > > On (08/14/19 12:49), Petr Mladek wrote:
> > > > On Fri 2019-08-09 15:10:34, Chuhong Yuan wrote:
> > > > > 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.
> > > > >
> > > > > Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
> > > >
> > > > Reviewed-by: Petr Mladek <pmladek@suse.com>
> > >
> > > Reviewed-by:  Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> >
> > 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

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

end of thread, other threads:[~2019-08-16  8:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-09  7:10 [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix Chuhong Yuan
2019-08-14 10:49 ` Petr Mladek
2019-08-15  7:50   ` Sergey Senozhatsky
2019-08-15 13:52     ` Petr Mladek
2019-08-15 15:33       ` Chuhong Yuan
2019-08-16  8:27         ` Petr Mladek

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