Skip to contentSkip to conversation

Staging: rtl8723bs: Use kstrto<type> instead of single variable sscanf

PATCH Superseded Georgiana Chelu 2 participants 3 messages Oct 1, 2017 – Oct 3, 2017 Staging

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Staging: rtl8723bs: Use kstrto<type> instead of single variable sscanf

Use kstro<type> instead of single variable sscanf to convert a string to the appropriate integer type. Also, check the return value.

Issue found by checkpatch.pl script:
Prefer kstrto<type> to single variable sscanf

---
 drivers/staging/rtl8723bs/os_dep/rtw_proc.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/staging/rtl8723bs/os_dep/rtw_proc.c +8 -4
index 92277457aba4..58eb50aa8098 100644
@@ -70,7 +70,11 @@ static ssize_t proc_set_log_level(struct file *file, const char __user *buffer,
 		return -EINVAL;
 
 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
-		sscanf(tmp, "%d ", &log_level);
+		int num = kstrtoint(tmp, 10, &log_level);
+
+		if (num != 1)
+			return count;
+
 		if (log_level >= _drv_always_ && log_level <= _drv_debug_)
 		{
 			GlobalDebugLevel = log_level;
@@ -476,7 +480,7 @@ ssize_t proc_set_odm_dbg_comp(struct file *file, const char __user *buffer, size
 
 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
 
-		int num = sscanf(tmp, "%llx", &dbg_comp);
+		int num = kstrtoull(tmp, 16, &dbg_comp);
 
 		if (num != 1)
 			return count;
@@ -510,7 +514,7 @@ ssize_t proc_set_odm_dbg_level(struct file *file, const char __user *buffer, siz
 
 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
 
-		int num = sscanf(tmp, "%u", &dbg_level);
+		int num = kstrtouint(tmp, 10, &dbg_level);
 
 		if (num != 1)
 			return count;
@@ -544,7 +548,7 @@ static ssize_t proc_set_odm_ability(struct file *file, const char __user *buffer
 
 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
 
-		int num = sscanf(tmp, "%x", &ability);
+		int num = kstrtouint(tmp, 16, &ability);
 
 		if (num != 1)
 			return count;
Signature · 1 line
2.11.0
#1 lore raw
Quoted 17 lines · Georgiana Chelu

Use kstro<type> instead of single variable sscanf to convert a string to the appropriate integer type. Also, check the return value.

Issue found by checkpatch.pl script:
Prefer kstrto<type> to single variable sscanf

---
 drivers/staging/rtl8723bs/os_dep/rtw_proc.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/staging/rtl8723bs/os_dep/rtw_proc.c +0 -0
index 92277457aba4..58eb50aa8098 100644

drivers/staging/rtl8723bs/os_dep/rtw_proc.c static ssize_t proc_set_log_level(struct file *file, const char __user *buffer, · line 76

Quoted 8 lines
drivers/staging/rtl8723bs/os_dep/rtw_proc.c +4 -1
@@ -70,7 +70,11 @@ static ssize_t proc_set_log_level(struct file *file, const char __user *buffer,
 		return -EINVAL;
 
 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
-		sscanf(tmp, "%d ", &log_level);
+		int num = kstrtoint(tmp, 10, &log_level);
+
+		if (num != 1)
+			return count;

How would num ever be 1 here? Did you test this?

Look at the documentation for kstrtoint() please, I think this just broke things :(

thanks,

greg k-h

#2 lore raw
Quoted 29 lines · Greg Kroah-Hartman
Quoted 17 lines · Georgiana Chelu

Use kstro<type> instead of single variable sscanf to convert a string to the appropriate integer type. Also, check the return value.

Issue found by checkpatch.pl script:
Prefer kstrto<type> to single variable sscanf

---
 drivers/staging/rtl8723bs/os_dep/rtw_proc.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/staging/rtl8723bs/os_dep/rtw_proc.c +0 -0
index 92277457aba4..58eb50aa8098 100644

drivers/staging/rtl8723bs/os_dep/rtw_proc.c static ssize_t proc_set_log_level(struct file *file, const char __user *buffer, · line 76

Quoted 8 lines
drivers/staging/rtl8723bs/os_dep/rtw_proc.c +4 -1
@@ -70,7 +70,11 @@ static ssize_t proc_set_log_level(struct file *file, const char __user *buffer,
              return -EINVAL;
 
      if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
-             sscanf(tmp, "%d ", &log_level);
+             int num = kstrtoint(tmp, 10, &log_level);
+
+             if (num != 1)
+                     return count;

How would num ever be 1 here? Did you test this?

Quoted 3 lines

Look at the documentation for kstrtoint() please, I think this just broke things :(

I missed that kstrtoint and sscanf have different output on SUCCESS. The return value should be 0 in case of success. I will send the second version of the patch. Thank you!

Georgiana

Quoted 3 lines

thanks,

greg k-h