Skip to content

Commit

Permalink
Fix crash on unaligned volume scale
Browse files Browse the repository at this point in the history
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 <kode54@gmail.com>
  • Loading branch information
kode54 committed Oct 12, 2023
1 parent 1c95771 commit 42ea824
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Audio/Chain/ConverterNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 42ea824

Please sign in to comment.