From 7741e39c8dd2ca7b6a5c68c7af74ca0507690644 Mon Sep 17 00:00:00 2001
From: John Darrington <john@darrington.wattle.id.au>
Date: Tue, 5 Apr 2016 21:08:50 +0200
Subject: [PATCH] Fixed a crash when data for correlations was empty.

Found by zzuf.
---
 src/language/stats/correlations.c    | 17 +++++++++++------
 tests/language/stats/correlations.at | 25 +++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/src/language/stats/correlations.c b/src/language/stats/correlations.c
index ba56c135c4..ebfea2583e 100644
--- a/src/language/stats/correlations.c
+++ b/src/language/stats/correlations.c
@@ -286,8 +286,8 @@ run_corr (struct casereader *r, const struct corr_opts *opts, const struct corr
 {
   struct ccase *c;
   const gsl_matrix *var_matrix,  *samples_matrix, *mean_matrix;
-  gsl_matrix *cov_matrix;
-  gsl_matrix *corr_matrix;
+  gsl_matrix *cov_matrix = NULL;
+  gsl_matrix *corr_matrix = NULL;
   struct covariance *cov = covariance_2pass_create (corr->n_vars_total, corr->vars,
 						    NULL,
 						    opts->wv, opts->exclude);
@@ -302,11 +302,15 @@ run_corr (struct casereader *r, const struct corr_opts *opts, const struct corr
     {
       covariance_accumulate_pass2 (cov, c);
     }
-
-  cov_matrix = covariance_calculate (cov);
-
   casereader_destroy (rc);
-
+  
+  cov_matrix = covariance_calculate (cov);
+  if (! cov_matrix)
+    {
+      msg (SE, _("The data for the chosen variables are all missing or empty."));
+      goto error;
+    }
+  
   samples_matrix = covariance_moments (cov, MOMENT_NONE);
   var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
   mean_matrix = covariance_moments (cov, MOMENT_MEAN);
@@ -319,6 +323,7 @@ run_corr (struct casereader *r, const struct corr_opts *opts, const struct corr
   output_correlation (corr, opts, corr_matrix,
 		      samples_matrix, cov_matrix);
 
+ error:
   covariance_destroy (cov);
   gsl_matrix_free (corr_matrix);
   gsl_matrix_free (cov_matrix);
diff --git a/tests/language/stats/correlations.at b/tests/language/stats/correlations.at
index b2fd22f823..1de7eed195 100644
--- a/tests/language/stats/correlations.at
+++ b/tests/language/stats/correlations.at
@@ -361,3 +361,28 @@ CORRELATIONS 'VARIABLES = a b.]
 AT_CHECK([pspp -o pspp.csv correlations.sps], [1], [ignore])
 
 AT_CLEANUP
+
+dnl Another Crash found by zzuf
+AT_SETUP([CORRELATIONS -- empty dataset 2])
+
+AT_DATA([correlations.sps], [dnl
+data list notable list /foo * bar * wiz bang *.
+begin data.
+ 1     00      3           .
+ 3     9     -50           .
+98    78     104           .
+ .     4       4           .
+ 5     3       0           .
+end data.
+
+correlations
+        variables = foo bar wiz bang
+        /missing = listwise
+        .
+])
+
+AT_CHECK([pspp -O format=csv correlations.sps], [1], [dnl
+correlations.sps:13: error: CORRELATIONS: The data for the chosen variables are all missing or empty.
+])
+
+AT_CLEANUP
-- 
2.30.2