2021-04-08 09:35:31

by Gioh Kim

[permalink] [raw]
Subject: [PATCH v3] lib/string: Introduce sysfs_streqcase

As the name shows, it checks if strings are equal in case insensitive
manner.

For example, drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c uses
strncasecmp to check that the input via sysfs is "mi". But it would
work even-if the input is "min-wrongcommand".

I found some more cases using strncasecmp to check the entire string
such as rtrs-clt-sysfs.c does. drivers/pnp/interface.c checks
"disable" command with strncasecmp but it would also work if the
command is "disable-wrong".

Signed-off-by: Gioh Kim <[email protected]>
---
include/linux/string.h | 1 +
lib/string.c | 38 ++++++++++++++++++++++++++++++--------
2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index 4fcfb56abcf5..36d00ff8013e 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -184,6 +184,7 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
extern void argv_free(char **argv);

extern bool sysfs_streq(const char *s1, const char *s2);
+extern bool sysfs_streqcase(const char *s1, const char *s2);
extern int kstrtobool(const char *s, bool *res);
static inline int strtobool(const char *s, bool *res)
{
diff --git a/lib/string.c b/lib/string.c
index 7548eb715ddb..cb53845cc4ac 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -687,6 +687,18 @@ char *strsep(char **s, const char *ct)
EXPORT_SYMBOL(strsep);
#endif

+#ifdef CONFIG_SYSFS
+static inline bool __streq_terminal(const char *s1, const char *s2)
+{
+ if (*s1 == *s2)
+ return true;
+ if (!*s1 && *s2 == '\n' && !s2[1])
+ return true;
+ if (*s1 == '\n' && !s1[1] && !*s2)
+ return true;
+ return false;
+}
+
/**
* sysfs_streq - return true if strings are equal, modulo trailing newline
* @s1: one string
@@ -703,17 +715,27 @@ bool sysfs_streq(const char *s1, const char *s2)
s1++;
s2++;
}
-
- if (*s1 == *s2)
- return true;
- if (!*s1 && *s2 == '\n' && !s2[1])
- return true;
- if (*s1 == '\n' && !s1[1] && !*s2)
- return true;
- return false;
+ return __streq_terminal(s1, s2);
}
EXPORT_SYMBOL(sysfs_streq);

+/**
+ * sysfs_streqcase - same to sysfs_streq and case insensitive
+ * @s1: one string
+ * @s2: another string
+ *
+ */
+bool sysfs_streqcase(const char *s1, const char *s2)
+{
+ while (*s1 && tolower(*s1) == tolower(*s2)) {
+ s1++;
+ s2++;
+ }
+ return __streq_terminal(s1, s2);
+}
+EXPORT_SYMBOL(sysfs_streqcase);
+#endif
+
/**
* match_string - matches given string in an array
* @array: array of strings
--
2.25.1


2021-04-08 10:51:12

by Jinpu Wang

[permalink] [raw]
Subject: Re: [PATCH v3] lib/string: Introduce sysfs_streqcase

On Thu, Apr 8, 2021 at 11:34 AM Gioh Kim <[email protected]> wrote:
>
> As the name shows, it checks if strings are equal in case insensitive
> manner.
>
> For example, drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c uses
> strncasecmp to check that the input via sysfs is "mi". But it would
> work even-if the input is "min-wrongcommand".
>
> I found some more cases using strncasecmp to check the entire string
> such as rtrs-clt-sysfs.c does. drivers/pnp/interface.c checks
> "disable" command with strncasecmp but it would also work if the
> command is "disable-wrong".
>
> Signed-off-by: Gioh Kim <[email protected]>
It looks good to me.
Reviewed-by: Jack Wang <[email protected]>
> ---
> include/linux/string.h | 1 +
> lib/string.c | 38 ++++++++++++++++++++++++++++++--------
> 2 files changed, 31 insertions(+), 8 deletions(-)
>
> diff --git a/include/linux/string.h b/include/linux/string.h
> index 4fcfb56abcf5..36d00ff8013e 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -184,6 +184,7 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
> extern void argv_free(char **argv);
>
> extern bool sysfs_streq(const char *s1, const char *s2);
> +extern bool sysfs_streqcase(const char *s1, const char *s2);
> extern int kstrtobool(const char *s, bool *res);
> static inline int strtobool(const char *s, bool *res)
> {
> diff --git a/lib/string.c b/lib/string.c
> index 7548eb715ddb..cb53845cc4ac 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -687,6 +687,18 @@ char *strsep(char **s, const char *ct)
> EXPORT_SYMBOL(strsep);
> #endif
>
> +#ifdef CONFIG_SYSFS
> +static inline bool __streq_terminal(const char *s1, const char *s2)
> +{
> + if (*s1 == *s2)
> + return true;
> + if (!*s1 && *s2 == '\n' && !s2[1])
> + return true;
> + if (*s1 == '\n' && !s1[1] && !*s2)
> + return true;
> + return false;
> +}
> +
> /**
> * sysfs_streq - return true if strings are equal, modulo trailing newline
> * @s1: one string
> @@ -703,17 +715,27 @@ bool sysfs_streq(const char *s1, const char *s2)
> s1++;
> s2++;
> }
> -
> - if (*s1 == *s2)
> - return true;
> - if (!*s1 && *s2 == '\n' && !s2[1])
> - return true;
> - if (*s1 == '\n' && !s1[1] && !*s2)
> - return true;
> - return false;
> + return __streq_terminal(s1, s2);
> }
> EXPORT_SYMBOL(sysfs_streq);
>
> +/**
> + * sysfs_streqcase - same to sysfs_streq and case insensitive
> + * @s1: one string
> + * @s2: another string
> + *
> + */
> +bool sysfs_streqcase(const char *s1, const char *s2)
> +{
> + while (*s1 && tolower(*s1) == tolower(*s2)) {
> + s1++;
> + s2++;
> + }
> + return __streq_terminal(s1, s2);
> +}
> +EXPORT_SYMBOL(sysfs_streqcase);
> +#endif
> +
> /**
> * match_string - matches given string in an array
> * @array: array of strings
> --
> 2.25.1
>

2021-04-08 12:39:04

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v3] lib/string: Introduce sysfs_streqcase

Hi Gioh,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linux/master]
[also build test ERROR on kees/for-next/pstore linus/master v5.12-rc6 next-20210407]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Gioh-Kim/lib-string-Introduce-sysfs_streqcase/20210408-173449
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 5e46d1b78a03d52306f21f77a4e4a144b6d31486
config: nds32-allnoconfig (attached as .config)
compiler: nds32le-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/7de114025f207fa41a9cffdf91c28e9b914e09f4
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Gioh-Kim/lib-string-Introduce-sysfs_streqcase/20210408-173449
git checkout 7de114025f207fa41a9cffdf91c28e9b914e09f4
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=nds32

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

nds32le-linux-ld: drivers/base/core.o: in function `device_find_child_by_name':
core.c:(.text+0x2e4c): undefined reference to `sysfs_streq'
>> nds32le-linux-ld: core.c:(.text+0x2e50): undefined reference to `sysfs_streq'
nds32le-linux-ld: drivers/base/core.o: in function `device_match_name':
core.c:(.text+0x35a0): undefined reference to `sysfs_streq'
nds32le-linux-ld: core.c:(.text+0x35a4): undefined reference to `sysfs_streq'
nds32le-linux-ld: lib/string.o: in function `__sysfs_match_string':
string.c:(.text+0x668): undefined reference to `sysfs_streq'
nds32le-linux-ld: lib/string.o:string.c:(.text+0x66c): more undefined references to `sysfs_streq' follow

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (2.27 kB)
.config.gz (5.10 kB)
Download all attachments

2021-04-08 13:09:10

by Gioh Kim

[permalink] [raw]
Subject: Re: [PATCH v3] lib/string: Introduce sysfs_streqcase

Hi Jinpu,

I removed #ifdef CONFIG_SYSFS ~ #endif.
Could you please review again?

On Thu, Apr 8, 2021 at 12:50 PM Jinpu Wang <[email protected]> wrote:
>
> On Thu, Apr 8, 2021 at 11:34 AM Gioh Kim <[email protected]> wrote:
> >
> > As the name shows, it checks if strings are equal in case insensitive
> > manner.
> >
> > For example, drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c uses
> > strncasecmp to check that the input via sysfs is "mi". But it would
> > work even-if the input is "min-wrongcommand".
> >
> > I found some more cases using strncasecmp to check the entire string
> > such as rtrs-clt-sysfs.c does. drivers/pnp/interface.c checks
> > "disable" command with strncasecmp but it would also work if the
> > command is "disable-wrong".
> >
> > Signed-off-by: Gioh Kim <[email protected]>
> It looks good to me.
> Reviewed-by: Jack Wang <[email protected]>
> > ---
> > include/linux/string.h | 1 +
> > lib/string.c | 38 ++++++++++++++++++++++++++++++--------
> > 2 files changed, 31 insertions(+), 8 deletions(-)
> >
> > diff --git a/include/linux/string.h b/include/linux/string.h
> > index 4fcfb56abcf5..36d00ff8013e 100644
> > --- a/include/linux/string.h
> > +++ b/include/linux/string.h
> > @@ -184,6 +184,7 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
> > extern void argv_free(char **argv);
> >
> > extern bool sysfs_streq(const char *s1, const char *s2);
> > +extern bool sysfs_streqcase(const char *s1, const char *s2);
> > extern int kstrtobool(const char *s, bool *res);
> > static inline int strtobool(const char *s, bool *res)
> > {
> > diff --git a/lib/string.c b/lib/string.c
> > index 7548eb715ddb..cb53845cc4ac 100644
> > --- a/lib/string.c
> > +++ b/lib/string.c
> > @@ -687,6 +687,18 @@ char *strsep(char **s, const char *ct)
> > EXPORT_SYMBOL(strsep);
> > #endif
> >
> > +#ifdef CONFIG_SYSFS
> > +static inline bool __streq_terminal(const char *s1, const char *s2)
> > +{
> > + if (*s1 == *s2)
> > + return true;
> > + if (!*s1 && *s2 == '\n' && !s2[1])
> > + return true;
> > + if (*s1 == '\n' && !s1[1] && !*s2)
> > + return true;
> > + return false;
> > +}
> > +
> > /**
> > * sysfs_streq - return true if strings are equal, modulo trailing newline
> > * @s1: one string
> > @@ -703,17 +715,27 @@ bool sysfs_streq(const char *s1, const char *s2)
> > s1++;
> > s2++;
> > }
> > -
> > - if (*s1 == *s2)
> > - return true;
> > - if (!*s1 && *s2 == '\n' && !s2[1])
> > - return true;
> > - if (*s1 == '\n' && !s1[1] && !*s2)
> > - return true;
> > - return false;
> > + return __streq_terminal(s1, s2);
> > }
> > EXPORT_SYMBOL(sysfs_streq);
> >
> > +/**
> > + * sysfs_streqcase - same to sysfs_streq and case insensitive
> > + * @s1: one string
> > + * @s2: another string
> > + *
> > + */
> > +bool sysfs_streqcase(const char *s1, const char *s2)
> > +{
> > + while (*s1 && tolower(*s1) == tolower(*s2)) {
> > + s1++;
> > + s2++;
> > + }
> > + return __streq_terminal(s1, s2);
> > +}
> > +EXPORT_SYMBOL(sysfs_streqcase);
> > +#endif
> > +
> > /**
> > * match_string - matches given string in an array
> > * @array: array of strings
> > --
> > 2.25.1
> >

2021-04-08 14:04:44

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v3] lib/string: Introduce sysfs_streqcase

Hi Gioh,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linux/master]
[also build test ERROR on kees/for-next/pstore linus/master v5.12-rc6 next-20210408]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Gioh-Kim/lib-string-Introduce-sysfs_streqcase/20210408-173449
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 5e46d1b78a03d52306f21f77a4e4a144b6d31486
config: s390-randconfig-r036-20210408 (attached as .config)
compiler: s390-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/7de114025f207fa41a9cffdf91c28e9b914e09f4
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Gioh-Kim/lib-string-Introduce-sysfs_streqcase/20210408-173449
git checkout 7de114025f207fa41a9cffdf91c28e9b914e09f4
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=s390

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

s390-linux-ld: drivers/dma/altera-msgdma.o: in function `request_and_map':
altera-msgdma.c:(.text+0x408): undefined reference to `devm_ioremap'
s390-linux-ld: drivers/dma/idma64.o: in function `idma64_platform_probe':
idma64.c:(.text+0x1c0a): undefined reference to `devm_ioremap_resource'
s390-linux-ld: drivers/dma/sf-pdma/sf-pdma.o: in function `sf_pdma_probe':
sf-pdma.c:(.text+0xc64): undefined reference to `devm_ioremap_resource'
s390-linux-ld: drivers/dma/qcom/hidma.o: in function `hidma_probe':
hidma.c:(.text+0xd64): undefined reference to `devm_ioremap_resource'
s390-linux-ld: hidma.c:(.text+0xdc8): undefined reference to `devm_ioremap_resource'
s390-linux-ld: drivers/dma/xilinx/xilinx_dpdma.o: in function `xilinx_dpdma_probe':
xilinx_dpdma.c:(.text+0x20be): undefined reference to `devm_platform_ioremap_resource'
s390-linux-ld: drivers/base/core.o: in function `device_find_child_by_name':
core.c:(.text+0xb38e): undefined reference to `sysfs_streq'
s390-linux-ld: drivers/base/core.o: in function `device_match_name':
core.c:(.text+0x11a22): undefined reference to `sysfs_streq'
s390-linux-ld: drivers/base/memory.o: in function `auto_online_blocks_store':
>> memory.c:(.text+0x184): undefined reference to `sysfs_streq'
s390-linux-ld: drivers/base/memory.o: in function `state_store':
memory.c:(.text+0x368): undefined reference to `sysfs_streq'
s390-linux-ld: drivers/base/memory.o: in function `mhp_online_type_from_str':
memory.c:(.text+0xc20): undefined reference to `sysfs_streq'
s390-linux-ld: lib/string.o:string.c:(.text+0xab6): more undefined references to `sysfs_streq' follow

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (3.25 kB)
.config.gz (10.75 kB)
Download all attachments