diff mbox series

[next] iio: hrtimer-trigger: Fix potential integer overflow in iio_hrtimer_store_sampling_frequency

Message ID 20210329205817.GA188755@embeddedor (mailing list archive)
State New, archived
Headers show
Series [next] iio: hrtimer-trigger: Fix potential integer overflow in iio_hrtimer_store_sampling_frequency | expand

Commit Message

Gustavo A. R. Silva March 29, 2021, 8:58 p.m. UTC
Add suffix ULL to constant 1000 in order to avoid a potential integer
overflow and give the compiler complete information about the proper
arithmetic to use. Notice that this constant is being used in a context
that expects an expression of type unsigned long long, but it's
currently evaluated using 32-bit arithmetic.

Addresses-Coverity-ID: 1503062 ("Unintentional integer overflow")
Fixes: dafcf4ed8392 ("iio: hrtimer: Allow sub Hz granularity")
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/iio/trigger/iio-trig-hrtimer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Jonathan Cameron April 5, 2021, 4:26 p.m. UTC | #1
On Mon, 29 Mar 2021 15:58:17 -0500
"Gustavo A. R. Silva" <gustavoars@kernel.org> wrote:

> Add suffix ULL to constant 1000 in order to avoid a potential integer
> overflow and give the compiler complete information about the proper
> arithmetic to use. Notice that this constant is being used in a context
> that expects an expression of type unsigned long long, but it's
> currently evaluated using 32-bit arithmetic.
> 
> Addresses-Coverity-ID: 1503062 ("Unintentional integer overflow")
> Fixes: dafcf4ed8392 ("iio: hrtimer: Allow sub Hz granularity")
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Thanks, Applied to the togreg branch of iio.git and pushed out as testing
for 0-day to poke at it.

Thanks,

Jonathan

> ---
>  drivers/iio/trigger/iio-trig-hrtimer.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/trigger/iio-trig-hrtimer.c b/drivers/iio/trigger/iio-trig-hrtimer.c
> index 51e362f091c2..716c795d08fb 100644
> --- a/drivers/iio/trigger/iio-trig-hrtimer.c
> +++ b/drivers/iio/trigger/iio-trig-hrtimer.c
> @@ -63,7 +63,7 @@ ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
>  	if (integer < 0 || fract < 0)
>  		return -ERANGE;
>  
> -	val = fract + 1000 * integer;  /* mHz */
> +	val = fract + 1000ULL * integer;  /* mHz */
>  
>  	if (!val || val > UINT_MAX)
>  		return -EINVAL;
Gustavo A. R. Silva April 6, 2021, 10:13 a.m. UTC | #2
On 4/5/21 11:26, Jonathan Cameron wrote:
> On Mon, 29 Mar 2021 15:58:17 -0500
> "Gustavo A. R. Silva" <gustavoars@kernel.org> wrote:
> 
>> Add suffix ULL to constant 1000 in order to avoid a potential integer
>> overflow and give the compiler complete information about the proper
>> arithmetic to use. Notice that this constant is being used in a context
>> that expects an expression of type unsigned long long, but it's
>> currently evaluated using 32-bit arithmetic.
>>
>> Addresses-Coverity-ID: 1503062 ("Unintentional integer overflow")
>> Fixes: dafcf4ed8392 ("iio: hrtimer: Allow sub Hz granularity")
>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> 
> Thanks, Applied to the togreg branch of iio.git and pushed out as testing
> for 0-day to poke at it.

Awesome. :)

Thanks, Jonathan.

--
Gustavo
diff mbox series

Patch

diff --git a/drivers/iio/trigger/iio-trig-hrtimer.c b/drivers/iio/trigger/iio-trig-hrtimer.c
index 51e362f091c2..716c795d08fb 100644
--- a/drivers/iio/trigger/iio-trig-hrtimer.c
+++ b/drivers/iio/trigger/iio-trig-hrtimer.c
@@ -63,7 +63,7 @@  ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
 	if (integer < 0 || fract < 0)
 		return -ERANGE;
 
-	val = fract + 1000 * integer;  /* mHz */
+	val = fract + 1000ULL * integer;  /* mHz */
 
 	if (!val || val > UINT_MAX)
 		return -EINVAL;