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

Andy Shevchenko <[email protected]>
Newsgroups org.kernel.vger.linux-iio,org.kernel.vger.linux-kernel
Organization Intel Finland Oy - BIC 0357606-4 - c/o Alberga Business Park, 6 krs, Bertel Jungin Aukio 5, 02600 Espoo
Message-ID <[email protected]>
On Tue, Aug 25, 2026 at 05:21:10PM +0800, Yang Zi 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.

Do you have HW to test? Any fault injection perhaps?
The problem with div-by-0 is that it's implementation defined, meaning
that it's how compiler decides. Now, the question is, what will compiler
do in the current code (no patch applied)?

...

>          return err;

>      cal_x1 = le16_to_cpu(val);

>  

I would remove this blank line as it's coupled with the above.

> +    if (cal_x1 == cal_x0)
> +        return -EINVAL;
> +

>      slope = &hw->sensors[HTS221_SENSOR_T].slope;
>      b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen;

Ditto for the rest.


-- 
With Best Regards,
Andy Shevchenko
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.