FACTOR: prevent crash if the dataset is empty. 20100731040501/pspp
authorJohn Darrington <john@darrington.wattle.id.au>
Sat, 31 Jul 2010 09:55:18 +0000 (11:55 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 31 Jul 2010 09:55:18 +0000 (11:55 +0200)
The FACTOR command crashed if there were no cases
which had non-missing values for all variables.
This change emits a warning in this case and does
no analysis.

src/language/stats/factor.c
src/math/covariance.c
tests/language/stats/factor.at

index a2a8e5df037a15f0a67361d079002d7f70bc33e7..d50d6ad3fdf2c5b6eb6c86bbbe3e3044a893f4ac 100644 (file)
@@ -1694,6 +1694,12 @@ do_factor (const struct cmd_factor *factor, struct casereader *r)
 
   idata->cov = covariance_calculate (cov);
 
+  if (idata->cov == NULL)
+    {
+      msg (MW, _("The dataset contains no complete observations. No analysis will be performed."));
+      goto finish;
+    }
+
   var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
   mean_matrix = covariance_moments (cov, MOMENT_MEAN);
   idata->n = covariance_moments (cov, MOMENT_NONE);
index 61f54f50752704e7e9717378574930f3a01b4752..b5a4166dfa40643c5a71c88f37ceb7b4c604b1d5 100644 (file)
@@ -601,7 +601,8 @@ covariance_calculate_single_pass (struct covariance *cov)
 const gsl_matrix *
 covariance_calculate (struct covariance *cov)
 {
-  assert ( cov->state > 0 );
+  if ( cov->state <= 0 )
+    return NULL;
 
   switch (cov->passes)
     {
@@ -681,7 +682,8 @@ covariance_calculate_single_pass_unnormalized (struct covariance *cov)
 const gsl_matrix *
 covariance_calculate_unnormalized (struct covariance *cov)
 {
-  assert ( cov->state > 0 );
+  if ( cov->state <= 0 )
+    return NULL;
 
   switch (cov->passes)
     {
index f1d4b2360b6f798c78b9f67ecfba0ec30e8a204b..ca01ca80513a2e800f996967e1f085eefefe5c9a 100644 (file)
@@ -1775,4 +1775,24 @@ science,.900,.198
 socst,.222,.922
 ])
 
-AT_CLEANUP
\ No newline at end of file
+AT_CLEANUP
+
+
+
+AT_SETUP([FACTOR empty dataset])
+dnl Test that something sane happens when the dataset contains no complete observations
+
+AT_DATA([factor-empty.sps],
+  [data list notable list /x * y * z *.
+begin data.
+3.4        .     92.9
+.        32.09   34.2
+1.00     19.80     .
+2.00       .      3.6
+end data.
+
+factor /variables = ALL.
+])
+
+AT_CHECK([pspp -O format=csv factor-empty.sps], [0], [ignore])
+AT_CLEANUP