Re: [PATCH v3 2/8] lib: add "on" and "off" to strtobool
From: Kees Cook
Date: Fri Dec 11 2015 - 13:50:27 EST
On Fri, Dec 11, 2015 at 9:00 AM, Andy Shevchenko
<andy.shevchenko@xxxxxxxxx> wrote:
> On Wed, Dec 9, 2015 at 11:43 PM, Kees Cook <keescook@xxxxxxxxxxxx> wrote:
>> Several places in the kernel expect to use "on" and "off" for their
>> boolean signifiers, so add them to strtobool.
>>
>> Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>
>> Cc: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
>> Cc: Daniel Borkmann <daniel@xxxxxxxxxxxxx>
>> ---
>> lib/string.c | 21 ++++++++++++++++++++-
>> 1 file changed, 20 insertions(+), 1 deletion(-)
>>
>> diff --git a/lib/string.c b/lib/string.c
>> index 0323c0d5629a..d7550432f91c 100644
>> --- a/lib/string.c
>> +++ b/lib/string.c
>> @@ -635,12 +635,16 @@ EXPORT_SYMBOL(sysfs_streq);
>> * @s: input string
>> * @res: result
>> *
>> - * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
>> + * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
>> + * 'Oo' when the second character is one of 'fFnN' (for "on" and "off").
>
> Maybe
>
> âor [Oo][FfNn] for "off" and "on"â
Sure! That's more readable.
>> * Otherwise it will return -EINVAL. Value pointed to by res is
>> * updated upon finding a match.
>> */
>> int strtobool(const char *s, bool *res)
>> {
>
>> + if (!s)
>> + return -EINVAL;
>> +
>
> This change I think is better to do separately. Do we have even need for it?
I'm happy to separate it, sure. I added it here because several of the
__setup and param callers do a check for !NULL input, and it made this
cleaner. Also it seems sensible to do this check anyway.
>> switch (s[0]) {
>> case 'y':
>> case 'Y':
>> @@ -652,6 +656,21 @@ int strtobool(const char *s, bool *res)
>> case '0':
>> *res = false;
>> break;
>> + case 'o':
>> + case 'O':
>> + switch (s[1]) {
>> + case 'n':
>> + case 'N':
>> + *res = true;
>> + break;
>> + case 'f':
>> + case 'F':
>> + *res = false;
>> + break;
>
>
>> + default:
>> + return -EINVAL;
>> + }
>> + break;
>> default:
>> return -EINVAL;
>
> Maybe in both cases
> default:
> break;
> }
> â
> }
> return -EINVAL;
I went back and forth on this. To switch to the fall-back being EINVAL
meant I had to change all the other "breaks" into "return 0", and it
just looked ugly to me. If that is preferred, though, I'm happy to do
it.
Thanks!
-Kees
--
Kees Cook
Chrome OS & Brillo Security
--
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/