linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 4/8] printk: Replace strncmp with str_has_prefix
@ 2019-08-05 12:22 Chuhong Yuan
  2019-08-06  8:07 ` Geert Uytterhoeven
  0 siblings, 1 reply; 2+ messages in thread
From: Chuhong Yuan @ 2019-08-05 12:22 UTC (permalink / raw)
  Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Joe Perches,
	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 v3:
  - Revise the description.
  - Remove else uses in printk.c.

 kernel/printk/braille.c | 10 ++++++----
 kernel/printk/printk.c  | 19 +++++++++++++------
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
index 1d21ebacfdb8..e451b8b1d3d5 100644
--- a/kernel/printk/braille.c
+++ b/kernel/printk/braille.c
@@ -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");
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 1888f6a3b694..6b8d9cfebc0b 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -118,19 +118,26 @@ 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;
+	}
+
+	if ((len = str_has_prefix(str, "off"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_OFF;
-		return 3;
-	} else if (!strncmp(str, "ratelimit", 9)) {
+		return len;
+	}
+
+	if ((len = str_has_prefix(str, "ratelimit"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
-		return 9;
+		return len;
 	}
+
 	return -EINVAL;
 }
 
-- 
2.20.1


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

* Re: [PATCH v3 4/8] printk: Replace strncmp with str_has_prefix
  2019-08-05 12:22 [PATCH v3 4/8] printk: Replace strncmp with str_has_prefix Chuhong Yuan
@ 2019-08-06  8:07 ` Geert Uytterhoeven
  0 siblings, 0 replies; 2+ messages in thread
From: Geert Uytterhoeven @ 2019-08-06  8:07 UTC (permalink / raw)
  To: Chuhong Yuan
  Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Joe Perches,
	Linux Kernel Mailing List

Hi Chuhong,

On Mon, Aug 5, 2019 at 2:24 PM Chuhong Yuan <hslester96@gmail.com> 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>

Thanks for your patch!

> --- a/kernel/printk/braille.c
> +++ b/kernel/printk/braille.c
> @@ -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,"))) {

Please write this as

    len = str_has_prefix(*str, "brl,");
    if (len) {

(everywhere)

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-05 12:22 [PATCH v3 4/8] printk: Replace strncmp with str_has_prefix Chuhong Yuan
2019-08-06  8:07 ` Geert Uytterhoeven

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