Skip to contentSkip to conversation

iio: proximity: srf08: replace sprintf with sysfs_emit

PATCH Superseded Maxwell Doose 2 participants 3 messages Apr 19 – Apr 20

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
v2:
- Fixed indenting of sysfs_emit call in function srf08_show_range_mm.
- Optimized return sequence in function srf08_show_sensitivity by directly returning instead of modifying a variable and then returning.

 drivers/iio/proximity/srf08.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/iio/proximity/srf08.c +6 -9
index d7e4cc48cfbf..604e89f89fe3 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,8 +238,8 @@ 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,
-						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;
 }
@@ -332,11 +332,8 @@ static ssize_t srf08_show_sensitivity(struct device *dev,
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct srf08_data *data = iio_priv(indio_dev);
-	int len;
 
-	len = sprintf(buf, "%d\n", data->sensitivity);
-
-	return len;
+	return sysfs_emit(buf, "%d\n", data->sensitivity);
 }
 
 static ssize_t srf08_write_sensitivity(struct srf08_data *data,
Signature · 1 line
2.53.0
#1 lore raw
Quoted 3 lines · Maxwell Doose

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.

Have you read my other reply?
Please, follow the comments I gave there and update the commit message, Subject, and the code accordingly.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 2 lines · Andy Shevchenko

Have you read my other reply?

Yes, but I sent this v2 before you replied with your comments on the original.

Quoted 2 lines

Please, follow the comments I gave there and update the commit message, Subject, and the code accordingly.

I'll get straight to that once I have time.

best regards,
maxwell