diff mbox series

ASoC: stm: Prevent potential division by zero in stm32_sai_mclk_round_rate()

Message ID 20241106014654.206860-1-luoyifan@cmss.chinamobile.com (mailing list archive)
State New
Headers show
Series ASoC: stm: Prevent potential division by zero in stm32_sai_mclk_round_rate() | expand

Commit Message

Luo Yifan Nov. 6, 2024, 1:46 a.m. UTC
This patch checks if div is less than or equal to zero (div <= 0). If
div is zero or negative, the function returns -EINVAL, ensuring the
division operation (*prate / div) is safe to perform.

Signed-off-by: Luo Yifan <luoyifan@cmss.chinamobile.com>
---
 sound/soc/stm/stm32_sai_sub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Olivier Moysan Nov. 6, 2024, 9:27 a.m. UTC | #1
Hi Luo,

On 11/6/24 02:46, Luo Yifan wrote:
> This patch checks if div is less than or equal to zero (div <= 0). If
> div is zero or negative, the function returns -EINVAL, ensuring the
> division operation (*prate / div) is safe to perform.
> 
> Signed-off-by: Luo Yifan <luoyifan@cmss.chinamobile.com>
> ---
>   sound/soc/stm/stm32_sai_sub.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/sound/soc/stm/stm32_sai_sub.c b/sound/soc/stm/stm32_sai_sub.c
> index 7bc4a96b7..2570daa3e 100644
> --- a/sound/soc/stm/stm32_sai_sub.c
> +++ b/sound/soc/stm/stm32_sai_sub.c
> @@ -378,8 +378,8 @@ static long stm32_sai_mclk_round_rate(struct clk_hw *hw, unsigned long rate,
>   	int div;
>   
>   	div = stm32_sai_get_clk_div(sai, *prate, rate);
> -	if (div < 0)
> -		return div;
> +	if (div <= 0)
> +		return -EINVAL;
>   
>   	mclk->freq = *prate / div;
>   

Thanks for your patch. It looks fine, but I think that it has to
be extended.

In CR1 register, MCKDIV = 0 gives the same result as MCKDIV = 1.
But while MCKDIV = 0 is valid, for sure div = 0 is not valid.

I agree that that div = 0 has to be managed as an error
This could be rather handled in stm32_sai_get_clk_div() function itself,
by returning an error, if div is null.
This is relevant as we may also get an error on test "if (input_rate % 
div)".
I suggest to add a specific test and error message to handle this case 
in stm32_sai_get_clk_div().
Something like:
if (!div)) {
	dev_err(&sai->pdev->dev, "Invalid null divider\n");
	return -EINVAL;
}

BRs
Olivier
Luo Yifan Nov. 7, 2024, 1:45 a.m. UTC | #2
Sure, I can submit a new patch with specific tests and error messages added to stm32_sai_get_clk_div().
diff mbox series

Patch

diff --git a/sound/soc/stm/stm32_sai_sub.c b/sound/soc/stm/stm32_sai_sub.c
index 7bc4a96b7..2570daa3e 100644
--- a/sound/soc/stm/stm32_sai_sub.c
+++ b/sound/soc/stm/stm32_sai_sub.c
@@ -378,8 +378,8 @@  static long stm32_sai_mclk_round_rate(struct clk_hw *hw, unsigned long rate,
 	int div;
 
 	div = stm32_sai_get_clk_div(sai, *prate, rate);
-	if (div < 0)
-		return div;
+	if (div <= 0)
+		return -EINVAL;
 
 	mclk->freq = *prate / div;