mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] hwmon: Minor errno and sysfs fixes
@ 2025-12-19 15:48 Kari Argillander
  2025-12-19 15:48 ` [PATCH 1/2] hwmon: Fix wrong return errno in *sanitize_name() Kari Argillander
  2025-12-19 15:48 ` [PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks Kari Argillander
  0 siblings, 2 replies; 5+ messages in thread
From: Kari Argillander @ 2025-12-19 15:48 UTC (permalink / raw)
  To: Kari Argillander, Guenter Roeck; +Cc: linux-hwmon, linux-kernel

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.

Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
---
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,
-- 
Kari Argillander <kari.argillander@gmail.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] hwmon: Fix wrong return errno in *sanitize_name()
  2025-12-19 15:48 [PATCH 0/2] hwmon: Minor errno and sysfs fixes Kari Argillander
@ 2025-12-19 15:48 ` Kari Argillander
  2025-12-19 16:46   ` Guenter Roeck
  2025-12-19 15:48 ` [PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks Kari Argillander
  1 sibling, 1 reply; 5+ messages in thread
From: Kari Argillander @ 2025-12-19 15:48 UTC (permalink / raw)
  To: Kari Argillander, Guenter Roeck; +Cc: linux-hwmon, linux-kernel

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 <kari.argillander@gmail.com>
---
 drivers/hwmon/hwmon.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 0b4bdcd33c7b..7a784d685d77 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -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);

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks
  2025-12-19 15:48 [PATCH 0/2] hwmon: Minor errno and sysfs fixes Kari Argillander
  2025-12-19 15:48 ` [PATCH 1/2] hwmon: Fix wrong return errno in *sanitize_name() Kari Argillander
@ 2025-12-19 15:48 ` Kari Argillander
  2025-12-19 16:47   ` Guenter Roeck
  1 sibling, 1 reply; 5+ messages in thread
From: Kari Argillander @ 2025-12-19 15:48 UTC (permalink / raw)
  To: Kari Argillander, Guenter Roeck; +Cc: linux-hwmon, linux-kernel

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] hwmon: Fix wrong return errno in *sanitize_name()
  2025-12-19 15:48 ` [PATCH 1/2] hwmon: Fix wrong return errno in *sanitize_name() Kari Argillander
@ 2025-12-19 16:46   ` Guenter Roeck
  0 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2025-12-19 16:46 UTC (permalink / raw)
  To: Kari Argillander; +Cc: linux-hwmon, linux-kernel

On Fri, Dec 19, 2025 at 05:48:14PM +0200, Kari Argillander wrote:
> 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 <kari.argillander@gmail.com>

Applied.

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks
  2025-12-19 15:48 ` [PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks Kari Argillander
@ 2025-12-19 16:47   ` Guenter Roeck
  0 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2025-12-19 16:47 UTC (permalink / raw)
  To: Kari Argillander; +Cc: linux-hwmon, linux-kernel

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,

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-12-19 16:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-19 15:48 [PATCH 0/2] hwmon: Minor errno and sysfs fixes Kari Argillander
2025-12-19 15:48 ` [PATCH 1/2] hwmon: Fix wrong return errno in *sanitize_name() Kari Argillander
2025-12-19 16:46   ` Guenter Roeck
2025-12-19 15:48 ` [PATCH 2/2] hwmon: Use sysfs_emit in show function callsbacks Kari Argillander
2025-12-19 16:47   ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®