Skip to content

Commit 3dee042

Browse files
tiwaiJonathan Cameron
authored andcommitted
iio: tsl2772: Use scnprintf() for avoiding potential buffer overflow
snprintf() is a hard-to-use function, it's especially difficult to use it for concatenating substrings in a buffer with a limited size. Since snprintf() returns the would-be-output size, not the actual size, the subsequent use of snprintf() may go beyond the given limit easily. Although the current code doesn't actually overflow the buffer, it's an incorrect usage. This patch replaces such snprintf() calls with a safer version, scnprintf(). Also this fixes the incorrect argument of the buffer limit size passed to snprintf(), too. The size has to be decremented for the remaining length. Signed-off-by: Takashi Iwai <tiwai@suse.de> Reviewed-by: Brian Masney <masneyb@onstation.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent bf09cdd commit 3dee042

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

‎drivers/iio/light/tsl2772.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ static ssize_t in_illuminance0_target_input_show(struct device *dev,
932932
{
933933
struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
934934

935-
return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
935+
return scnprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
936936
}
937937

938938
static ssize_t in_illuminance0_target_input_store(struct device *dev,
@@ -986,7 +986,7 @@ static ssize_t in_illuminance0_lux_table_show(struct device *dev,
986986
int offset = 0;
987987

988988
while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
989-
offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
989+
offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%u,%u,",
990990
chip->tsl2772_device_lux[i].ch0,
991991
chip->tsl2772_device_lux[i].ch1);
992992
if (chip->tsl2772_device_lux[i].ch0 == 0) {
@@ -1000,7 +1000,7 @@ static ssize_t in_illuminance0_lux_table_show(struct device *dev,
10001000
i++;
10011001
}
10021002

1003-
offset += snprintf(buf + offset, PAGE_SIZE, "\n");
1003+
offset += scnprintf(buf + offset, PAGE_SIZE - offset, "\n");
10041004
return offset;
10051005
}
10061006

0 commit comments

Comments
 (0)