Skip to contentSkip to conversation

lib/string: Introduce sysfs_streqcase

PATCH Superseded Gioh Kim 3 participants 5 messages Apr 8, 2021

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Gioh Kim

author
#0 lore raw

PATCH v3 1/1 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".

---
 include/linux/string.h |  1 +
 lib/string.c           | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 31 insertions(+), 8 deletions(-)
include/linux/string.h +1 -0
index 4fcfb56abcf5..36d00ff8013e 100644
@@ -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)
 {
lib/string.c +30 -8
index 7548eb715ddb..cb53845cc4ac 100644
@@ -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
Signature · 1 line
2.25.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 8 lines · Gioh Kim

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".

Quoted 6 lines

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".

It looks good to me.

Quoted 79 lines
---
 include/linux/string.h |  1 +
 lib/string.c           | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 31 insertions(+), 8 deletions(-)
include/linux/string.h +1 -0
index 4fcfb56abcf5..36d00ff8013e 100644
@@ -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)
 {
lib/string.c +30 -9
index 7548eb715ddb..cb53845cc4ac 100644
@@ -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

author
#2 lore raw

Hi Jinpu,

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

Quoted 97 lines · Jinpu Wang
Quoted 8 lines · Gioh Kim

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".

Quoted 6 lines

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".

It looks good to me.

Quoted 79 lines
---
 include/linux/string.h |  1 +
 lib/string.c           | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 31 insertions(+), 8 deletions(-)
include/linux/string.h +1 -0
index 4fcfb56abcf5..36d00ff8013e 100644
@@ -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)
 {
lib/string.c +30 -9
index 7548eb715ddb..cb53845cc4ac 100644
@@ -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

#3 lore raw
In reply to Gioh Kim
Thank you for the patch! Yet something to improve:

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

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/kbuild-all(a)lists.01.org

#4 lore raw
In reply to Gioh Kim
Thank you for the patch! Yet something to improve:

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

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/kbuild-all(a)lists.01.org