From: Joe Perches <joe@perches.com>
To: Chuhong Yuan <hslester96@gmail.com>
Cc: Petr Mladek <pmladek@suse.com>,
Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
Steven Rostedt <rostedt@goodmis.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 06/10] printk: Replace strncmp with str_has_prefix
Date: Thu, 01 Aug 2019 20:13:06 -0700 [thread overview]
Message-ID: <df75fa45950ab7bb67da52fe4b711209790e52ef.camel@perches.com> (raw)
In-Reply-To: <20190802014718.8952-1-hslester96@gmail.com>
On Fri, 2019-08-02 at 09:47 +0800, 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.
[]
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
[]
> @@ -118,18 +118,20 @@ 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;
> + } else if ((len = str_has_prefix(str, "off"))) {
> devkmsg_log = DEVKMSG_LOG_MASK_OFF;
> - return 3;
> - } else if (!strncmp(str, "ratelimit", 9)) {
> + return len;
> + } else if ((len = str_has_prefix(str, "ratelimit"))) {
> devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
> - return 9;
> + return len;
> }
> return -EINVAL;
> }
All of the else uses above could also be removed.
prev parent reply other threads:[~2019-08-02 3:13 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-02 1:47 [PATCH v2 06/10] printk: Replace strncmp with str_has_prefix Chuhong Yuan
2019-08-02 3:13 ` Joe Perches [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=df75fa45950ab7bb67da52fe4b711209790e52ef.camel@perches.com \
--to=joe@perches.com \
--cc=hslester96@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=sergey.senozhatsky@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox