Hotfix crashes for [extreme] time-stretch -- #7305
authorRobin Gareus <robin@gareus.org>
Sat, 1 Apr 2017 17:10:08 +0000 (19:10 +0200)
committerRobin Gareus <robin@gareus.org>
Sat, 1 Apr 2017 17:16:12 +0000 (19:16 +0200)
e.g. stretch-shrink 3712 samples down to 1780.
The filter order defines nFact which can become larger than length - 2
leading to out-of-bounds array access.

e.g. m_ord = 2 -> nFilt = 2, nFact = 6;  process < 7 samples (here 6)

libs/qm-dsp/dsp/signalconditioning/FiltFilt.cpp

index 89076c150f22582faffeaa35f8d4b18a50be46c9..03b21138a44d7985e003324a87564340d153e8ad 100644 (file)
@@ -13,6 +13,7 @@
     COPYING included with this distribution for more information.
 */
 
+#include <stdio.h>
 #include "FiltFilt.h"
 
 //////////////////////////////////////////////////////////////////////
@@ -55,6 +56,14 @@ void FiltFilt::process(double *src, double *dst, unsigned int length)
 
     if (length == 0) return;
 
+    if (length < 2) {
+       fprintf (stderr, "FiltFilt::process called for %d samples\n", length);
+       for( i = 0; i < length; i++ ) {
+           dst[i] = src [i];
+       }
+       return;
+    }
+
     unsigned int nFilt = m_ord + 1;
     unsigned int nFact = 3 * ( nFilt - 1);
     unsigned int nExt  = length + 2 * nFact;
@@ -79,11 +88,16 @@ void FiltFilt::process(double *src, double *dst, unsigned int length)
        m_filtScratchIn[ index++ ] = sample0 - src[ i ];
     }
     index = 0;
-    for( i = 0; i < nFact; i++ )
+    for( i = 0; i < nFact && i + 2 < length; i++ )
     {
        m_filtScratchIn[ (nExt - nFact) + index++ ] = sampleN - src[ (length - 2) - i ];
     }
 
+    for(; i < nFact; i++ )
+    {
+       m_filtScratchIn[ (nExt - nFact) + index++ ] = 0;
+    }
+
     index = 0;
     for( i = 0; i < length; i++ )
     {