printk: Replace strncmp() with str_has_prefix()

strncmp(str, const, len) is error-prone because len is easy to have typo.
An 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.

Link: http://lkml.kernel.org/r/20190809071034.17279-1-hslester96@gmail.com
Cc: "Steven Rostedt" <rostedt@goodmis.org>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
Reviewed-by:  Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
[pmladek@suse.com: Slightly updated and reformatted the commit message.]
Signed-off-by: Petr Mladek <pmladek@suse.com>
This commit is contained in:
Chuhong Yuan 2019-08-09 15:10:34 +08:00 committed by Petr Mladek
parent 8ebea6ea1a
commit 35c35493b0
2 changed files with 29 additions and 12 deletions

View File

@ -11,11 +11,18 @@
int _braille_console_setup(char **str, char **brl_options) 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 = ""; *brl_options = "";
*str += 4; *str += len;
} else if (!strncmp(*str, "brl=", 4)) { return 0;
*brl_options = *str + 4; }
len = str_has_prefix(*str, "brl=");
if (len) {
*brl_options = *str + len;
*str = strchr(*brl_options, ','); *str = strchr(*brl_options, ',');
if (!*str) { if (!*str) {
pr_err("need port name after brl=\n"); pr_err("need port name after brl=\n");

View File

@ -118,19 +118,29 @@ static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
static int __control_devkmsg(char *str) static int __control_devkmsg(char *str)
{ {
size_t len;
if (!str) if (!str)
return -EINVAL; return -EINVAL;
if (!strncmp(str, "on", 2)) { len = str_has_prefix(str, "on");
if (len) {
devkmsg_log = DEVKMSG_LOG_MASK_ON; devkmsg_log = DEVKMSG_LOG_MASK_ON;
return 2; return len;
} else if (!strncmp(str, "off", 3)) {
devkmsg_log = DEVKMSG_LOG_MASK_OFF;
return 3;
} else if (!strncmp(str, "ratelimit", 9)) {
devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
return 9;
} }
len = str_has_prefix(str, "off");
if (len) {
devkmsg_log = DEVKMSG_LOG_MASK_OFF;
return len;
}
len = str_has_prefix(str, "ratelimit");
if (len) {
devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
return len;
}
return -EINVAL; return -EINVAL;
} }