lib/string: Introduce sysfs_streqcase
Subsystems (2)
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
@@ -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
@@ -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
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.cuses 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.cdoes.drivers/pnp/interface.cchecks "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
Signed-off-by Gioh Kim <[email protected]>--- include/linux/string.h | 1 + lib/string.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+)include/linux/string.h +1
-0@@ -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@@ -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
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.cuses 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.cdoes.drivers/pnp/interface.cchecks "disable" command with strncasecmp but it would also work if the command is "disable-wrong".
Quoted 5 lines
Reviewed-by Nick Desaulniers <[email protected]>I do wonder if these (
sysfs_streqcaseandsysfs_streq) could or should be conditionally available onCONFIG_SYSFS=y; don't pay for what you don't use (without needingCONFIG_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_streqvia a new static function. Though it will just get inlined in both forCONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y, it might help the compiler ifCONFIG_CC_OPTIMIZE_FOR_SIZE=ywas 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.