Skip to contentSkip to conversation

usb: misc: cytherm: replace deprecated simple_strtoul with kstrtoint

PATCH In review Jad Keskes 2 participants 4 messages Jun 22 – Jul 10 USB

lore
Share & tools
lore

Jad Keskes

author
#0 lore raw

PATCH 1/1 usb: misc: cytherm: replace deprecated simple_strtoul with kstrtoint

simple_strtoul() is deprecated in favor of kstrtoint(). The old function silently accepts garbage input. kstrtoint() returns -EINVAL so the driver can actually tell the user they typed something stupid instead of accepting 0 and wondering why the thermometer isn't responding.

The 0-255 clamping stays the same, just with proper error handling in front of it now.

---
 drivers/usb/misc/cytherm.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/usb/misc/cytherm.c +9 -3
index b183df9826bc..81de3fecf3da 100644
@@ -85,7 +85,9 @@ static ssize_t brightness_store(struct device *dev, struct device_attribute *att
 	if (!buffer)
 		return 0;
 
-	cytherm->brightness = simple_strtoul(buf, NULL, 10);
+	retval = kstrtoint(buf, 10, &cytherm->brightness);
+	if (retval < 0)
+		return retval;
    
 	if (cytherm->brightness > 0xFF)
 		cytherm->brightness = 0xFF;
@@ -217,7 +219,9 @@ static ssize_t port0_store(struct device *dev, struct device_attribute *attr, co
 	if (!buffer)
 		return 0;
 
-	tmp = simple_strtoul(buf, NULL, 10);
+	retval = kstrtoint(buf, 10, &tmp);
+	if (retval < 0)
+		return retval;
    
 	if (tmp > 0xFF)
 		tmp = 0xFF;
@@ -272,7 +276,9 @@ static ssize_t port1_store(struct device *dev, struct device_attribute *attr, co
 	if (!buffer)
 		return 0;
 
-	tmp = simple_strtoul(buf, NULL, 10);
+	retval = kstrtoint(buf, 10, &tmp);
+	if (retval < 0)
+		return retval;
    
 	if (tmp > 0xFF)
 		tmp = 0xFF;
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 19 lines · Jad Keskes

simple_strtoul() is deprecated in favor of kstrtoint(). The old function silently accepts garbage input. kstrtoint() returns -EINVAL so the driver can actually tell the user they typed something stupid instead of accepting 0 and wondering why the thermometer isn't responding.

The 0-255 clamping stays the same, just with proper error handling in front of it now.

---
 drivers/usb/misc/cytherm.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/usb/misc/cytherm.c +0 -0
index b183df9826bc..81de3fecf3da 100644

drivers/usb/misc/cytherm.c static ssize_t brightness_store(struct device *dev, struct device_attribute *att · line 90

Quoted 7 lines
drivers/usb/misc/cytherm.c +3 -1
@@ -85,7 +85,9 @@ static ssize_t brightness_store(struct device *dev, struct device_attribute *att
 	if (!buffer)
 		return 0;
 
-	cytherm->brightness = simple_strtoul(buf, NULL, 10);
+	retval = kstrtoint(buf, 10, &cytherm->brightness);
+	if (retval < 0)
+		return retval;

This changes the user/kernel functionality, are you sure it is safe to do so?

What is wrong with leaving the current calls?

thanks,

greg k-h

author
#2 lore raw
Quoted 2 lines · Greg KH

This changes the user/kernel functionality, are you sure it is safe to do so?

Nobody writes garbage to these on purpose, and if they do by accident they should get an error, not 0. The only way this breaks is if some tool depends on "echo foo > brightness" silently setting to 0 — which would be a bug in that tool.

Quoted 1 line

What is wrong with leaving the current calls?

simple_strtoul is deprecated per deprecated.rst. It ignores overflows too — writing -1 wraps to ULONG_MAX then gets clamped to 0xFF instead of erroring out. kstrtoint returns -EINVAL for both.

maintainer
#3 lore raw
Quoted 9 lines · Jad Keskes
Quoted 2 lines · Greg KH

This changes the user/kernel functionality, are you sure it is safe to do so?

Nobody writes garbage to these on purpose, and if they do by accident they should get an error, not 0. The only way this breaks is if some tool depends on "echo foo > brightness" silently setting to 0 — which would be a bug in that tool.

Quoted 5 lines
Quoted 1 line

What is wrong with leaving the current calls?

simple_strtoul is deprecated per deprecated.rst. It ignores overflows too — writing -1 wraps to ULONG_MAX then gets clamped to 0xFF instead of erroring out. kstrtoint returns -EINVAL for both.

Sure, it's deprecated, so don't add it to new code, but for existing stuff, don't change how user/kernel interactions might work.

thanks,

greg k-h