iio: tsl2x7x/tsl2772: avoid potential division by zero
authorColin Ian King <colin.king@canonical.com>
Wed, 30 May 2018 18:19:36 +0000 (19:19 +0100)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sun, 24 Jun 2018 13:22:38 +0000 (14:22 +0100)
It may be possible for tsl2772_get_lux to return a zero lux value
and hence a division by zero can occur when lux_val is zero. Check
for this case and return -ERANGE to avoid the division by zero.

Detected by CoverityScan, CID#1469484 ("Division or modulo by zero")

Signed-off-by: Colin Ian King <colin.king@canonical.com>
Acked-by: Brian Masney <masneyb@onstation.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/light/tsl2772.c

index 34d42a2..df5b2a0 100644 (file)
@@ -582,6 +582,8 @@ static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
                        "%s: failed to get lux\n", __func__);
                return lux_val;
        }
+       if (lux_val == 0)
+               return -ERANGE;
 
        ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
                        lux_val;