From f27674ef4cecbc1c2ebdb916f7be7227fda5de5f Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 31 Oct 2024 11:22:38 -0400 Subject: [PATCH] Fix a boudns bug that could crash empty zones (#1441) The unison fix resulted in an oob array read which could speciously cause crashes. Fix. Also note to self - do an ASAN run soon! --- src/voice/voice.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/voice/voice.cpp b/src/voice/voice.cpp index ef066f90..eec4bd79 100644 --- a/src/voice/voice.cpp +++ b/src/voice/voice.cpp @@ -255,9 +255,18 @@ template bool Voice::processWithOS() auto fpitch = calculateVoicePitch(); auto [firstIndex, lastIndex] = sampleIndexRange(); - for (auto i = firstIndex; i < lastIndex; ++i) - if (zone->samplePointers[i]) - calculateGeneratorRatio(fpitch, i, i - firstIndex); + if (firstIndex >= 0) + { + for (auto i = firstIndex; i < lastIndex; ++i) + { + assert(i >= 0); + assert(i < zone->samplePointers.size()); + if (zone->samplePointers[i]) + { + calculateGeneratorRatio(fpitch, i, i - firstIndex); + } + } + } if (useOversampling) for (auto i = 0; i < numGeneratorsActive; ++i) @@ -874,13 +883,13 @@ std::pair Voice::sampleIndexRange() const if (zone->variantData.variantPlaybackMode == engine::Zone::UNISON) { - firstIndex = 0; lastIndex = 0; for (int i = 0; i < maxVariantsPerZone; ++i) { if (zone->variantData.variants[i].active) { + firstIndex = 0; lastIndex++; } }