Re: [PATCH] iio: humidity: hts221: fix division by zero in calibration data parsing

Joshua Crofts <[email protected]>
Newsgroups org.kernel.vger.linux-iio,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
On Tue, 25 Aug 2026 17:21:10 +0800
Yang Zi <[email protected]> wrote:

> hts221_parse_temp_caldata() and hts221_parse_rh_caldata() compute the
> sensor slope as ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0). If the
> device reports cal_x1 == cal_x0 the division causes a divide-by-zero
> error.
> 
> Return -EINVAL when cal_x1 == cal_x0 in both functions before performing
> the division.
> 
> Signed-off-by: Yang Zi <[email protected]>
> ---
> diff --git a/drivers/iio/humidity/hts221_core.c b/drivers/iio/humidity/hts221_core.c
> index bfeb0a60d3af..7d1f04951d8b 100644
> --- a/drivers/iio/humidity/hts221_core.c
> +++ b/drivers/iio/humidity/hts221_core.c
> @@ -288,6 +288,9 @@ static int hts221_parse_temp_caldata(struct hts221_hw *hw)
>          return err;
>      cal_x1 = le16_to_cpu(val);
>  
> +    if (cal_x1 == cal_x0)
> +        return -EINVAL;
> +
>      slope = &hw->sensors[HTS221_SENSOR_T].slope;
>      b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen;
>  
> @@ -327,6 +330,9 @@ static int hts221_parse_rh_caldata(struct hts221_hw *hw)
>          return err;
>      cal_x1 = le16_to_cpu(val);
>  
> +    if (cal_x1 == cal_x0)
> +        return -EINVAL;
> +
>      slope = &hw->sensors[HTS221_SENSOR_H].slope;
>      b_gen = &hw->sensors[HTS221_SENSOR_H].b_gen;
>  
> 
> 

This definitely needs a Fixes: tag, otherwise LGTM.

Feel free to carry my review tag.

Reviewed-by: Joshua Crofts <[email protected]>

PS, a bit of process info: I noticed Sashiko (sashiko.dev) failed to apply your
patches. To prevent this from happening in the future, please use the `--base` flag
when running `git format-patch` to specify which commit it should be applied onto.

-- 
Kind regards,
Joshua Crofts
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.