Skip to contentSkip to conversation

iio: proximity: srf08: replace sprintf with sysfs_emit

PATCH Superseded Maxwell Doose 3 participants 3 messages Apr 18 – Apr 20

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: proximity: srf08: replace sprintf with sysfs_emit

This patch replaces sprintf function calls with sysfs_emit and sysfs_emit_at. This removes unsafe functions and makes the driver more stable and modern, by ensuring that boundary checks take place.

---
 drivers/iio/proximity/srf08.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/proximity/srf08.c +5 -5
index d7e4cc48cfbf..6d38a894ecd3 100644
@@ -226,7 +226,7 @@ static int srf08_read_raw(struct iio_dev *indio_dev,
 static ssize_t srf08_show_range_mm_available(struct device *dev,
 				struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "[0.043 0.043 11.008]\n");
+	return sysfs_emit(buf, "[0.043 0.043 11.008]\n");
 }
 
 static IIO_DEVICE_ATTR(sensor_max_range_available, S_IRUGO,
@@ -238,7 +238,7 @@ static ssize_t srf08_show_range_mm(struct device *dev,
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct srf08_data *data = iio_priv(indio_dev);
 
-	return sprintf(buf, "%d.%03d\n", data->range_mm / 1000,
+	return sysfs_emit(buf, "%d.%03d\n", data->range_mm / 1000,
 						data->range_mm % 1000);
 }
 
@@ -316,10 +316,10 @@ static ssize_t srf08_show_sensitivity_available(struct device *dev,
 
 	for (i = 0; i < data->chip_info->num_sensitivity_avail; i++)
 		if (data->chip_info->sensitivity_avail[i])
-			len += sprintf(buf + len, "%d ",
+			len += sysfs_emit_at(buf, len, "%d ",
 				data->chip_info->sensitivity_avail[i]);
 
-	len += sprintf(buf + len, "\n");
+	len += sysfs_emit_at(buf, len, "\n");
 
 	return len;
 }
@@ -334,7 +334,7 @@ static ssize_t srf08_show_sensitivity(struct device *dev,
 	struct srf08_data *data = iio_priv(indio_dev);
 	int len;
 
-	len = sprintf(buf, "%d\n", data->sensitivity);
+	len = sysfs_emit(buf, "%d\n", data->sensitivity);
 
 	return len;
 }
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 5 lines · On Fri, 17 Apr 2026 19:52:04 -0500 Maxwell Doose <[email protected]> wrote:

This patch replaces sprintf function calls with sysfs_emit and sysfs_emit_at. This removes unsafe functions and makes the driver more stable and modern, by ensuring that boundary checks take place.

Hi Maxwell.

Changes look good but I think we should make a few more changes whilst doing this. Whilst often we'll ask people to beak out different types of changes, that gets a bit 'flexible' when the code is changing for other reasons and the other stuff is small enough.

So here I'd prefer to do all the stuff mentioned below in one patch.

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/proximity/srf08.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/proximity/srf08.c +1 -1
index d7e4cc48cfbf..6d38a894ecd3 100644
@@ -226,7 +226,7 @@ static int srf08_read_raw(struct iio_dev *indio_dev,
 static ssize_t srf08_show_range_mm_available(struct device *dev,
 				struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "[0.043 0.043 11.008]\n");
+	return sysfs_emit(buf, "[0.043 0.043 11.008]\n");
 }
 
 static IIO_DEVICE_ATTR(sensor_max_range_available, S_IRUGO,

drivers/iio/proximity/srf08.c static ssize_t srf08_show_range_mm(struct device *dev, · line 242

Quoted 6 lines
drivers/iio/proximity/srf08.c +1 -1
@@ -238,7 +238,7 @@ static ssize_t srf08_show_range_mm(struct device *dev,
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct srf08_data *data = iio_priv(indio_dev);
 
-	return sprintf(buf, "%d.%03d\n", data->range_mm / 1000,
+	return sysfs_emit(buf, "%d.%03d\n", data->range_mm / 1000,
 						data->range_mm % 1000);

That is really odd indenting. Please reindent as:

	return sysfs_emit(buf, "%d.%03d\n", data->range_mm / 1000,
			  data->range_mm % 1000);

If there are lots of similar cases in the driver this may make sense as a separate follow up patch.

Quoted 16 lines
drivers/iio/proximity/srf08.c +2 -2
 }
 
@@ -316,10 +316,10 @@ static ssize_t srf08_show_sensitivity_available(struct device *dev,
 
 	for (i = 0; i < data->chip_info->num_sensitivity_avail; i++)
 		if (data->chip_info->sensitivity_avail[i])
-			len += sprintf(buf + len, "%d ",
+			len += sysfs_emit_at(buf, len, "%d ",
 				data->chip_info->sensitivity_avail[i]);
 
-	len += sprintf(buf + len, "\n");
+	len += sysfs_emit_at(buf, len, "\n");
 
 	return len;
 }

drivers/iio/proximity/srf08.c static ssize_t srf08_show_sensitivity(struct device *dev, · line 337

Quoted 5 lines
drivers/iio/proximity/srf08.c +1 -1
@@ -334,7 +334,7 @@ static ssize_t srf08_show_sensitivity(struct device *dev,
 	struct srf08_data *data = iio_priv(indio_dev);
 	int len;
 
-	len = sprintf(buf, "%d\n", data->sensitivity);
+	len = sysfs_emit(buf, "%d\n", data->sensitivity);

Might was well do

	return sysfs_emit();
Quoted 3 lines
drivers/iio/proximity/srf08.c +0 -0
 	return len;
 }
#2 lore raw
In reply to Maxwell Doose
Quoted 1 line · Maxwell Doose

This patch replaces sprintf function calls with sysfs_emit and

sysfs_emit()

Quoted 1 line

sysfs_emit_at. This removes unsafe functions and makes the driver more

sysfs_emit_at()

Quoted 1 line

stable and modern, by ensuring that boundary checks take place.

...

drivers/iio/proximity/srf08.c static ssize_t srf08_show_range_mm(struct device *dev, · line 242

Quoted 3 lines
drivers/iio/proximity/srf08.c +1 -1
-	return sprintf(buf, "%d.%03d\n", data->range_mm / 1000,
+	return sysfs_emit(buf, "%d.%03d\n", data->range_mm / 1000,
 						data->range_mm % 1000);

While at it, we may rewrap it to follow the logical split:

	return sysfs_emit(buf, "%d.%03d\n",
			  data->range_mm / 1000, data->range_mm % 1000);

No need to resend for this. And maybe Jonathan has better idea of this.

Signature · 2 lines
With Best Regards,
Andy Shevchenko