Skip to contentSkip to conversation

staging: iio: light: Replace snprintf calls with scnprintf

PATCH In review Harinath Nampally 2 participants 3 messages May 24, 2017 – May 26, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: iio: light: Replace snprintf calls with scnprintf

This patch fixes the miscoded use of return value of snprintf by using the scnprintf function which returns the length of actual string created in the buffer.

---
 drivers/staging/iio/light/tsl2x7x.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/staging/iio/light/tsl2x7x.c +10 -10
index 1467199..6908bc1 100644
@@ -921,7 +921,7 @@ static ssize_t power_state_show(struct device *dev,
 {
 	struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
 
-	return snprintf(buf, PAGE_SIZE, "%d\n", chip->tsl2x7x_chip_status);
+	return scnprintf(buf, PAGE_SIZE, "%d\n", chip->tsl2x7x_chip_status);
 }
 
 static ssize_t power_state_store(struct device *dev,
@@ -954,17 +954,17 @@ static ssize_t in_illuminance0_calibscale_available_show(struct device *dev,
 	case tmd2671:
 	case tsl2771:
 	case tmd2771:
-		return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 128");
+		return scnprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 128");
 	}
 
-	return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 120");
+	return scnprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 120");
 }
 
 static ssize_t in_proximity0_calibscale_available_show(struct device *dev,
 						struct device_attribute *attr,
 						char *buf)
 {
-		return snprintf(buf, PAGE_SIZE, "%s\n", "1 2 4 8");
+		return scnprintf(buf, PAGE_SIZE, "%s\n", "1 2 4 8");
 }
 
 static ssize_t in_illuminance0_integration_time_show(struct device *dev,
@@ -979,7 +979,7 @@ static ssize_t in_illuminance0_integration_time_show(struct device *dev,
 	y /= 1000;
 	z %= 1000;
 
-	return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
+	return scnprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
 }
 
 static ssize_t in_illuminance0_integration_time_store(struct device *dev,
@@ -1016,7 +1016,7 @@ static ssize_t in_illuminance0_target_input_show(struct device *dev,
 {
 	struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
 
-	return snprintf(buf, PAGE_SIZE, "%d\n",
+	return scnprintf(buf, PAGE_SIZE, "%d\n",
 			chip->tsl2x7x_settings.als_cal_target);
 }
 
@@ -1054,7 +1054,7 @@ static ssize_t in_intensity0_thresh_period_show(struct device *dev,
 	y = filter_delay / 1000;
 	z = filter_delay % 1000;
 
-	return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
+	return scnprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
 }
 
 static ssize_t in_intensity0_thresh_period_store(struct device *dev,
@@ -1102,7 +1102,7 @@ static ssize_t in_proximity0_thresh_period_show(struct device *dev,
 	y = filter_delay / 1000;
 	z = filter_delay % 1000;
 
-	return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
+	return scnprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
 }
 
 static ssize_t in_proximity0_thresh_period_store(struct device *dev,
@@ -1163,7 +1163,7 @@ static ssize_t in_illuminance0_lux_table_show(struct device *dev,
 	int offset = 0;
 
 	while (i < (TSL2X7X_MAX_LUX_TABLE_SIZE * 3)) {
-		offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,%u,",
+		offset += scnprintf(buf + offset, PAGE_SIZE, "%u,%u,%u,",
 			chip->tsl2x7x_device_lux[i].ratio,
 			chip->tsl2x7x_device_lux[i].ch0,
 			chip->tsl2x7x_device_lux[i].ch1);
@@ -1178,7 +1178,7 @@ static ssize_t in_illuminance0_lux_table_show(struct device *dev,
 		i++;
 	}
 
-	offset += snprintf(buf + offset, PAGE_SIZE, "\n");
+	offset += scnprintf(buf + offset, PAGE_SIZE, "\n");
 	return offset;
 }
 
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 14 lines · Harinath Nampally

This patch fixes the miscoded use of return value of snprintf by using the scnprintf function which returns the length of actual string created in the buffer.

---
 drivers/staging/iio/light/tsl2x7x.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/staging/iio/light/tsl2x7x.c +0 -0
index 1467199..6908bc1 100644

drivers/staging/iio/light/tsl2x7x.c static ssize_t power_state_show(struct device *dev, · line 924

Quoted 5 lines
drivers/staging/iio/light/tsl2x7x.c +1 -1
@@ -921,7 +921,7 @@ static ssize_t power_state_show(struct device *dev,
 {
 	struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
 
-	return snprintf(buf, PAGE_SIZE, "%d\n", chip->tsl2x7x_chip_status);
+	return scnprintf(buf, PAGE_SIZE, "%d\n", chip->tsl2x7x_chip_status);

It should just be sprintf(), no need for testing for PAGE_SIZE for sysfs attributes, we "know" an integer will not overflow that buffer.

thanks,

greg k-h