From c85ac38c0ac47d08b9fb7afbde5b5637d545eaf1 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 25 Mar 2012 09:14:39 +0200 Subject: [PATCH] Fix a crashes where histograms of a single datum were trying to be created. Reported-by: Ben Pfaff. --- src/language/stats/examine.c | 4 ++++ src/language/stats/frequencies.q | 11 +++++++++-- src/math/histogram.c | 16 +++++++++++++--- tests/language/stats/examine.at | 2 +- tests/language/stats/frequencies.at | 14 ++------------ 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/language/stats/examine.c b/src/language/stats/examine.c index dc53d806c0..45c64cbb55 100644 --- a/src/language/stats/examine.c +++ b/src/language/stats/examine.c @@ -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])); diff --git a/src/language/stats/frequencies.q b/src/language/stats/frequencies.q index d7754220fa..b9da4677d1 100644 --- a/src/language/stats/frequencies.q +++ b/src/language/stats/frequencies.q @@ -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]; diff --git a/src/math/histogram.c b/src/math/histogram.c index 9b89df6895..2f19f31095 100644 --- a/src/math/histogram.c +++ b/src/math/histogram.c @@ -21,10 +21,16 @@ #include #include +#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; diff --git a/tests/language/stats/examine.at b/tests/language/stats/examine.at index 24005a0675..045506357f 100644 --- a/tests/language/stats/examine.at +++ b/tests/language/stats/examine.at @@ -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 diff --git a/tests/language/stats/frequencies.at b/tests/language/stats/frequencies.at index 9eae09e836..1313f85093 100644 --- a/tests/language/stats/frequencies.at +++ b/tests/language/stats/frequencies.at @@ -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 -- 2.30.2