Fix a crashes where histograms of a single datum were trying to be created.
authorJohn Darrington <john@darrington.wattle.id.au>
Sun, 25 Mar 2012 07:14:39 +0000 (09:14 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sun, 25 Mar 2012 07:14:39 +0000 (09:14 +0200)
Reported-by: Ben Pfaff.
src/language/stats/examine.c
src/language/stats/frequencies.q
src/math/histogram.c
tests/language/stats/examine.at
tests/language/stats/frequencies.at

index dc53d806c0869c8da9b1870a9dfae2265ddd9a2c..45c64cbb5526bd17e9dfb349aa69215d14cd8ae2 100644 (file)
@@ -479,6 +479,10 @@ show_histogram (const struct examine *cmd, int iact_idx)
             categoricals_get_user_data_by_category_real (cmd->cats, iact_idx, grp);
 
           struct string label;
+
+         if (es[v].histogram == NULL)
+           continue;
+
           ds_init_cstr (&label, 
                         var_to_string (cmd->dep_vars[v]));
 
index d7754220fa4efe7fc2a4861ef4a806fb9deb29b8..b9da4677d18b8599d97a6449343e5e5e22c37031 100644 (file)
@@ -499,14 +499,17 @@ postcalc (struct frq_proc *frq, const struct dataset *ds)
 
          histogram = freq_tab_to_hist (frq, &vf->tab, vf->var);
 
-          chart_item_submit (histogram_chart_create (
+         if ( histogram)
+           {
+             chart_item_submit (histogram_chart_create (
                                histogram->gsl_hist, var_to_string(vf->var),
                                vf->tab.valid_cases,
                                d[FRQ_MEAN],
                                d[FRQ_STDDEV],
                                frq->hist->draw_normal));
 
-         statistic_destroy (&histogram->parent);
+             statistic_destroy (&histogram->parent);
+           }
        }
 
       if (frq->pie)
@@ -1142,6 +1145,10 @@ freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft,
   bin_width = chart_rounded_tick (bin_width);
 
   histogram = histogram_create (bin_width, x_min, x_max);
+
+  if ( histogram == NULL)
+    return NULL;
+
   for (i = 0; i < ft->n_valid; i++)
     {
       const struct freq *f = &ft->valid[i];
index 9b89df689527996d1d4393f8f21fb56e3bba494f..2f19f310954c5d767a12ee309986c9f87cc87ede 100644 (file)
 #include <gsl/gsl_histogram.h>
 #include <math.h>
 
+#include "libpspp/message.h"
 #include "libpspp/assertion.h"
 #include "libpspp/cast.h"
 #include "math/chart-geometry.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+#define N_(msgid) msgid
+
+
 #include "gl/xalloc.h"
 
 void
@@ -68,10 +74,14 @@ histogram_create (double bin_width, double min, double max)
      +1 otherwise.  */
   short sparse_end = 0;
 
-  assert (max >= min);
-
   if (max == min)
-    bin_width = 1;
+    {
+      msg (MW, _("Not creating histogram because the data contains less than 2 distinct values"));
+      free (h);
+      return NULL;
+    }
+
+  assert (max > min);
 
   lower_limit = floor (min / half_bin_width) - 1;
   upper_limit = floor (max / half_bin_width) + 1;
index 24005a0675b734abebc7e0e0bd03b9e373dc30fd..045506357f3fddbac92871f96b42d00f1289985a 100644 (file)
@@ -522,7 +522,7 @@ EXAMINE
         /PLOT = histogram
        .
 ])
-AT_CHECK([pspp -o pspp.csv examine.sps])
+AT_CHECK([pspp -o pspp.csv examine.sps], [0], [ignore])
 dnl Ignore output -- this is just a no-crash check.
 AT_CLEANUP
 
index 9eae09e8364e974c447f4be8505abd9190323788..1313f8509327159ed27bd26510c6dcfcc6c611c6 100644 (file)
@@ -152,18 +152,8 @@ end data.
 
 frequencies /x /histogram /STATISTICS=median.
 ])
-AT_CHECK([pspp -O format=csv frequencies.sps], [0], [dnl
-Table: x
-Value Label,Value,Frequency,Percent,Valid Percent,Cum Percent
-,1.00,1,100.00,100.00,100.00
-Total,,1,100.0,100.0,
-
-Table: x
-N,Valid,1
-,Missing,0
-S.E. Kurt,,.00
-,50 (Median),1.00
-])
+AT_CHECK([pspp -O format=csv frequencies.sps], [0], [ignore])
+dnl Ignore output - No crash test.
 AT_CLEANUP
 
 # Tests for a bug which caused FREQUENCIES following TEMPORARY to