lib/string: Introduce sysfs_streqcase
Subsystems (2)
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
@@ -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
@@ -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
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.cuses 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.cdoes.drivers/pnp/interface.cchecks "disable" command with strncasecmp but it would also work if the command is "disable-wrong".Signed-off-by Gioh Kim <[email protected]>
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@@ -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@@ -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
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.cuses 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.cdoes.drivers/pnp/interface.cchecks "disable" command with strncasecmp but it would also work if the command is "disable-wrong".Signed-off-by Gioh Kim <[email protected]>you should add the
Reported-by kernel test robot <[email protected]>
Quoted 10 lines
Quoted 1 line
---
you can add the changelog here after the ---
v4->v3: removed #ifdefCONFIG_SYSFS~ #endif.The string comparison doesn't depends on
CONFIG_SYSFSat all.It looks good to me.
Reviewed-by Jack Wang <[email protected]>
Yes, I got two build error reports for v3.
Should I send v5 including "Reported-by: kernel test robot <[email protected]>" tag?
Nick Desaulniers
Reviewed-byQuoted 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.cuses 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.cdoes.drivers/pnp/interface.cchecks "disable" command with strncasecmp but it would also work if the command is "disable-wrong".Signed-off-by Gioh Kim <[email protected]>
v4 LGTM, thanks.
Quoted 8 lines
Quoted 8 lines
you should add the
Reported-by kernel test robot <[email protected]>Quoted 1 line
---
you can add the changelog here after the ---
v4->v3: removed #ifdefCONFIG_SYSFS~ #endif.The string comparison doesn't depends on
CONFIG_SYSFSat all.
Quoted 7 lines
Quoted 4 lines
It looks good to me.
Reviewed-by Jack Wang <[email protected]>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
Andrew Morton
maintainerQuoted 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?
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.
Andrew Morton
maintainerQuoted 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...
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/etcon 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
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