diff mbox series

[1/1] sound: avoid endless loop

Message ID 20221204130513.43406-1-heinrich.schuchardt@canonical.com
State Superseded, archived
Delegated to: Tom Rini
Headers show
Series [1/1] sound: avoid endless loop | expand

Commit Message

Heinrich Schuchardt Dec. 4, 2022, 1:05 p.m. UTC
'sound play 1 100000' results in an endless loop on the sandbox.

If the frequency exceeds half the sampling rate, zero out the output
buffer.

Fixes: 511ed5fdd389 ("SOUND: SAMSUNG: Add I2S driver")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
 drivers/sound/sound.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Simon Glass Dec. 4, 2022, 9:17 p.m. UTC | #1
On Mon, 5 Dec 2022 at 02:05, Heinrich Schuchardt
<heinrich.schuchardt@canonical.com> wrote:
>
> 'sound play 1 100000' results in an endless loop on the sandbox.
>
> If the frequency exceeds half the sampling rate, zero out the output
> buffer.
>
> Fixes: 511ed5fdd389 ("SOUND: SAMSUNG: Add I2S driver")
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> ---
>  drivers/sound/sound.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)

Reviewed-by: Simon Glass <sjg@chromium.org>

(nit below)

>
> diff --git a/drivers/sound/sound.c b/drivers/sound/sound.c
> index 041dfdccfe..c0fc50c99d 100644
> --- a/drivers/sound/sound.c
> +++ b/drivers/sound/sound.c
> @@ -15,7 +15,10 @@ void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
>         const int period = freq ? sample_rate / freq : 0;
>         const int half = period / 2;
>
> -       assert(freq);
> +       if (!half) {
> +               memset(data, 0, size);

'\0'


> +               return;
> +       }
>
>         /* Make sure we don't overflow our buffer */
>         if (size % 2)
> --
> 2.37.2
>
Heinrich Schuchardt Dec. 4, 2022, 9:45 p.m. UTC | #2
On 12/4/22 22:17, Simon Glass wrote:
> On Mon, 5 Dec 2022 at 02:05, Heinrich Schuchardt
> <heinrich.schuchardt@canonical.com> wrote:
>>
>> 'sound play 1 100000' results in an endless loop on the sandbox.
>>
>> If the frequency exceeds half the sampling rate, zero out the output
>> buffer.
>>
>> Fixes: 511ed5fdd389 ("SOUND: SAMSUNG: Add I2S driver")
>> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
>> ---
>>   drivers/sound/sound.c | 5 ++++-
>>   1 file changed, 4 insertions(+), 1 deletion(-)
> 
> Reviewed-by: Simon Glass <sjg@chromium.org>
> 
> (nit below)
> 
>>
>> diff --git a/drivers/sound/sound.c b/drivers/sound/sound.c
>> index 041dfdccfe..c0fc50c99d 100644
>> --- a/drivers/sound/sound.c
>> +++ b/drivers/sound/sound.c
>> @@ -15,7 +15,10 @@ void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
>>          const int period = freq ? sample_rate / freq : 0;
>>          const int half = period / 2;
>>
>> -       assert(freq);
>> +       if (!half) {
>> +               memset(data, 0, size);
> 
> '\0'

Please, have a look at the function definition:

include/malloc.h:366:
void* memset(void*, int, size_t);

Best regards

Heinrich

> 
> 
>> +               return;
>> +       }
>>
>>          /* Make sure we don't overflow our buffer */
>>          if (size % 2)
>> --
>> 2.37.2
>>
diff mbox series

Patch

diff --git a/drivers/sound/sound.c b/drivers/sound/sound.c
index 041dfdccfe..c0fc50c99d 100644
--- a/drivers/sound/sound.c
+++ b/drivers/sound/sound.c
@@ -15,7 +15,10 @@  void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
 	const int period = freq ? sample_rate / freq : 0;
 	const int half = period / 2;
 
-	assert(freq);
+	if (!half) {
+		memset(data, 0, size);
+		return;
+	}
 
 	/* Make sure we don't overflow our buffer */
 	if (size % 2)