Skip to contentSkip to conversation

lib/string: Introduce sysfs_streqcase

PATCH Superseded Gioh Kim 2 participants 3 messages Apr 7, 2021 – Apr 8, 2021

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Gioh Kim

author
#0 lore raw

PATCH 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           | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)
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 +23 -0
index 7548eb715ddb..5e6bc0d3d5c6 100644
@@ -714,6 +714,29 @@ bool sysfs_streq(const char *s1, const char *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++;
+	}
+
+	if (*s1 == *s2)
+		return true;
+	if (!*s1 && *s2 == '\n' && !s2[1])
+		return true;
+	if (*s1 == '\n' && !s1[1] && !*s2)
+		return true;
+	return false;
+}
+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 v1 1/1
#1 lore raw
Quoted 4 lines · Gioh Kim

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

Quoted 8 lines

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

I do wonder if these (sysfs_streqcase and sysfs_streq) could or should be conditionally available on CONFIG_SYSFS=y; don't pay for what you don't use (without needing CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y)?

Also, it might be nice to share the second half of the function with sysfs_streq via a new static function. Though it will just get inlined in both for CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y, it might help the compiler if CONFIG_CC_OPTIMIZE_FOR_SIZE=y was instead chosen if the compiler cannot outline/deduplicate the shared code. At the least, there's less duplication between two very similar functions; if one changes then authors may need to be careful to update both.

Are either of those concerns worth a v3? ¯\_(ツ)_/¯

Quoted 56 lines
---
 include/linux/string.h |  1 +
 lib/string.c           | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)
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 +23 -1
index 7548eb715ddb..5e6bc0d3d5c6 100644
@@ -714,6 +714,29 @@ bool sysfs_streq(const char *s1, const char *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++;
+       }
+
+       if (*s1 == *s2)
+               return true;
+       if (!*s1 && *s2 == '\n' && !s2[1])
+               return true;
+       if (*s1 == '\n' && !s1[1] && !*s2)
+               return true;
+       return false;
+}
+EXPORT_SYMBOL(sysfs_streqcase);
+
 /**
  * match_string - matches given string in an array
  * @array:     array of strings
--

2.25.1

Signature · 2 lines
Thanks,
~Nick Desaulniers
author
#2 lore raw
Quoted 15 lines · Nick Desaulniers
Quoted 4 lines · Gioh Kim

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

Quoted 8 lines

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

Quoted 5 lines

I do wonder if these (sysfs_streqcase and sysfs_streq) could or should be conditionally available on CONFIG_SYSFS=y; don't pay for what you don't use (without needing CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y)?

Good idea.
Thank you.

Quoted 8 lines

Also, it might be nice to share the second half of the function with sysfs_streq via a new static function. Though it will just get inlined in both for CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y, it might help the compiler if CONFIG_CC_OPTIMIZE_FOR_SIZE=y was instead chosen if the compiler cannot outline/deduplicate the shared code. At the least, there's less duplication between two very similar functions; if one changes then authors may need to be careful to update both.

Yes, they are exactly the same.
I will make an inline function for the common code.

Quoted 2 lines

Are either of those concerns worth a v3? ¯\_(ツ)_/¯

Sure, I will not forget to add 'V2'.

Thank you for kind review.