lkml.org 
[lkml]   [2019]   [Aug]   [1]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
SubjectRe: [PATCH 08/12] printk: Replace strncmp with str_has_prefix
From
Date
On Thu, 2019-08-01 at 23:23 +0800, Chuhong Yuan wrote:
> Joe Perches <joe@perches.com> 于2019年7月30日周二 上午8:16写道:
> > On Mon, 2019-07-29 at 23:15 +0800, Chuhong Yuan wrote:
> > > strncmp(str, const, len) is error-prone.
> > > We had better use newly introduced
> > > str_has_prefix() instead of it.
> > []
> > > diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
> > []
> > > @@ -11,10 +11,10 @@
> > >
> > > int _braille_console_setup(char **str, char **brl_options)
> > > {
> > > - if (!strncmp(*str, "brl,", 4)) {
> > > + if (str_has_prefix(*str, "brl,")) {
> > > *brl_options = "";
> > > *str += 4;
> > > - } else if (!strncmp(*str, "brl=", 4)) {
> > > + } else if (str_has_prefix(*str, "brl=")) {
> > > *brl_options = *str + 4;
> >
> > Better to get rid of the += 4 uses too by storing the result
> > of str_has_prefix and using that as the addend.
> >
> > Perhaps
> > size_t len;
> >
> > if ((len = str_has_prefix(*str, "brl,"))) {
> > *brl_options = "";
> > *str += len;
> > } else if ((len = str_has_prefix(*str, "brl="))) {
> > etc...
> >
>
> I find that checkpatch.pl forbids assignment in if condition.
> So this seems to be infeasible...

So the code could be rewritten like below:
(though it's trivial as-is)
---
kernel/printk/braille.c | 30 ++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c
index 1d21ebacfdb8..46dd9fcc7525 100644
--- a/kernel/printk/braille.c
+++ b/kernel/printk/braille.c
@@ -11,19 +11,29 @@

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 = strchr(*brl_options, ',');
- if (!*str) {
- pr_err("need port name after brl=\n");
- return -EINVAL;
- }
- *((*str)++) = 0;
+ *str += len;
+ return 0;
+ }
+
+ len = str_has_prefix(*str, "brl=");
+ if (!len)
+ return 0;
+
+ *brl_options = *str + len;
+
+ *str = strchr(*brl_options, ',');
+ if (!*str) {
+ pr_err("need port name after brl=\n");
+ return -EINVAL;
}

+ *((*str)++) = 0;
+
return 0;
}

\
 
 \ /
  Last update: 2019-08-01 19:52    [W:0.213 / U:0.164 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site