From: Herman van Hazendonk <github.com@herrie.org>
To: linux-iio@vger.kernel.org
Cc: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
devicetree@vger.kernel.org,
Herman van Hazendonk <github.com@herrie.org>
Subject: [PATCH v2 1/2] iio: light: isl29018: fix 32-bit overflow in isl29018_read_lux()
Date: Thu, 4 Jun 2026 08:49:24 +0200 [thread overview]
Message-ID: <20260604064925.3097108-2-github.com@herrie.org> (raw)
In-Reply-To: <20260604064925.3097108-1-github.com@herrie.org>
The intermediate calculations in isl29018_read_lux() use 32-bit
arithmetic throughout, which overflows in two distinct ways:
1. lux_data * chip->scale.uscale — at 16-bit integration time and the
1000 fc range, scale.uscale is 976562. A full-scale 16-bit reading
(65535) gives 65535 * 976562 ≈ 64 billion, far beyond UINT_MAX.
The value wraps before the / 1000000 division can save it, producing
a wildly wrong data_x_range.
2. data_x_range * chip->calibscale — even after a correct data_x_range,
multiplying by a calibscale of a few hundred (reasonable for a deeply
tinted cover glass) pushes the product past INT_MAX, causing *lux to
wrap negative.
Fix both by widening the intermediate variables to u64 and clamping the
final result to INT_MAX before storing it in the signed int *lux out
parameter.
Signed-off-by: Herman van Hazendonk <github.com@herrie.org>
---
drivers/iio/light/isl29018.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/drivers/iio/light/isl29018.c b/drivers/iio/light/isl29018.c
index b6ab726d1dae..eafdfd9c4635 100644
--- a/drivers/iio/light/isl29018.c
+++ b/drivers/iio/light/isl29018.c
@@ -193,17 +193,18 @@ static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
{
int lux_data;
- unsigned int data_x_range;
+ u64 data_x_range, result;
lux_data = isl29018_read_sensor_input(chip,
ISL29018_CMD1_OPMODE_ALS_ONCE);
if (lux_data < 0)
return lux_data;
- data_x_range = lux_data * chip->scale.scale +
- lux_data * chip->scale.uscale / 1000000;
- *lux = data_x_range * chip->calibscale +
- data_x_range * chip->ucalibscale / 1000000;
+ data_x_range = (u64)lux_data * chip->scale.scale +
+ (u64)lux_data * chip->scale.uscale / 1000000;
+ result = data_x_range * chip->calibscale +
+ data_x_range * chip->ucalibscale / 1000000;
+ *lux = (int)min_t(u64, result, INT_MAX);
return 0;
}
--
2.43.0
next prev parent reply other threads:[~2026-06-04 6:49 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-04 5:47 [PATCH 0/1] iio: light: isl29018: cover-glass gain compensation via DT Herman van Hazendonk
2026-06-04 5:47 ` [PATCH 1/1] iio: light: isl29018: support " Herman van Hazendonk
2026-06-04 5:57 ` sashiko-bot
2026-06-04 7:17 ` Andy Shevchenko
2026-06-04 7:22 ` Joshua Crofts
2026-06-04 6:49 ` [PATCH v2 0/2] iio: light: isl29018: overflow fix + cover-glass gain " Herman van Hazendonk
2026-06-04 6:49 ` Herman van Hazendonk [this message]
2026-06-04 6:58 ` [PATCH v2 1/2] iio: light: isl29018: fix 32-bit overflow in isl29018_read_lux() sashiko-bot
2026-06-04 6:49 ` [PATCH v2 2/2] iio: light: isl29018: support cover-glass gain compensation via DT Herman van Hazendonk
2026-06-04 7:00 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260604064925.3097108-2-github.com@herrie.org \
--to=github.com@herrie.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox