iio: imu: inv_icm42600: fix 64bit multiplication
Subsystems (2)
PATCH 1/1 iio: imu: inv_icm42600: fix 64bit multiplication
In inv_icm42600_accel_convert_wom_to_roc() multiplying threshold by convert may result in a number requiring more than 32bit.
In this case, although value is 64bit wide, the result is truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.
Cast the first operand to u64 to ensure the multiplication is performed in the expected domain.
Address-Coverity-ID: 1647596 ("Integer handling issues (OVERFLOW_BEFORE_WIDEN)")
--- drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c +1 -1
@@ -347,7 +347,7 @@ static u64 inv_icm42600_accel_convert_wom_to_roc(unsigned int threshold,u64 value;u64 freq_uhz; - value = threshold * convert; + value = (u64 )threshold * convert; freq_uhz = (u64 )accel_hz *MICRO + (u64 )accel_uhz;/* compute the differential by multiplying by the frequency */
Signature · 1 line
2.49.1
Quoted 10 lines · Antonio Quartulli
In
inv_icm42600_accel_convert_wom_to_roc()multiplyingthresholdbyconvertmay result in a number requiring more than 32bit.
In this case, althoughvalueis 64bit wide, the result is truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.Cast the first operand to u64 to ensure the multiplication is performed in the expected domain.
Is this a theoretical or practical issue?
Quoted 3 lines
Fixes 50cfaa9a46c8 ("iio: imu: inv_icm42600: add WoM support")Address-Coverity-ID: 1647596 ("Integer handling issues (
OVERFLOW_BEFORE_WIDEN)")
Is this tag now official? I can't find where it's documented.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Antonio Quartulli
In
inv_icm42600_accel_convert_wom_to_roc()multiplyingthresholdbyconvertmay result in a number requiring more than 32bit.
In this case, althoughvalueis 64bit wide, the result is truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.
Quoted 4 lines
Quoted 2 lines
Cast the first operand to u64 to ensure the multiplication is performed in the expected domain.
Is this a theoretical or practical issue?
Can't say if it's practical because I don't know how large threshold can be.
This said, value is declared as u64, therefore I assumed the result is expected to be potentially larger than 32 bits.
Quoted 6 lines
Quoted 4 lines
Fixes 50cfaa9a46c8 ("iio: imu: inv_icm42600: add WoM support")Address-Coverity-ID: 1647596 ("Integer handling issues (
OVERFLOW_BEFORE_WIDEN)")Is this tag now official? I can't find where it's documented.
I don't think it's official, but it's what I have found in the git history for other bugs reported by Coverity. I already used it on other accepted patches as well.
Regards,
Signature · 5 lines
Antonio Quartulli CEO and Co-Founder Mandelbit Srl https://www.mandelbit.com
David Lechner
reviewerQuoted 11 lines · Antonio Quartulli
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Antonio Quartulli
In
inv_icm42600_accel_convert_wom_to_roc()multiplyingthresholdbyconvertmay result in a number requiring more than 32bit.
In this case, althoughvalueis 64bit wide, the result is truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Cast the first operand to u64 to ensure the multiplication is performed in the expected domain.
Is this a theoretical or practical issue?
Can't say if it's practical because I don't know how large
thresholdcan be.
The code is clear that it is between 0 and 255 inclusive.
unsigned int inv_icm42600_accel_convert_roc_to_wom(...)
{
...
if (roc == 0)
return 0;
...
return clamp(value, 1, 255);
}threshold = inv_icm42600_accel_convert_roc_to_wom(value, accel_hz, accel_uhz); value = inv_icm42600_accel_convert_wom_to_roc(threshold, accel_hz, accel_uhz)
So I would not call this a fix, just making the code more robust against any changes in the future.
Quoted 15 lines
This said,
valueis declared as u64, therefore I assumed the result is expected to be potentially larger than 32 bits.Quoted 6 lines
Quoted 4 lines
Fixes 50cfaa9a46c8 ("iio: imu: inv_icm42600: add WoM support")Address-Coverity-ID: 1647596 ("Integer handling issues (
OVERFLOW_BEFORE_WIDEN)")Is this tag now official? I can't find where it's documented.
I don't think it's official, but it's what I have found in the git history for other bugs reported by Coverity. I already used it on other accepted patches as well.
Regards,
Quoted 31 lines · David Lechner
Quoted 11 lines · Antonio Quartulli
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Antonio Quartulli
In
inv_icm42600_accel_convert_wom_to_roc()multiplyingthresholdbyconvertmay result in a number requiring more than 32bit.
In this case, althoughvalueis 64bit wide, the result is truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Cast the first operand to u64 to ensure the multiplication is performed in the expected domain.
Is this a theoretical or practical issue?
Can't say if it's practical because I don't know how large
thresholdcan be.The code is clear that it is between 0 and 255 inclusive.
unsigned int
inv_icm42600_accel_convert_roc_to_wom(...){ ... if (roc == 0) return 0; ... return clamp(value, 1, 255); }
Quoted 2 lines
threshold =
inv_icm42600_accel_convert_roc_to_wom(value, accel_hz, accel_uhz); value =inv_icm42600_accel_convert_wom_to_roc(threshold, accel_hz, accel_uhz)
Thanks for pointing this out - I overlooked that.
Quoted 3 lines
So I would not call this a fix, just making the code more robust against any changes in the future.
It's the type being used that convinced me something could be off.
Maybe threshold could just be u8 and value be u32? (just to improve self documentation) Also inv_icm42600_accel_convert_roc_to_wom() could return u8 for consistency.
I any case, this is a bit code cleanup and a bit bike shedding :-) I agree there is nothing to "fix" at this point.
Regards,
Signature · 5 lines
Antonio Quartulli CEO and Co-Founder Mandelbit Srl https://www.mandelbit.com
Quoted 14 lines · Antonio Quartulli
Quoted 13 lines · Andy Shevchenko
Quoted 10 lines · Antonio Quartulli
In
inv_icm42600_accel_convert_wom_to_roc()multiplyingthresholdbyconvertmay result in a number requiring more than 32bit.
In this case, althoughvalueis 64bit wide, the result is truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.Cast the first operand to u64 to ensure the multiplication is performed in the expected domain.
Quoted 7 lines
Quoted 1 line
Is this a theoretical or practical issue?
Can't say if it's practical because I don't know how large
thresholdcan be.This said,
valueis declared as u64, therefore I assumed the result is expected to be potentially larger than 32 bits.
Yeah, but can you prove the potential overflow? Can the compiler prove otherwise?
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Address-Coverity-ID: 1647596 ("Integer handling issues (
OVERFLOW_BEFORE_WIDEN)")Is this tag now official? I can't find where it's documented.
I don't think it's official, but it's what I have found in the git history for other bugs reported by Coverity.
I already used it on other accepted patches as well.
Are you an official representative from the coverity that runs against Linux source code? Or please explain how it keeps those numbers (e.g., 1647596) unique per project. For example, you and I run coverity and we get let's say the very same number (ID) but for different problems (even if we are looking at the same project), is this scenario real? If not, we need to have documentation inside the Linux kernel source tree explaining how to run Coverity, who is eligible to do that, how to resolve clashes (if any), etc., etc...
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 18 lines · Andy Shevchenko
Quoted 16 lines · Antonio Quartulli
Quoted 14 lines · Andy Shevchenko
Quoted 10 lines · Antonio Quartulli
In
inv_icm42600_accel_convert_wom_to_roc()multiplyingthresholdbyconvertmay result in a number requiring more than 32bit.
In this case, althoughvalueis 64bit wide, the result is truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.Cast the first operand to u64 to ensure the multiplication is performed in the expected domain.
Is this a theoretical or practical issue?
Quoted 8 lines
Quoted 5 lines
Can't say if it's practical because I don't know how large
thresholdcan be.This said,
valueis declared as u64, therefore I assumed the result is expected to be potentially larger than 32 bits.Yeah, but can you prove the potential overflow? Can the compiler prove otherwise?
As David pointed out, threshold is limited to [1, 255], as returned by inv_icm42600_accel_convert_roc_to_wom(), therefore no real overflow is possible.
It's the variable types being used that didn't make that fully clear. Maybe this could be cleaned up a little, but there is nothing to "fix".
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Address-Coverity-ID: 1647596 ("Integer handling issues (
OVERFLOW_BEFORE_WIDEN)")Is this tag now official? I can't find where it's documented.
I don't think it's official, but it's what I have found in the git history for other bugs reported by Coverity.
I already used it on other accepted patches as well.
Quoted 8 lines
Are you an official representative from the coverity that runs against Linux source code? Or please explain how it keeps those numbers (e.g., 1647596) unique per project. For example, you and I run coverity and we get let's say the very same number (ID) but for different problems (even if we are looking at the same project), is this scenario real? If not, we need to have documentation inside the Linux kernel source tree explaining how to run Coverity, who is eligible to do that, how to resolve clashes (if any), etc., etc...
There is an ("official") linux-next project in Coverity managed by Gustavo A. R. Silva, where all maintainers can subscribe to and then receive the weekly scan results:
https://scan.coverity.com/projects/linux-next-weekly-scan
https://embeddedor.com/blog/2024/09/28/one-simple-and-rewarding-way-to-contribute-to-the-linux-kernel-fix-coverity-issues/
I assumed all IDs in git are taken from that scan/project.
Regards,
Signature · 5 lines
Antonio Quartulli CEO and Co-Founder Mandelbit Srl https://www.mandelbit.com
Quoted 6 lines · Antonio Quartulli
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Antonio Quartulli
Quoted 2 lines · Andy Shevchenko
On Wed, Jul 16, 2025 at 10:22 PM Antonio Quartulli <[email protected]> wrote:
...
Quoted 17 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Address-Coverity-ID: 1647596 ("Integer handling issues (
OVERFLOW_BEFORE_WIDEN)")Is this tag now official? I can't find where it's documented.
I don't think it's official, but it's what I have found in the git history for other bugs reported by Coverity.
I already used it on other accepted patches as well.Quoted 8 lines
Are you an official representative from the coverity that runs against Linux source code? Or please explain how it keeps those numbers (e.g., 1647596) unique per project. For example, you and I run coverity and we get let's say the very same number (ID) but for different problems (even if we are looking at the same project), is this scenario real? If not, we need to have documentation inside the Linux kernel source tree explaining how to run Coverity, who is eligible to do that, how to resolve clashes (if any), etc., etc...
Quoted 8 lines
There is an ("official") linux-next project in Coverity managed by Gustavo A. R. Silva, where all maintainers can subscribe to and then receive the weekly scan results:
https://scan.coverity.com/projects/linux-next-weekly-scan
https://embeddedor.com/blog/2024/09/28/one-simple-and-rewarding-way-to-contribute-to-the-linux-kernel-fix-coverity-issues/I assumed all IDs in git are taken from that scan/project.
This makes a lot of sense! Gustavo et al., can you make a Documentation patch to explain all this?
Personally from now on I am okay with the tag as long as the patches tagged with it are at least Acked by Gustavo.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 4 lines · Antonio Quartulli
In
inv_icm42600_accel_convert_wom_to_roc()multiplyingthresholdbyconvertmay result in a number requiring more than 32bit.
32 bits
Quoted 1 line
In this case, although
valueis 64bit wide, the result is
64-bit
Quoted 2 lines
truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.
32-bit
32-bit
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 11 lines · Andy Shevchenko
Quoted 4 lines · Antonio Quartulli
In
inv_icm42600_accel_convert_wom_to_roc()multiplyingthresholdbyconvertmay result in a number requiring more than 32bit.32 bits
Quoted 1 line
In this case, although
valueis 64bit wide, the result is
Quoted 7 lines
64-bit
Quoted 2 lines
truncated because the multiplication is performed in the 32bit domain, due to both operands being 32bit long.
32-bit
32-bit
Thanks for highlighting those.
Should I send v2?
Regards,
Signature · 5 lines
Antonio Quartulli CEO and Co-Founder Mandelbit Srl https://www.mandelbit.com