lkml.org 
[lkml]   [2018]   [Feb]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH RFC] auxdisplay: charlcd: Fix and clean up handling of x/y commands
On Tue, Feb 27, 2018 at 9:32 PM, Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> 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)

> +{
> + char tmp[PARSE_N_MAX_SIZE + 1];
> + int i;
> +

First of all you need

unsigned int i = 0;

while (s[i] == '0')
i++;

> + 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?

--
With Best Regards,
Andy Shevchenko

\
 
 \ /
  Last update: 2018-02-27 21:52    [W:0.075 / U:2.328 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site