staging: comedi: dt2811: fix integer overflow in multiply
Subsystems (2)
PATCH 1/1 staging: comedi: dt2811: fix integer overflow in multiply
From: Colin Ian King <[email protected]>
Multiplying two unsigned ints leads to an unsigned int result. The intention is that the result is a unsigned long long, so to fix the overflow cast the div to an unsigned long long to ensure that the multiplication is on unsigned long longs to avoid overflow.
Detected by CoverityScan, CID#1357597 ("Unintentioal integer overflow")
--- drivers/staging/comedi/drivers/dt2811.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/comedi/drivers/dt2811.c +1 -1
@@ -323,7 +323,8 @@ static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,for (_mult =0 ; _mult <=7 ; _mult++) {unsigned int div = dt2811_clk_dividers[_div];unsigned int mult = dt2811_clk_multipliers[_mult]; -unsigned long long divider = div * mult; + unsigned long long divider + (unsigned long long)div * mult;unsigned int divisor =DT2811_TMRCTR_MANTISSA (_div) |DT2811_TMRCTR_EXPONENT (_mult);
Signature · 1 line
2.20.1
Quoted 20 lines · Colin King
From: Colin Ian King <[email protected]>
Multiplying two unsigned ints leads to an unsigned int result. The intention is that the result is a unsigned long long, so to fix the overflow cast the div to an unsigned long long to ensure that the multiplication is on unsigned long longs to avoid overflow.
Detected by CoverityScan, CID#1357597 ("Unintentioal integer overflow")
Fixes f2975a9b2ab9 ("staging: comedi: dt2811: add async command support for AI subdevice")Signed-off-by Colin Ian King <[email protected]>--- drivers/staging/comedi/drivers/dt2811.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/staging/comedi/drivers/dt2811.c +0
-0
drivers/staging/comedi/drivers/dt2811.c static unsigned int dt2811_ns_to_timer(unsigned int *nanosec, · line 326
Quoted 5 lines
drivers/staging/comedi/drivers/dt2811.c +1
-1@@ -323,7 +323,8 @@ static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,for (_mult =0 ; _mult <=7 ; _mult++) {unsigned int div = dt2811_clk_dividers[_div];unsigned int mult = dt2811_clk_multipliers[_mult]; -unsigned long long divider = div * mult; + unsigned long long divider > + (unsigned long long)div * mult;
The max "div" can be is 12. The max "mult" can be is 10,000,000. So this is a false positive because there is no overflow. The code is not complicated. Unfortunately, Smatch has the exact same problem...
We should fix the checker instead of the code.
regards,
dan carpenter
Quoted 6 lines · Dan Carpenter
Quoted 6 lines
drivers/staging/comedi/drivers/dt2811.c +0
-0@@ -323,7 +323,8 @@ static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,for (_mult =0 ; _mult <=7 ; _mult++) {
Quoted 8 lines
drivers/staging/comedi/drivers/dt2811.c static unsigned int dt2811_ns_to_timer(unsigned int *nanosec,
Quoted 4 lines
drivers/staging/comedi/drivers/dt2811.c +1
-1unsigned int div = dt2811_clk_dividers[_div];unsigned int mult = dt2811_clk_multipliers[_mult]; -unsigned long long divider = div * mult; + unsigned long long divider > > + (unsigned long long)div * mult;The max "div" can be is 12. The max "mult" can be is 10,000,000. So this is a false positive because there is no overflow. The code is not complicated. Unfortunately, Smatch has the exact same problem...
Smatch actually parses this correctly, but I had a power failure over the weekend that messed up my results.
regards,
dan carpenter