All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cpumask: fix double string traverse in cpumask_parse
@ 2019-04-09 20:42 Yury Norov
  2019-04-09 21:31 ` Rasmus Villemoes
  2019-04-09 21:36 ` Andrew Morton
  0 siblings, 2 replies; 4+ messages in thread
From: Yury Norov @ 2019-04-09 20:42 UTC (permalink / raw)
  To: Andrew Morton, Andy Shevchenko, Rasmus Villemoes,
	Amritha Nambiar, David S. Miller, Tejun Heo, Willem de Bruijn
  Cc: Yury Norov, Yury Norov, linux-kernel

From: Yury Norov <ynorov@marvell.com>

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@marvell.com>
---
 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);
 
 	return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpumask: fix double string traverse in cpumask_parse
  2019-04-09 20:42 [PATCH] cpumask: fix double string traverse in cpumask_parse Yury Norov
@ 2019-04-09 21:31 ` Rasmus Villemoes
  2019-04-09 21:39   ` Yury Norov
  2019-04-09 21:36 ` Andrew Morton
  1 sibling, 1 reply; 4+ messages in thread
From: Rasmus Villemoes @ 2019-04-09 21:31 UTC (permalink / raw)
  To: Yury Norov, Andrew Morton, Andy Shevchenko, Amritha Nambiar,
	David S. Miller, Tejun Heo, Willem de Bruijn
  Cc: Yury Norov, linux-kernel

On 09/04/2019 22.42, Yury Norov wrote:
> From: Yury Norov <ynorov@marvell.com>
> 
> 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@marvell.com>
> ---
>  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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpumask: fix double string traverse in cpumask_parse
  2019-04-09 20:42 [PATCH] cpumask: fix double string traverse in cpumask_parse Yury Norov
  2019-04-09 21:31 ` Rasmus Villemoes
@ 2019-04-09 21:36 ` Andrew Morton
  1 sibling, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2019-04-09 21:36 UTC (permalink / raw)
  To: Yury Norov
  Cc: Andy Shevchenko, Rasmus Villemoes, Amritha Nambiar,
	David S. Miller, Tejun Heo, Willem de Bruijn, Yury Norov,
	linux-kernel

On Tue,  9 Apr 2019 13:42:08 -0700 Yury Norov <yury.norov@gmail.com> wrote:

> From: Yury Norov <ynorov@marvell.com>
> 
> 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().

Fair enough.

> --- 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);

Does the cast do anything useful?  The compiler will convert a
ptrdiff_t to uint without issues, I think?

>  	return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
>  }


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpumask: fix double string traverse in cpumask_parse
  2019-04-09 21:31 ` Rasmus Villemoes
@ 2019-04-09 21:39   ` Yury Norov
  0 siblings, 0 replies; 4+ messages in thread
From: Yury Norov @ 2019-04-09 21:39 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Andrew Morton, Andy Shevchenko, Amritha Nambiar, David S. Miller,
	Tejun Heo, Willem de Bruijn, Yury Norov, linux-kernel

On Tue, Apr 09, 2019 at 11:31:50PM +0200, Rasmus Villemoes wrote:
> On 09/04/2019 22.42, Yury Norov wrote:
> > From: Yury Norov <ynorov@marvell.com>
> > 
> > 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@marvell.com>
> > ---
> >  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;"

Indeed. See below.

Thanks,
Yury

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

From: Yury Norov <ynorov@marvell.com>

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@marvell.com>
Acked-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 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 = strchrnul(buf, '\n') - buf;
 
 	return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-04-09 21:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-09 20:42 [PATCH] cpumask: fix double string traverse in cpumask_parse Yury Norov
2019-04-09 21:31 ` Rasmus Villemoes
2019-04-09 21:39   ` Yury Norov
2019-04-09 21:36 ` Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.