Fixed bug where piecharts with many segments crashed.
authorJohn Darrington <john@darrington.wattle.id.au>
Thu, 2 Apr 2009 00:19:09 +0000 (08:19 +0800)
committerJohn Darrington <john@darrington.wattle.id.au>
Thu, 2 Apr 2009 00:19:09 +0000 (08:19 +0800)
Make sure that we don't index beyond the bounds of the
data_colour array.

src/output/charts/barchart.c
src/output/charts/piechart.c
src/output/charts/plot-chart.c
src/output/charts/plot-chart.h
tests/automake.mk
tests/bugs/piechart.sh [new file with mode: 0755]

index 7869d5d6bd4355f71f8e82ee917f43edd95edb19..88a1d741ddf9199f08404d49096de62dada0a373 100644 (file)
@@ -148,7 +148,7 @@ draw_barchart(struct chart *ch, const char *title,
        {
 
          pl_savestate_r(ch->lp);
-         pl_fillcolorname_r(ch->lp,data_colour[sc]);
+         pl_fillcolorname_r(ch->lp,data_colour[sc % N_CHART_COLOURS]);
 
          switch ( opt )
            {
index c4dc0e67a8b36a0d390e9247e8ec3fc5c886e74c..4eeb10ca63af06f70f764477cd0865401c34a8c0 100644 (file)
@@ -96,7 +96,7 @@ piechart_plot(const char *title, const struct slice *slices, int n_slices)
       draw_segment(ch,
                   centre_x, centre_y, radius,
                   angle, segment_angle,
-                  data_colour[i]);
+                  data_colour[i % N_CHART_COLOURS]);
 
       /* Now add the labels */
       if ( label_x < centre_x )
index 88aa58b06803a4c65a7f1f52a021acf116f111c9..3b4f1b377a12a624ceb929d00c617bb4bd581f30 100644 (file)
 
 #include "xalloc.h"
 
-const char *const data_colour[] = {
-  "brown",
-  "red",
-  "orange",
-  "yellow",
-  "green",
-  "blue",
-  "violet",
-  "grey",
-  "pink"
-};
+const char *const data_colour[N_CHART_COLOURS] =
+  {
+    "brown",
+    "red",
+    "orange",
+    "yellow",
+    "green",
+    "blue",
+    "violet",
+    "grey",
+    "pink"
+  };
 
 
 
index dae1cc07a1adcb30dd81f58b48eaec53d1874292..4a1dc10538d2567b630cca096886c8ca8e5011cd 100644 (file)
 #ifndef PLOT_CHART_H
 #define PLOT_CHART_H
 
-
+#define N_CHART_COLOURS 9
 extern const char *const data_colour[];
 
-enum tick_orientation {
-  TICK_ABSCISSA=0,
-  TICK_ORDINATE
-};
+enum tick_orientation
+  {
+    TICK_ABSCISSA=0,
+    TICK_ORDINATE
+  };
 
 
 /* Draw a tick mark at position
index bfca93d4b90218fc00bcb57ad65e17fd5e22b15d..b0039df456be5dffdbd5cae89c1d9a660650f700 100644 (file)
@@ -121,6 +121,7 @@ dist_TESTS = \
        tests/bugs/multipass.sh \
        tests/bugs/overwrite-input-file.sh \
        tests/bugs/overwrite-special-file.sh \
+       tests/bugs/piechart.sh \
        tests/bugs/random.sh \
        tests/bugs/signals.sh \
        tests/bugs/t-test-with-temp.sh \
diff --git a/tests/bugs/piechart.sh b/tests/bugs/piechart.sh
new file mode 100755 (executable)
index 0000000..657e538
--- /dev/null
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+# This program tests for a bug which crashed pspp when a
+# piechart with too many segments was requested.
+
+
+TEMPDIR=/tmp/pspp-tst-$$
+TESTFILE=$TEMPDIR/`basename $0`.sps
+
+# ensure that top_builddir  are absolute
+if [ -z "$top_builddir" ] ; then top_builddir=. ; fi
+if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi
+top_builddir=`cd $top_builddir; pwd`
+PSPP=$top_builddir/src/ui/terminal/pspp
+
+# ensure that top_srcdir is absolute
+top_srcdir=`cd $top_srcdir; pwd`
+
+STAT_CONFIG_PATH=$top_srcdir/config
+export STAT_CONFIG_PATH
+
+
+cleanup()
+{
+     if [ x"$PSPP_TEST_NO_CLEANUP" != x ] ; then 
+       echo "NOT cleaning $TEMPDIR" 
+       return ; 
+     fi
+     cd /
+     chmod u+w $TEMPDIR
+     rm -rf $TEMPDIR
+}
+
+
+fail()
+{
+    echo $activity
+    echo FAILED
+    cleanup;
+    exit 1;
+}
+
+
+no_result()
+{
+    echo $activity
+    echo NO RESULT;
+    cleanup;
+    exit 2;
+}
+
+pass()
+{
+    cleanup;
+    exit 0;
+}
+
+mkdir -p $TEMPDIR
+
+cd $TEMPDIR
+
+activity="create test syntax"
+cat > $TESTFILE <<EOF
+data list list /x * w *.
+begin data.
+1  4
+34 10
+-9 15
+232 6
+11  4
+134 1
+9  5
+32 16
+-2 6
+2  16
+20  6
+end data.
+
+weight by w.
+
+frequencies /x
+       /piechart.
+EOF
+if [ $? -ne 0 ] ; then no_result ; fi
+
+
+activity="run program 1"
+$SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE
+if [ $? -ne 0 ] ; then fail ; fi
+
+
+pass;