Skip to contentSkip to conversation

lib/string: Introduce sysfs_streqcase

PATCH In review Gioh Kim 5 participants 9 messages Apr 8, 2021 – Apr 9, 2021 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Gioh Kim

author
#0 lore raw

PATCH v4 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           | 36 ++++++++++++++++++++++++++++--------
 2 files changed, 29 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 +28 -8
index 7548eb715ddb..d0fb02efd5da 100644
@@ -687,6 +687,17 @@ char *strsep(char **s, const char *ct)
 EXPORT_SYMBOL(strsep);
 #endif
 
+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 +714,26 @@ 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);
+
 /**
  * match_string - matches given string in an array
  * @array:	array of strings
Signature · 1 line
2.25.1
Reviewed-by on v4 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".

you should add the

Quoted 1 line

---

you can add the changelog here after the ---
v4->v3: removed #ifdef CONFIG_SYSFS ~ #endif.

The string comparison doesn't depends on CONFIG_SYSFS at all.

It looks good to me.

Quoted 76 lines
 include/linux/string.h |  1 +
 lib/string.c           | 36 ++++++++++++++++++++++++++++--------
 2 files changed, 29 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 +28 -9
index 7548eb715ddb..d0fb02efd5da 100644
@@ -687,6 +687,17 @@ char *strsep(char **s, const char *ct)
 EXPORT_SYMBOL(strsep);
 #endif
 
+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 +714,26 @@ 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);
+
 /**
  * match_string - matches given string in an array
  * @array:     array of strings
--

2.25.1

author
#2 lore raw
Quoted 18 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".

you should add the

Quoted 10 lines
Quoted 1 line

---

you can add the changelog here after the ---
v4->v3: removed #ifdef CONFIG_SYSFS ~ #endif.

The string comparison doesn't depends on CONFIG_SYSFS at all.

It looks good to me.

Yes, I got two build error reports for v3.
Should I send v5 including "Reported-by: kernel test robot <[email protected]>" tag?

Reviewed-by
#3 lore raw
Quoted 12 lines · Gioh Kim
Quoted 10 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
Quoted 6 lines
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".

v4 LGTM, thanks.

Quoted 8 lines
Quoted 8 lines

you should add the

Quoted 1 line

---

you can add the changelog here after the ---
v4->v3: removed #ifdef CONFIG_SYSFS ~ #endif.

The string comparison doesn't depends on CONFIG_SYSFS at all.

Quoted 7 lines
Quoted 4 lines

It looks good to me.

Yes, I got two build error reports for v3.
Should I send v5 including "Reported-by: kernel test robot <[email protected]>" tag?

I don't think that's necessary. I would use that tag if I was fixing an issue reported by the bot; but v4 is basically the same as v2 in regards to the issue 0day bot reported with v3. v3 just demonstrates that there are drivers with possibly incorrect Kconfig dependencies (missing a dependency on SYSFS perhaps). So the underlying problem was not reported by 0day bot; 0day bot just helped avoid issues from v3.

Fixing the Kconfig dependencies would be nice to have, but not a requirement IMO to this feature/functionality.

Signature · 2 lines
Thanks,
~Nick Desaulniers
maintainer
#4 lore raw
In reply to Gioh Kim
Quoted 2 lines · On Thu, 8 Apr 2021 15:06:05 +0200 Gioh Kim <[email protected]> wrote:

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

Peh. Who would die if we simply made sysfs_streq() case-insensitive?

#5 lore raw
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Thu, 8 Apr 2021 15:06:05 +0200 Gioh Kim <[email protected]> wrote:

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

Peh. Who would die if we simply made sysfs_streq() case-insensitive?

I doubt anyone, let's do that instead.

maintainer
#6 lore raw
Quoted 9 lines · On Fri, 9 Apr 2021 08:44:39 +0200 Greg Kroah-Hartman…
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Thu, 8 Apr 2021 15:06:05 +0200 Gioh Kim <[email protected]> wrote:

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

Peh. Who would die if we simply made sysfs_streq() case-insensitive?

I doubt anyone, let's do that instead.

There's a risk that people will write scripts/config/etc on a 5.13+ kernel and then find that they malfunction on earlier kernels...

#7 lore raw
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Fri, 9 Apr 2021 08:44:39 +0200 Greg Kroah-Hartman…
Quoted 5 lines · Andrew Morton
Quoted 2 lines · On Thu, 8 Apr 2021 15:06:05 +0200 Gioh Kim <[email protected]> wrote:

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

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Peh. Who would die if we simply made sysfs_streq() case-insensitive?

I doubt anyone, let's do that instead.

There's a risk that people will write scripts/config/etc on a 5.13+ kernel and then find that they malfunction on earlier kernels...

That's not a regression, that is a "newer kernels have newer features" issue :)

A message that is not in the archive (sent off-list or not yet received): CAHp75VcjNWycV-SLUzzfgtMp8BzXUiUAoz_BYr4+nCazAzsrqw@mail.gmail.com

author
#9 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Andrew Morton

On Thu, 8 Apr 2021 15:06:05 +0200 Gioh Kim <[email protected]> wrote:

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

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

Peh. Who would die if we simply made sysfs_streq() case-insensitive?

I think it will be a disaster. Like we have with case-insensitive file systems. Famous Mac case that most of the soft stop working when they actually moved to the right direction, I.e. case-sensitive. Personally I’m against such change.

Hi Andy,

I am sorry but I am a little bit confused because I don't have any experience with case-insensitive file systems.
I think the case-insensitive file system does not care if sysfs_streq is changed to case-insensitive.

For example, if the case-insensitive file system would accept the "OFF" command, the user can pass either of "off" and "OFF" and both will work with case-insensitive strfs_streq.

On the contrary, I think the case-sensitive file system would not like it if sysfs_streq is made case-insensitive.

Could you please inform me what I am missing?

Quoted 9 lines

--
With Best Regards,
Andy Shevchenko