All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Jagdish Gediya <jvgediya@linux.ibm.com>
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	ying.huang@intel.com, dave.hansen@intel.com,
	Andrew Morton <akpm@linux-foundation.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Richard Fitzgerald <rf@opensource.cirrus.com>
Subject: Re: [PATCH v2] lib/kstrtox.c: Add "false"/"true" support to kstrtobool
Date: Tue, 26 Apr 2022 15:48:44 +0200	[thread overview]
Message-ID: <Ymf4PNfLcYcf1btz@alley> (raw)
In-Reply-To: <YmfDiO6KSRzo8C6e@li-6e1fa1cc-351b-11b2-a85c-b897023bb5f3.ibm.com>

On Tue 2022-04-26 15:33:52, Jagdish Gediya wrote:
> On Tue, Apr 26, 2022 at 11:39:57AM +0200, Petr Mladek wrote:
> > On Tue 2022-04-26 12:10:01, Jagdish Gediya wrote:
> > > At many places in kernel, It is necessary to convert sysfs input
> > > to corrosponding bool value e.g. "false" or "0" need to be converted
> > > to bool false, "true" or "1" need to be converted to bool true,
> > > places where such conversion is needed currently check the input
> > > string manually, kstrtobool can be utilized at such places but
> > > currently kstrtobool doesn't have support to "false"/"true".
> > >
> > > Add "false"/"true" support to kstrtobool while string conversion
> > > to bool. Modify existing manual sysfs conversions to use kstrtobool().
> > 
> > It looks reasonable. I would just do it slightly other way, see
> > below.
> > 
> > > This patch doesn't have any functionality change.
> > 
> > This is not true. All kstrtobool() callers will react differently
> > on the "true"/"false" input.
> 
> how? Is it related to performance as more characters are compared?
> otherwise semantic wise they will get the expected response, correct?

kstrtobool() returned -EINVAL for "true"/"false" strings before this
patch. It will successfully handle them after this patch.
This is a behavior/functional change that will affect all
existing kstrtobool() callers.

The change makes sense and most likely will not cause any regression.
But are you 100% sure? People do crazy things.

> > > --- a/lib/kstrtox.c
> > > +++ b/lib/kstrtox.c
> > > @@ -377,6 +377,13 @@ int kstrtobool(const char *s, bool *res)
> > >  		}
> > >  		break;
> > >  	default:
> > > +		if (!strncmp(s, "true", 4)) {
> > > +			*res = true;
> > > +			return 0;
> > > +		} else if (!strncmp(s, "false", 5)) {
> > > +			*res = false;
> > > +			return 0;
> > 
> > It should be enough to check the first letter like we do in
> > the other cases. I mean to set true when s[0] is 'T' or 't'
> > and false when s[0] is 'F' or 'f'.
> 
> For "on" and "off", 2 characters are matched, so is it good enough
> to compare only single character for strings "true" and "false"?

Yes, the 1st character is enough to distinguish "true" and "false".
Two characters are needed for "on" and "off" because the 1st
character is the same.

Best Regards,
Petr

  reply	other threads:[~2022-04-26 13:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-26  6:40 [PATCH v2] lib/kstrtox.c: Add "false"/"true" support to kstrtobool Jagdish Gediya
2022-04-26  9:39 ` Petr Mladek
2022-04-26 10:03   ` Jagdish Gediya
2022-04-26 13:48     ` Petr Mladek [this message]
2022-04-26 13:27 ` Andy Shevchenko
2022-04-26 14:20   ` Jagdish Gediya
2022-04-27 13:50     ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Ymf4PNfLcYcf1btz@alley \
    --to=pmladek@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=dave.hansen@intel.com \
    --cc=jvgediya@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rf@opensource.cirrus.com \
    --cc=ying.huang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.