linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Input: max77693-haptic - fix potential overflow
@ 2014-10-28 16:43 Dmitry Torokhov
  2014-10-29  3:22 ` Jaewon Kim
  2014-10-29  4:34 ` Chanwoo Choi
  0 siblings, 2 replies; 3+ messages in thread
From: Dmitry Torokhov @ 2014-10-28 16:43 UTC (permalink / raw)
  To: linux-input; +Cc: Chanwoo Choi, Jaewon Kim, linux-kernel

Expression haptic->pwm_dev->period * haptic->magnitude is of type 'unsigned
int' and may overflow. We need to convert one of the operands to u64 before
multiplying, instead of casting result (potentially overflown) to u64.

Reported by Coverity: CID 1248753

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/misc/max77693-haptic.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/input/misc/max77693-haptic.c b/drivers/input/misc/max77693-haptic.c
index 7b1fde9..ef6a9d6 100644
--- a/drivers/input/misc/max77693-haptic.c
+++ b/drivers/input/misc/max77693-haptic.c
@@ -194,7 +194,7 @@ static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
 				       struct ff_effect *effect)
 {
 	struct max77693_haptic *haptic = input_get_drvdata(dev);
-	uint64_t period_mag_multi;
+	u64 period_mag_multi;
 
 	haptic->magnitude = effect->u.rumble.strong_magnitude;
 	if (!haptic->magnitude)
@@ -205,8 +205,7 @@ static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
 	 * The formula to convert magnitude to pwm_duty as follows:
 	 * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
 	 */
-	period_mag_multi = (int64_t)(haptic->pwm_dev->period *
-						haptic->magnitude);
+	period_mag_multi = (u64)haptic->pwm_dev->period * haptic->magnitude;
 	haptic->pwm_duty = (unsigned int)(period_mag_multi >>
 						MAX_MAGNITUDE_SHIFT);
 
-- 
2.1.0.rc2.206.gedb03e5


-- 
Dmitry

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Input: max77693-haptic - fix potential overflow
  2014-10-28 16:43 [PATCH] Input: max77693-haptic - fix potential overflow Dmitry Torokhov
@ 2014-10-29  3:22 ` Jaewon Kim
  2014-10-29  4:34 ` Chanwoo Choi
  1 sibling, 0 replies; 3+ messages in thread
From: Jaewon Kim @ 2014-10-29  3:22 UTC (permalink / raw)
  To: Dmitry Torokhov, linux-input; +Cc: Chanwoo Choi, linux-kernel

Hi Dmitry,

2014년 10월 29일 01:43에 Dmitry Torokhov 이(가) 쓴 글:
> Expression haptic->pwm_dev->period * haptic->magnitude is of type 'unsigned
> int' and may overflow. We need to convert one of the operands to u64 before
> multiplying, instead of casting result (potentially overflown) to u64.
>
> Reported by Coverity: CID 1248753
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>   drivers/input/misc/max77693-haptic.c | 5 ++---
>   1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/input/misc/max77693-haptic.c b/drivers/input/misc/max77693-haptic.c
> index 7b1fde9..ef6a9d6 100644
> --- a/drivers/input/misc/max77693-haptic.c
> +++ b/drivers/input/misc/max77693-haptic.c
> @@ -194,7 +194,7 @@ static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
>   				       struct ff_effect *effect)
>   {
>   	struct max77693_haptic *haptic = input_get_drvdata(dev);
> -	uint64_t period_mag_multi;
> +	u64 period_mag_multi;
>   
>   	haptic->magnitude = effect->u.rumble.strong_magnitude;
>   	if (!haptic->magnitude)
> @@ -205,8 +205,7 @@ static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
>   	 * The formula to convert magnitude to pwm_duty as follows:
>   	 * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
>   	 */
> -	period_mag_multi = (int64_t)(haptic->pwm_dev->period *
> -						haptic->magnitude);
> +	period_mag_multi = (u64)haptic->pwm_dev->period * haptic->magnitude;
>   	haptic->pwm_duty = (unsigned int)(period_mag_multi >>
>   						MAX_MAGNITUDE_SHIFT);
>   


There was casting miss in multiplying.
Thanks.

Acked-by : Jaewon Kim <jaewon02.kim@samsung.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Input: max77693-haptic - fix potential overflow
  2014-10-28 16:43 [PATCH] Input: max77693-haptic - fix potential overflow Dmitry Torokhov
  2014-10-29  3:22 ` Jaewon Kim
@ 2014-10-29  4:34 ` Chanwoo Choi
  1 sibling, 0 replies; 3+ messages in thread
From: Chanwoo Choi @ 2014-10-29  4:34 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: linux-input, Jaewon Kim, linux-kernel

On 10/29/2014 01:43 AM, Dmitry Torokhov wrote:
> Expression haptic->pwm_dev->period * haptic->magnitude is of type 'unsigned
> int' and may overflow. We need to convert one of the operands to u64 before
> multiplying, instead of casting result (potentially overflown) to u64.
> 
> Reported by Coverity: CID 1248753
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/misc/max77693-haptic.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/input/misc/max77693-haptic.c b/drivers/input/misc/max77693-haptic.c
> index 7b1fde9..ef6a9d6 100644
> --- a/drivers/input/misc/max77693-haptic.c
> +++ b/drivers/input/misc/max77693-haptic.c
> @@ -194,7 +194,7 @@ static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
>  				       struct ff_effect *effect)
>  {
>  	struct max77693_haptic *haptic = input_get_drvdata(dev);
> -	uint64_t period_mag_multi;
> +	u64 period_mag_multi;
>  
>  	haptic->magnitude = effect->u.rumble.strong_magnitude;
>  	if (!haptic->magnitude)
> @@ -205,8 +205,7 @@ static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
>  	 * The formula to convert magnitude to pwm_duty as follows:
>  	 * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
>  	 */
> -	period_mag_multi = (int64_t)(haptic->pwm_dev->period *
> -						haptic->magnitude);
> +	period_mag_multi = (u64)haptic->pwm_dev->period * haptic->magnitude;
>  	haptic->pwm_duty = (unsigned int)(period_mag_multi >>
>  						MAX_MAGNITUDE_SHIFT);
>  
> 

Looks good to me.

Reviewed-by: Chanwoo Choi <cw00.choi@samsung.com>

Thanks,
Chanwoo Choi

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-10-29  4:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-28 16:43 [PATCH] Input: max77693-haptic - fix potential overflow Dmitry Torokhov
2014-10-29  3:22 ` Jaewon Kim
2014-10-29  4:34 ` Chanwoo Choi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).