All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ASoC: fsl_spdif: Fix integer overflow when calculating divisors
@ 2014-06-09 16:16 Anssi Hannula
  2014-06-09 17:26 ` Nicolin Chen
  2014-06-09 20:00 ` Mark Brown
  0 siblings, 2 replies; 3+ messages in thread
From: Anssi Hannula @ 2014-06-09 16:16 UTC (permalink / raw)
  To: Mark Brown; +Cc: alsa-devel, Nicolin Chen

The calculation code does
u64 = (u32 - u32) * 100000;

The 64 bits are of no help here as the type is casted only after the
multiplication, and therefore the result may overflow, possibly causing
inoptimal or wrong clock setup in an unfortunate case (the maximum
result value of the first substraction is currently 47999).

Fix the code to cast before multiplication.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
Cc: Nicolin Chen <Guangyu.Chen@freescale.com>
---
 sound/soc/fsl/fsl_spdif.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sound/soc/fsl/fsl_spdif.c b/sound/soc/fsl/fsl_spdif.c
index b912d45a2a4c..0bd9136279a2 100644
--- a/sound/soc/fsl/fsl_spdif.c
+++ b/sound/soc/fsl/fsl_spdif.c
@@ -1076,7 +1076,7 @@ static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
 				goto out;
 			} else if (arate / rate[index] == 1) {
 				/* A little bigger than expect */
-				sub = (arate - rate[index]) * 100000;
+				sub = (u64)(arate - rate[index]) * 100000;
 				do_div(sub, rate[index]);
 				if (sub >= savesub)
 					continue;
@@ -1086,7 +1086,7 @@ static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
 				spdif_priv->txrate[index] = arate;
 			} else if (rate[index] / arate == 1) {
 				/* A little smaller than expect */
-				sub = (rate[index] - arate) * 100000;
+				sub = (u64)(rate[index] - arate) * 100000;
 				do_div(sub, rate[index]);
 				if (sub >= savesub)
 					continue;
-- 
1.8.4.5

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

* Re: [PATCH] ASoC: fsl_spdif: Fix integer overflow when calculating divisors
  2014-06-09 16:16 [PATCH] ASoC: fsl_spdif: Fix integer overflow when calculating divisors Anssi Hannula
@ 2014-06-09 17:26 ` Nicolin Chen
  2014-06-09 20:00 ` Mark Brown
  1 sibling, 0 replies; 3+ messages in thread
From: Nicolin Chen @ 2014-06-09 17:26 UTC (permalink / raw)
  To: Anssi Hannula; +Cc: alsa-devel, Mark Brown

On Mon, Jun 09, 2014 at 07:16:43PM +0300, Anssi Hannula wrote:
> The calculation code does
> u64 = (u32 - u32) * 100000;
> 
> The 64 bits are of no help here as the type is casted only after the
> multiplication, and therefore the result may overflow, possibly causing
> inoptimal or wrong clock setup in an unfortunate case (the maximum
> result value of the first substraction is currently 47999).
> 
> Fix the code to cast before multiplication.
> 
> Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
> Cc: Nicolin Chen <Guangyu.Chen@freescale.com>

Acked-by: Nicolin Chen <Guangyu.Chen@freescale.com>

Thank you so much :)
Nicolin

> ---
>  sound/soc/fsl/fsl_spdif.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/sound/soc/fsl/fsl_spdif.c b/sound/soc/fsl/fsl_spdif.c
> index b912d45a2a4c..0bd9136279a2 100644
> --- a/sound/soc/fsl/fsl_spdif.c
> +++ b/sound/soc/fsl/fsl_spdif.c
> @@ -1076,7 +1076,7 @@ static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
>  				goto out;
>  			} else if (arate / rate[index] == 1) {
>  				/* A little bigger than expect */
> -				sub = (arate - rate[index]) * 100000;
> +				sub = (u64)(arate - rate[index]) * 100000;
>  				do_div(sub, rate[index]);
>  				if (sub >= savesub)
>  					continue;
> @@ -1086,7 +1086,7 @@ static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
>  				spdif_priv->txrate[index] = arate;
>  			} else if (rate[index] / arate == 1) {
>  				/* A little smaller than expect */
> -				sub = (rate[index] - arate) * 100000;
> +				sub = (u64)(rate[index] - arate) * 100000;
>  				do_div(sub, rate[index]);
>  				if (sub >= savesub)
>  					continue;
> -- 
> 1.8.4.5
> 

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

* Re: [PATCH] ASoC: fsl_spdif: Fix integer overflow when calculating divisors
  2014-06-09 16:16 [PATCH] ASoC: fsl_spdif: Fix integer overflow when calculating divisors Anssi Hannula
  2014-06-09 17:26 ` Nicolin Chen
@ 2014-06-09 20:00 ` Mark Brown
  1 sibling, 0 replies; 3+ messages in thread
From: Mark Brown @ 2014-06-09 20:00 UTC (permalink / raw)
  To: Anssi Hannula; +Cc: alsa-devel, Nicolin Chen


[-- Attachment #1.1: Type: text/plain, Size: 139 bytes --]

On Mon, Jun 09, 2014 at 07:16:43PM +0300, Anssi Hannula wrote:
> The calculation code does
> u64 = (u32 - u32) * 100000;

Applied, thanks.

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

[-- Attachment #2: Type: text/plain, Size: 0 bytes --]



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

end of thread, other threads:[~2014-06-09 20:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-09 16:16 [PATCH] ASoC: fsl_spdif: Fix integer overflow when calculating divisors Anssi Hannula
2014-06-09 17:26 ` Nicolin Chen
2014-06-09 20:00 ` Mark Brown

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.