Skip to contentSkip to conversation

hwmon: Replace sprintf() with sysfs_emit()

PATCH Superseded Amay Agarwal 2 participants 6 messages Mar 2 Hardware monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hwmon: Replace sprintf() with sysfs_emit()

This series of commits replaces sprintf() with sysfs_emit() in the code for several drivers (tc74, max31722, ads7828, max6659, emc1403) and helps avoid potential buffer overflows.

Amay Agarwal (5):
hwmon: tc74: use sysfs_emit() instead of sprintf()
hwmon: max31722: use sysfs_emit() instead of sprintf()
hwmon: ads7828: use sysfs_emit() instead of sprintf()
hwmon: max6650: use sysfs_emit() instead of sprintf()
hwmon: emc1403: use sysfs_emit() instead of sprintf()

 drivers/hwmon/ads7828.c  | 2 +-
 drivers/hwmon/emc1403.c  | 2 +-
 drivers/hwmon/max31722.c | 3 ++-
 drivers/hwmon/max6650.c  | 3 ++-
 drivers/hwmon/tc74.c     | 2 +-
 5 files changed, 7 insertions(+), 5 deletions(-)

Thank you,
Amay

#1 lore raw

PATCH 1/5 hwmon: tc74: use sysfs_emit() instead of sprintf()

Replacing sprintf() with sysfs_emit() for drivers/hwmon/tc74.c since it is the preferred helper for formatting sysfs outputs and preventing buffer overflows.

---
 drivers/hwmon/tc74.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/tc74.c +1 -1
index 9984373a25fb..7fb7b50ad1ad 100644
@@ -92,7 +92,7 @@ static ssize_t temp_input_show(struct device *dev,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%d\n", data->temp_input * 1000);
+	return sysfs_emit(buf, "%d\n", data->temp_input * 1000);
 }
 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
 
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH 1/5] hwmon: tc74: use sysfs_emit() instead of sprintf()

Quoted 1 line · Amay Agarwal

Replacing sprintf() with sysfs_emit() for drivers/hwmon/tc74.c since it

That is not "imperative mood" as asked for in
Documentation/process/submitting-patches.rst.

While minor, I think it is especially important in patches like this one which do not make functional changes.

On top of that, mentioning the driver name in the commit description is pointless, and the subject fir hwmon patches should start with "hwmon: (driver) ..."

Guenter

Quoted 21 lines

is the preferred helper for formatting sysfs outputs and preventing buffer overflows.

---
 drivers/hwmon/tc74.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/tc74.c +1 -1
index 9984373a25fb..7fb7b50ad1ad 100644
@@ -92,7 +92,7 @@ static ssize_t temp_input_show(struct device *dev,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%d\n", data->temp_input * 1000);
+	return sysfs_emit(buf, "%d\n", data->temp_input * 1000);
 }
 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
 
#3 lore raw

PATCH 2/5 hwmon: max31722: use sysfs_emit() instead of sprintf()

In reply to Amay Agarwal

Replacing sprintf() with sysfs_emit() for drivers/hwmon/max31722.c since it is the preferred helper for formatting sysfs outputs and preventing buffer overflows.

---
 drivers/hwmon/max31722.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/max31722.c +2 -1
index 9a31ef388396..6c5c86c75c36 100644
@@ -11,6 +11,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/spi/spi.h>
+#include <linux/sysfs.h>
 
 #define MAX31722_REG_CFG				0x00
 #define MAX31722_REG_TEMP_LSB				0x01
@@ -56,7 +57,7 @@ static ssize_t max31722_temp_show(struct device *dev,
 	if (ret < 0)
 		return ret;
 	/* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
-	return sprintf(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
+	return sysfs_emit(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
 }
 
 static SENSOR_DEVICE_ATTR_RO(temp1_input, max31722_temp, 0);
Signature · 1 line
2.53.0
maintainer
#4 lore raw

Re: [PATCH 2/5] hwmon: max31722: use sysfs_emit() instead of sprintf()

Quoted 4 lines · Amay Agarwal

Replacing sprintf() with sysfs_emit() for drivers/hwmon/max31722.c since it is the preferred helper for formatting sysfs outputs and preventing buffer overflows.

Same comment as for the other patch.

Guenter

Quoted 26 lines
---
 drivers/hwmon/max31722.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/max31722.c +2 -1
index 9a31ef388396..6c5c86c75c36 100644
@@ -11,6 +11,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/spi/spi.h>
+#include <linux/sysfs.h>
 
 #define MAX31722_REG_CFG				0x00
 #define MAX31722_REG_TEMP_LSB				0x01
@@ -56,7 +57,7 @@ static ssize_t max31722_temp_show(struct device *dev,
 	if (ret < 0)
 		return ret;
 	/* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
-	return sprintf(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
+	return sysfs_emit(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
 }
 
 static SENSOR_DEVICE_ATTR_RO(temp1_input, max31722_temp, 0);
maintainer
#5 lore raw
In reply to Amay Agarwal
Quoted 18 lines · Amay Agarwal

This series of commits replaces sprintf() with sysfs_emit() in the code for several drivers (tc74, max31722, ads7828, max6659, emc1403) and helps avoid potential buffer overflows.

Amay Agarwal (5):
hwmon: tc74: use sysfs_emit() instead of sprintf()
hwmon: max31722: use sysfs_emit() instead of sprintf()
hwmon: ads7828: use sysfs_emit() instead of sprintf()
hwmon: max6650: use sysfs_emit() instead of sprintf()
hwmon: emc1403: use sysfs_emit() instead of sprintf()

  drivers/hwmon/ads7828.c  | 2 +-
  drivers/hwmon/emc1403.c  | 2 +-
  drivers/hwmon/max31722.c | 3 ++-
  drivers/hwmon/max6650.c  | 3 ++-
  drivers/hwmon/tc74.c     | 2 +-
  5 files changed, 7 insertions(+), 5 deletions(-)
Quoted 3 lines

Thank you,
Amay

I have not seen patches 3..5 yet, but I bet I am sure they have the same problem as the first two patches, so I won't separately comment on those.

Guenter