usb: misc: cytherm: replace deprecated simple_strtoul with kstrtoint
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
@@ -85,7 +85,9 @@ static ssize_t brightness_store(struct device *dev, struct device_attribute *attif (!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, coif (!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, coif (!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
Greg KH
maintainerQuoted 19 lines · Jad Keskes
simple_strtoul()is deprecated in favor ofkstrtoint(). The old function silently accepts garbage input.kstrtoint()returns-EINVALso 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.
Signed-off-by Jad Keskes <[email protected]>--- drivers/usb/misc/cytherm.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)drivers/usb/misc/cytherm.c +0
-0
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 *attif (!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
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.
Greg KH
maintainerQuoted 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_strtoulis deprecated perdeprecated.rst. It ignores overflows too — writing -1 wraps toULONG_MAXthen gets clamped to0xFFinstead of erroring out. kstrtoint returns-EINVALfor 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