From 0f5f11a5a4c441db664aa7aeb38b80fbfbce80fc Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Wed, 11 Oct 2023 20:15:16 -0700 Subject: [PATCH] Fix crash on unaligned volume scale Volume scaling would potentially crash when handling unaligned blocks of samples, and also handled them completely wrong. It should be counting up single samples until the buffer is aligned to a multiple of 16 bytes, and it should not exceed the intended count. BUG: It was not only counting the unaligned samples backwards, it was ignoring the real sample count. Fixes #380 Signed-off-by: Christopher Snowhill --- Audio/Chain/ConverterNode.m | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Audio/Chain/ConverterNode.m b/Audio/Chain/ConverterNode.m index 6ee7c8f8c..159d69c8c 100644 --- a/Audio/Chain/ConverterNode.m +++ b/Audio/Chain/ConverterNode.m @@ -76,15 +76,17 @@ void scale_by_volume(float *buffer, size_t count, float volume) { if(volume != 1.0) { size_t unaligned = (uintptr_t)buffer & 15; if(unaligned) { - size_t count3 = unaligned >> 2; - while(count3 > 0) { + size_t count_unaligned = (16 - unaligned) / sizeof(float); + while(count > 0 && count_unaligned > 0) { *buffer++ *= volume; - count3--; + count_unaligned--; count--; } } - vDSP_vsmul(buffer, 1, &volume, buffer, 1, count); + if(count) { + vDSP_vsmul(buffer, 1, &volume, buffer, 1, count); + } } }