Skip to contentSkip to conversation

cpumask: fix double string traverse in cpumask_parse

PATCH In review Yury Norov 3 participants 4 messages Apr 9, 2019 Bitmap API

lore
Share & tools
lore

Yury Norov

author maintainer
#0 lore raw

PATCH 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
index 147bdec42215..2b87f35c586c 100644
@@ -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
#1 lore raw
Quoted 16 lines · Yury Norov

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 +0 -0
index 147bdec42215..2b87f35c586c 100644

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

author maintainer Acked-by Rasmus Villemoes
#2 lore raw
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 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(-)
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
index 147bdec42215..2b87f35c586c 100644
@@ -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
index 147bdec42215..2b87f35c586c 100644
@@ -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
#3 lore raw
In reply to Yury Norov
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 calling strchr() and strlen(). We can do it better with a single call of strchrnul().

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 -0
 	return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
 }