From: Adi Nata <adinata.softwareengineer@gmail.com>
To: lorenzo@kernel.org, jic23@kernel.org, dlechner@baylibre.com,
nuno.sa@analog.com, andy@kernel.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: linux-kernel-mentees@lists.linux.dev,
Adi Nata <adinata.softwareengineer@gmail.com>
Subject: [PATCH v2 5/5] iio: humidity: hts221: fix division by zero in calibration parsing
Date: Thu, 13 Aug 2026 23:31:29 +0800 [thread overview]
Message-ID: <20260813153129.12423-6-adinata.softwareengineer@gmail.com> (raw)
In-Reply-To: <20260813153129.12423-1-adinata.softwareengineer@gmail.com>
hts221_parse_rh_caldata() and hts221_parse_temp_caldata() divide by
(cal_x1 - cal_x0) without checking that the two calibration points
differ can cause division by zero.
Reject zero divisor with -EINVAL, logging the offending calibration
values. A device with such calibration data cannot produce meaningful
scale or offset values anyway.
Signed-off-by: Adi Nata <adinata.softwareengineer@gmail.com>
---
drivers/iio/humidity/hts221_core.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/drivers/iio/humidity/hts221_core.c b/drivers/iio/humidity/hts221_core.c
index fda38dfc6742..47a10884d812 100644
--- a/drivers/iio/humidity/hts221_core.c
+++ b/drivers/iio/humidity/hts221_core.c
@@ -245,6 +245,7 @@ int hts221_set_enable(struct hts221_hw *hw, bool enable)
static int hts221_parse_temp_caldata(struct hts221_hw *hw)
{
+ struct device *dev = hw->dev;
int err, *slope, *b_gen, cal0, cal1;
s16 cal_x0, cal_x1, cal_y0, cal_y1;
__le16 val;
@@ -275,10 +276,20 @@ static int hts221_parse_temp_caldata(struct hts221_hw *hw)
return err;
cal_x1 = le16_to_cpu(val);
+ if (cal_x1 == cal_x0)
+ return dev_err_probe(dev, -EINVAL,
+ "invalid temperature calibration points (x0 %d, x1 %d)\n",
+ cal_x0, cal_x1);
+
slope = &hw->sensors[HTS221_SENSOR_T].slope;
b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen;
*slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0);
+ if (!*slope)
+ return dev_err_probe(dev, -EINVAL,
+ "invalid temperature calibration slope (y0 %d, y1 %d)\n",
+ cal_y0, cal_y1);
+
*b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) /
(cal_x1 - cal_x0);
*b_gen *= 8;
@@ -288,6 +299,7 @@ static int hts221_parse_temp_caldata(struct hts221_hw *hw)
static int hts221_parse_rh_caldata(struct hts221_hw *hw)
{
+ struct device *dev = hw->dev;
int err, *slope, *b_gen, data;
s16 cal_x0, cal_x1, cal_y0, cal_y1;
__le16 val;
@@ -314,10 +326,20 @@ static int hts221_parse_rh_caldata(struct hts221_hw *hw)
return err;
cal_x1 = le16_to_cpu(val);
+ if (cal_x1 == cal_x0)
+ return dev_err_probe(dev, -EINVAL,
+ "invalid rh calibration points (x0 %d, x1 %d)\n",
+ cal_x0, cal_x1);
+
slope = &hw->sensors[HTS221_SENSOR_H].slope;
b_gen = &hw->sensors[HTS221_SENSOR_H].b_gen;
*slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0);
+ if (!*slope)
+ return dev_err_probe(dev, -EINVAL,
+ "invalid rh calibration slope (y0 %d, y1 %d)\n",
+ cal_y0, cal_y1);
+
*b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) /
(cal_x1 - cal_x0);
*b_gen *= 8;
--
2.47.3
prev parent reply other threads:[~2026-08-13 15:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-13 15:31 [PATCH v2 0/5] iio: humidity: hts221: update probe and logging implementations Adi Nata
2026-08-13 15:31 ` [PATCH v2 1/5] iio: humidity: hts221: report available values via read_avail() Adi Nata
2026-08-13 15:31 ` [PATCH v2 2/5] iio: humidity: hts221: Add a blank line after variable declarations Adi Nata
2026-08-13 15:31 ` [PATCH v2 3/5] iio: humidity: hts221: Allow unknown whoami for DT fallback Adi Nata
2026-08-13 15:31 ` [PATCH v2 4/5] iio: humidity: hts221: use dev_err_probe() in probe paths Adi Nata
2026-08-13 15:31 ` Adi Nata [this message]
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=20260813153129.12423-6-adinata.softwareengineer@gmail.com \
--to=adinata.softwareengineer@gmail.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel-mentees@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=lorenzo@kernel.org \
--cc=nuno.sa@analog.com \
/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