From d2126c8a2ee5e5cf4652c6b9d0234368180f348d Mon Sep 17 00:00:00 2001 From: Chris Cannam Date: Thu, 30 Jun 2022 09:32:39 +0100 Subject: [PATCH] Fix array overrun --- src/finer/Guide.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/finer/Guide.h b/src/finer/Guide.h index a7e5538..8162b01 100644 --- a/src/finer/Guide.h +++ b/src/finer/Guide.h @@ -458,12 +458,17 @@ protected: } double descendToValley(double f, const double *const magnitudes) const { + if (f == 0.0 || f == m_parameters.sampleRate/2.0) { + // These are special cases + return f; + } int b = binForFrequency(f, m_configuration.classificationFftSize, m_parameters.sampleRate); + int n = m_configuration.classificationFftSize/2; for (int i = 0; i < 3; ++i) { - if (magnitudes[b+1] < magnitudes[b]) { + if (b < n && magnitudes[b+1] < magnitudes[b]) { ++b; - } else if (magnitudes[b-1] < magnitudes[b]) { + } else if (b > 0 && magnitudes[b-1] < magnitudes[b]) { --b; } else { break;