[PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks

Kari Argillander posted 2 patches 1 month, 3 weeks ago
[PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks
Posted by Kari Argillander 1 month, 3 weeks ago
Use 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().

Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
---
 drivers/hwmon/hwmon.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 7a784d685d77..714caa6a36a3 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -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,

-- 
2.43.0
Re: [PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks
Posted by Guenter Roeck 1 month, 3 weeks ago
On Fri, Dec 19, 2025 at 05:48:15PM +0200, Kari Argillander wrote:
> Use 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().
> 
> Signed-off-by: Kari Argillander <kari.argillander@gmail.com>

Applied.

Thanks,
Guenter

> ---
>  drivers/hwmon/hwmon.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index 7a784d685d77..714caa6a36a3 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c
> @@ -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,