[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20190802014718.8952-1-hslester96@gmail.com>
Date: Fri, 2 Aug 2019 09:47:18 +0800
From: Chuhong Yuan <hslester96@...il.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Petr Mladek <pmladek@...e.com>,
Sergey Senozhatsky <sergey.senozhatsky@...il.com>,
Steven Rostedt <rostedt@...dmis.org>,
Joe Perches <joe@...ches.com>, linux-kernel@...r.kernel.org,
Chuhong Yuan <hslester96@...il.com>
Subject: [PATCH v2 06/10] printk: Replace strncmp with str_has_prefix
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.
Signed-off-by: Chuhong Yuan <hslester96@...il.com>
---
Changes in v2:
- Revise the description.
- Utilize str_has_prefix's return value to
eliminate some hard codes.
kernel/printk/braille.c | 10 ++++++----
kernel/printk/printk.c | 14 ++++++++------
2 files changed, 14 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..21b28c7dd18f 100644
--- 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;
}
--
2.20.1
Powered by blists - more mailing lists