Re: [PATCH v2] lib: parser: optimize match_NUMER apis to use local array

From: Eric Biggers
Date: Wed Jan 11 2023 - 02:07:54 EST


On Wed, Jan 11, 2023 at 12:22:15PM +0800, Li Lingfeng wrote:
> /**
> * match_one - Determines if a string matches a simple pattern
> * @s: the string to examine for presence of the pattern
> @@ -129,14 +138,13 @@ EXPORT_SYMBOL(match_token);
> static int match_number(substring_t *s, int *result, int base)
> {
> char *endp;
> - char *buf;
> + char buf[NUMBER_BUF_LEN];
> int ret;
> long val;
>
> - buf = match_strdup(s);
> - if (!buf)
> - return -ENOMEM;
> -
> + if ((s->to - s->from) >= NUMBER_BUF_LEN)
> + return -ERANGE;
> + match_strlcpy(buf, s, NUMBER_BUF_LEN);

Instead of doing '(s->to - s->from) >= NUMBER_BUF_LEN', it would be simpler to
check the return value of match_strlcpy():

if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >= NUMBER_BUF_LEN)
return -ERANGE;

Likewise in match_u64int() and match_uint().

- Eric