hwmon: Minor errno and sysfs fixes
cover hwmon: Minor errno and sysfs fixes
This series contains two small hwmon cleanups.
The first patch fixes an incorrect errno returned by sanitize_name() when called with a NULL pointer. Returning ENOMEM in that case is misleading. EINVAL better reflects the actual error.
The second patch switches sysfs show callbacks to sysfs_emit(), as recommended by Documentation/filesystems/sysfs.rst. While the risk of overflow with sprintf() is low in practice, using sysfs_emit() avoids the issue entirely and aligns the code with current kernel guidance. This also simplifies Rust bindings, where overflows cannot be ignored.
---
Kari Argillander (2):
hwmon: Fix wrong return errno in *sanitize_name()
hwmon: Use sysfs_emit in show function callsbacks
drivers/hwmon/hwmon.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
---
base-commit: cc3aa43b44bdb43dfbac0fcb51c56594a11338a8
change-id: 20251219-hwmon-fixes-b8e205834d20
Best regards,
Signature · 1 line
Kari Argillander <[email protected]>
PATCH 1/2 hwmon: Fix wrong return errno in *sanitize_name()
Currently if user of *sanitize_name() function gives nullptr for name they get's ENOMEM. Logically it should be EINVAL.
--- drivers/hwmon/hwmon.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/hwmon/hwmon.c +4 -1
@@ -1260,6 +1260,9 @@ static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)*/ char *hwmon_sanitize_name (const char *name) { +if (!name) +return ERR_PTR (-EINVAL ); +return __hwmon_sanitize_name (NULL , name); }EXPORT_SYMBOL_GPL (hwmon_sanitize_name);
@@ -1276,7 +1279,7 @@ EXPORT_SYMBOL_GPL(hwmon_sanitize_name);*/ char *devm_hwmon_sanitize_name (struct device *dev,const char *name) { -if (!dev) +if (!dev || !name)return ERR_PTR (-EINVAL );return __hwmon_sanitize_name (dev, name);
Signature · 1 line
2.43.0
Re: [PATCH 1/2] hwmon: Fix wrong return errno in *sanitize_name()
Quoted 4 lines · Kari Argillander
Currently if user of
*sanitize_name()function gives nullptr for name they get's ENOMEM. Logically it should be EINVAL.Signed-off-by Kari Argillander <[email protected]>
Applied.
Thanks,
Guenter
PATCH 2/2 hwmon: Use sysfs_emit in show function callsbacks
In reply to Kari ArgillanderUse sysfs_emit() over sprintf() to prevent potential overflows. In hwmon_attr_show() that is totally impossible but looking other places many still use sysfs_emit().
Also according Documentation/filesystems/sysfs.rst code should use sysfs_emit().
--- drivers/hwmon/hwmon.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/hwmon/hwmon.c +3 -3
@@ -73,7 +73,7 @@ struct hwmon_thermal_data {static ssize_t name_show (struct device *dev,struct device_attribute *attr,char *buf) { -return sprintf (buf,"%s\n" ,to_hwmon_device (dev)->name); +return sysfs_emit (buf,"%s\n" ,to_hwmon_device (dev)->name); }static DEVICE_ATTR_RO (name);
@@ -446,7 +446,7 @@ static ssize_t hwmon_attr_show(struct device *dev,trace_hwmon_attr_show (hattr->index +hwmon_attr_base (hattr->type), hattr->name, val64); -return sprintf (buf,"%lld\n" , val64); +return sysfs_emit (buf,"%lld\n" , val64); } static ssize_t hwmon_attr_show_string(struct device *dev,
@@ -469,7 +469,7 @@ static ssize_t hwmon_attr_show_string(struct device *dev,trace_hwmon_attr_show_string (hattr->index +hwmon_attr_base (type), hattr->name, s); -return sprintf (buf,"%s\n" , s); +return sysfs_emit (buf,"%s\n" , s); } static ssize_t hwmon_attr_store(struct device *dev,
Signature · 1 line
2.43.0
Re: [PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks
Quoted 8 lines · Kari Argillander
Use
sysfs_emit()oversprintf()to prevent potential overflows. Inhwmon_attr_show()that is totally impossible but looking other places many still usesysfs_emit().Also according
Documentation/filesystems/sysfs.rstcode should usesysfs_emit().Signed-off-by Kari Argillander <[email protected]>
Applied.
Thanks,
Guenter
Quoted 35 lines
--- drivers/hwmon/hwmon.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/hwmon/hwmon.c +3
-3@@ -73,7 +73,7 @@ struct hwmon_thermal_data {static ssize_t name_show (struct device *dev,struct device_attribute *attr,char *buf) { -return sprintf (buf,"%s\n" ,to_hwmon_device (dev)->name); +return sysfs_emit (buf,"%s\n" ,to_hwmon_device (dev)->name); }static DEVICE_ATTR_RO (name);@@ -446,7 +446,7 @@ static ssize_t hwmon_attr_show(struct device *dev,trace_hwmon_attr_show (hattr->index +hwmon_attr_base (hattr->type), hattr->name, val64); -return sprintf (buf,"%lld\n" , val64); +return sysfs_emit (buf,"%lld\n" , val64); } static ssize_t hwmon_attr_show_string(struct device *dev,@@ -469,7 +469,7 @@ static ssize_t hwmon_attr_show_string(struct device *dev,trace_hwmon_attr_show_string (hattr->index +hwmon_attr_base (type), hattr->name, s); -return sprintf (buf,"%s\n" , s); +return sysfs_emit (buf,"%s\n" , s); } static ssize_t hwmon_attr_store(struct device *dev,