From 04725cb1eff561d0d8547020b1d2ab3ff51fc990 Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Thu, 12 Apr 2012 23:24:44 -0700 Subject: [PATCH] Fixed a stack overflow bug when using the regression fitting command in logfile plot. Solution involved just allocating on the heap instead of the stack. --- src/ui/QGCDataPlot2D.cc | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/ui/QGCDataPlot2D.cc b/src/ui/QGCDataPlot2D.cc index 94d6426c3..d23a56d4e 100644 --- a/src/ui/QGCDataPlot2D.cc +++ b/src/ui/QGCDataPlot2D.cc @@ -561,7 +561,10 @@ void QGCDataPlot2D::loadCsvLog(QString file, QString xAxisName, QString yAxisFil bool QGCDataPlot2D::calculateRegression() { // TODO Add support for quadratic / cubic curve fitting - return calculateRegression(ui->xRegressionComboBox->currentText(), ui->yRegressionComboBox->currentText(), "linear"); + qDebug() << ui->xRegressionComboBox->currentText(); + qDebug() << ui->yRegressionComboBox->currentText(); + //return false; + return calculateRegression(ui->xRegressionComboBox->currentText(), ui->yRegressionComboBox->currentText(), "quadratic"); } /** @@ -579,9 +582,15 @@ bool QGCDataPlot2D::calculateRegression(QString xName, QString yName, QString me ui->xRegressionComboBox->setCurrentIndex(curveNames.indexOf(xName)); ui->yRegressionComboBox->setCurrentIndex(curveNames.indexOf(yName)); } + + // Create a couple of arrays for us to use to temporarily store some of the data from the plot. + // These arrays are allocated on the heap as they are far too big to go in the stack and will + // cause an overflow. + // TODO: Look into if this would be better done by having a getter return const double pointers instead + // of using memcpy(). const int size = 100000; - double x[size]; - double y[size]; + double *x = new double[size]; + double *y = new double[size]; int copied = plot->data(yName, x, y, size); if (method == "linear") { @@ -604,14 +613,12 @@ bool QGCDataPlot2D::calculateRegression(QString xName, QString yName, QString me function = tr("Linear regression failed. (Limit: %1 data points. Try with less)").arg(size); result = false; } - } else if (method == "quadratic") { - - } else if (method == "cubic") { - } else { function = tr("Regression method %1 not found").arg(method); result = false; } + + delete x, y; } else { // xName == yName function = tr("Please select different X and Y dimensions, not %1 = %2").arg(xName, yName); @@ -635,7 +642,7 @@ bool QGCDataPlot2D::calculateRegression(QString xName, QString yName, QString me * the match of the regression. * @return 1 on success, 0 on failure (e.g. because of infinite slope) */ -int QGCDataPlot2D::linearRegression(double* x,double* y,int n,double* a,double* b,double* r) +int QGCDataPlot2D::linearRegression(double *x, double *y, int n, double *a, double *b, double *r) { int i; double sumx=0,sumy=0,sumx2=0,sumy2=0,sumxy=0; -- 2.22.0