Re: [PATCH] cpumask: fix double string traverse in cpumask_parse

From: Rasmus Villemoes
Date: Tue Apr 09 2019 - 17:31:55 EST


On 09/04/2019 22.42, Yury Norov wrote:
> From: Yury Norov <ynorov@xxxxxxxxxxx>
>
> cpumask_parse() finds first occurrence of either \n or \0 by calling
> strchr() and strlen(). We can do it better with a single call of
> strchrnul().
>
> Signed-off-by: Yury Norov <ynorov@xxxxxxxxxxx>
> ---
> include/linux/cpumask.h | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
> index 147bdec42215..2b87f35c586c 100644
> --- a/include/linux/cpumask.h
> +++ b/include/linux/cpumask.h
> @@ -633,8 +633,7 @@ static inline int cpumask_parselist_user(const char __user *buf, int len,
> */
> static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
> {
> - char *nl = strchr(buf, '\n');
> - unsigned int len = nl ? (unsigned int)(nl - buf) : strlen(buf);
> + unsigned int len = (unsigned int)(strchrnul(buf, '\n') - buf);

ack, but please drop the cast. "len = strchrnul(buf, '\n') - buf;"

Rasmus