From: John Darrington <john@darrington.wattle.id.au>
Date: Fri, 21 May 2010 11:11:04 +0000 (+0200)
Subject: Fix crash on Windows when calculating a covariance matrix of dimension 1.
X-Git-Tag: v0.7.5~4
X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=930138c8279976294019300966e1fb842173f762;p=pspp-builds.git

Fix crash on Windows when calculating a covariance matrix of dimension 1.

Calculating a covariance matrix on a single variable crashed under Windows.
This change fixes that.  It also prevents GUI users from attempting to
perform a correlation with only one variable, since if they are trying to
do that, then the have probably made a mistake.
---

diff --git a/src/math/covariance.c b/src/math/covariance.c
index dc316692..d03f6ad0 100644
--- a/src/math/covariance.c
+++ b/src/math/covariance.c
@@ -135,7 +135,7 @@ covariance_1pass_create (size_t n_vars, const struct variable **vars,
 			 const struct variable *weight, enum mv_class exclude)
 {
   size_t i;
-  struct covariance *cov = xmalloc (sizeof *cov);
+  struct covariance *cov = xzalloc (sizeof *cov);
 
   cov->passes = 1;
   cov->state = 0;
@@ -156,7 +156,8 @@ covariance_1pass_create (size_t n_vars, const struct variable **vars,
 
   cov->n_cm = (n_vars * (n_vars - 1)  ) / 2;
 
-  cov->cm = xcalloc (sizeof *cov->cm, cov->n_cm);
+  if (cov->n_cm > 0)
+    cov->cm = xcalloc (sizeof *cov->cm, cov->n_cm);
   cov->categoricals = NULL;
 
   return cov;
diff --git a/src/ui/gui/correlation-dialog.c b/src/ui/gui/correlation-dialog.c
index 04e11ba1..afc23447 100644
--- a/src/ui/gui/correlation-dialog.c
+++ b/src/ui/gui/correlation-dialog.c
@@ -74,7 +74,7 @@ dialog_state_valid (gpointer data)
   GtkTreeModel *liststore =
     gtk_tree_view_get_model (GTK_TREE_VIEW (corr->variables));
 
-  if  (gtk_tree_model_iter_n_children (liststore, NULL) >= 1)
+  if  (gtk_tree_model_iter_n_children (liststore, NULL) > 1)
     return TRUE;
 
   return FALSE;