Re: [PATCH v11 09/15] iio: afe: rescale: reduce risk of integer overflow
From: Liam Beguin <hidden>
Date: 2021-12-22 18:38:56
Also in:
linux-iio, lkml
From: Liam Beguin <hidden>
Date: 2021-12-22 18:38:56
Also in:
linux-iio, lkml
On Wed, Dec 22, 2021 at 02:29:04PM +0200, Andy Shevchenko wrote:
On Wed, Dec 22, 2021 at 5:47 AM Liam Beguin [off-list ref] wrote:quoted
From: Liam Beguin <redacted> Reduce the risk of integer overflow by doing the scale calculation on a 64-bit integer. Since the rescaling is only performed on *val, reuse the IIO_VAL_FRACTIONAL_LOG2 case....quoted
- tmp = 1 << *val2;At some point this should be BIT()
I'm not against changing this, but (to me at least) 1 << *val2 seems more explicit as we're not working with bitfields. No?
Rule of thumb (in accordance with C standard), always use unsigned value as left operand of the _left_ shift.
Right, that makes sense! In practice though, since we'll most likely never use higher bits of *val2 with IIO_VAL_FRACTIONAL_LOG2, would it be enough to simply typecast? tmp = 1 << (unsigned int)*val2; Cheers, Liam
quoted
+ if (scale_type == IIO_VAL_FRACTIONAL) + tmp = *val2; + else + tmp = 1 << *val2;-- With Best Regards, Andy Shevchenko