cpumask: fix double string traverse in cpumask_parse
Yury Norov
maintainerPATCH 1/1 cpumask: fix double string traverse in cpumask_parse
From: Yury Norov <[email protected]>
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().
--- include/linux/cpumask.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/cpumask.h +1 -2
@@ -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); }
Signature · 1 line
2.17.1
Rasmus Villemoes
reviewerQuoted 16 lines · Yury Norov
From: Yury Norov <[email protected]>
cpumask_parse()finds first occurrence of either \n or \0 by callingstrchr()andstrlen(). We can do it better with a single call ofstrchrnul().Signed-off-by Yury Norov <[email protected]>--- include/linux/cpumask.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)include/linux/cpumask.h +0
-0
include/linux/cpumask.h static inline int cpumask_parselist_user(const char __user *buf, int len, · line 636
Quoted 6 lines
include/linux/cpumask.h +1
-2@@ -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
Quoted 12 lines · Rasmus Villemoes
Quoted 11 lines · Yury Norov
From: Yury Norov <[email protected]>
cpumask_parse()finds first occurrence of either \n or \0 by callingstrchr()andstrlen(). We can do it better with a single call ofstrchrnul().Signed-off-by Yury Norov <[email protected]>--- include/linux/cpumask.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 13 lines
include/linux/cpumask.h static inline int cpumask_parselist_user(const char __user *buf, int len, · line 636
Quoted 11 lines
include/linux/cpumask.h +1
-2@@ -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 <[email protected]>
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().
--- include/linux/cpumask.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/cpumask.h +1 -2
@@ -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); }
Signature · 1 line
2.17.1
Quoted 5 lines · On Tue, 9 Apr 2019 13:42:08 -0700 Yury Norov <[email protected]> wrote:
From: Yury Norov <[email protected]>
cpumask_parse()finds first occurrence of either \n or \0 by callingstrchr()andstrlen(). We can do it better with a single call ofstrchrnul().
Fair enough.
include/linux/cpumask.h static inline int cpumask_parselist_user(const char __user *buf, int len, · line 636
Quoted 9 lines
include/linux/cpumask.h +1
-2@@ -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?
Quoted 2 lines
include/linux/cpumask.h +0
-0return bitmap_parse (buf, len,cpumask_bits (dstp), nr_cpumask_bits); }