Re: [PATCH 1/3] Add a kstrtobool function matching semantics of existingin kernel equivalents.

From: Jonathan Cameron
Date: Wed Mar 23 2011 - 12:14:40 EST


On 03/23/11 16:01, Greg KH wrote:
> On Wed, Mar 23, 2011 at 05:30:11PM +0200, Alexey Dobriyan wrote:
>> On Wed, Mar 23, 2011 at 3:39 PM, Jonathan Cameron <jic23@xxxxxxxxx> wrote:
>>> +int kstrtobool(const char *s, bool *res)
>>> +{
>>> + switch (s[0]) {
>>> + case 'y':
>>> + case 'Y':
>>> + case '1':
>>> + *res = true;
>>> + case 'n':
>>> + case 'N':
>>> + case '0':
>>> + *res = false;
>>> + default:
>>> + return -EINVAL;
>>> + }
>>> + return 0;
>>> +}
>>
>> sigh... such simple thing and so many bugs
Yeah, not by best work.
>>
>> The only values such function should accept is 0 and 1.
>
> Why? That's not the way the existing kernel functions that use this
> work.
>
>> Have you read the rest of kstrto*() code?
>> Where is newline check?
There are plenty of nastier cases that get through than a newline
in the middle of the string (ybobsyouruncle -> 1 nyes->0 :)
>>
>> Anyway, I think it's better do not exist.
>
> I think it is, as it's already duplicated in at least 2 different places
> in the kernel, and probably more. Once we get this implementation
> working correctly, we don't need to rewrite it again.
Perhaps naming it like this is a bad idea. It manages to imply that it
has the same level of strict checking which is seen in the other kstrto*
functions - which is self evidently not true!

The alternative is to try and pin down future interfaces to a narrower set
of 'true' and 'false' values. We can't realistically change this pair,
(even to 'fix' them) but maybe we can ensure future versions only take 0 or 1?
That sort of simple convention would make life simpler!

Jonathan

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/