Re: [PATCH RFC] auxdisplay: charlcd: Fix and clean up handling of x/y commands
From: Miguel Ojeda
Date: Tue Feb 27 2018 - 16:12:54 EST
On Tue, Feb 27, 2018 at 9:52 PM, Andy Shevchenko
<andy.shevchenko@xxxxxxxxx> wrote:
> On Tue, Feb 27, 2018 at 9:32 PM, Miguel Ojeda
> <miguel.ojeda.sandonis@xxxxxxxxx> wrote:
>> The current version is not parsing multiple x/y commands as the code
>> originally intended. On top of that, kstrtoul() expects
>> NULL-terminated strings. Finally, the code had to do two passes over
>> the string, while now only one is done.
>>
>> Some explanations about the supported syntax are added as well.
>
> Something like this would work, but see my comments below.
>
>> +/*
>> + * Parses a base 10 number from a string, until a non-digit number is found or
>> + * until PARSE_N_MAX_SIZE digits.
>> + *
>> + * See kstrtoul() for the meaning of the return value.
>> + * It also returns the next character in the string, i.e. the first non-digit.
>> + */
>> +#define PARSE_N_MAX_SIZE (3)
>> +static unsigned long parse_n(const char *s, unsigned long *res,
>> + const char **next_s)
>
> static int parse_n(const char *s, unsigned long *res, const char **next_s)
Oops, thanks!
>
>> +{
>> + char tmp[PARSE_N_MAX_SIZE + 1];
>> + int i;
>> +
>
> First of all you need
>
> unsigned int i = 0;
>
> while (s[i] == '0')
> i++;
Why?
>
>> + for (i = 0; ; ++i) {
>> + if (!isdigit(s[i]))
>> + break;
>> + if (i >= PARSE_N_MAX_SIZE)
>> + return -EINVAL;
>> + tmp[i] = s[i];
>> + }
>
> And here you partially open coded what simple_strto*() does.
>
>> +
>> + tmp[i] = 0;
>> + *next_s = &s[i];
>> + return kstrtoul(tmp, 10, res);
>> +}
>
> It's over engineered. Just simple use simple_strto*() and that's all.
> Do you really care about overflows? Is those numbers somehow critical?
> What will go wrong?
We shouldn't use simple_strto*() since it is deprecated. So parse_n()
can be either this wrapper to call kstrto*() (which indeed is
overengineered, but easier to understand than Robert's patch that
modified the original sequence, and can be replaced by an inplace
version whenever we have one) or we just put a simple loop for the
moment (like the one Willy coded in 2008). Both are fine with me.
Cheers,
Miguel
>
> --
> With Best Regards,
> Andy Shevchenko