From 2ba9563cb1fddad9430be0c415dc81456f006281 Mon Sep 17 00:00:00 2001
From: John Darrington <john@darrington.wattle.id.au>
Date: Sat, 2 Oct 2021 15:32:40 +0200
Subject: [PATCH] Use XCALLOC / XZALLOC macros where reasonable

---
 src/language/data-io/matrix-data.c   |  2 +-
 src/language/stats/chisquare.c       |  4 ++--
 src/language/stats/crosstabs.c       |  4 ++--
 src/language/stats/examine.c         |  6 ++----
 src/language/stats/frequencies.c     |  2 +-
 src/language/stats/graph.c           |  2 +-
 src/language/stats/means.c           | 12 +++++-------
 src/language/stats/median.c          |  2 +-
 src/language/stats/oneway.c          |  4 ++--
 src/language/stats/regression.c      |  5 ++---
 src/language/stats/reliability.c     | 15 ++++++---------
 src/libpspp/bit-vector.c             |  3 +--
 src/math/box-whisker.c               |  2 +-
 src/math/wilcoxon-sig.c              |  3 +--
 src/output/pivot-output.c            |  4 ++--
 src/output/pivot-table.c             |  8 ++++----
 src/output/render.c                  |  9 ++++-----
 src/ui/gui/psppire-import-textfile.c |  2 +-
 src/ui/gui/widget-io.c               |  3 +--
 19 files changed, 40 insertions(+), 52 deletions(-)

diff --git a/src/language/data-io/matrix-data.c b/src/language/data-io/matrix-data.c
index c0f5979bcd..bffdd92cb7 100644
--- a/src/language/data-io/matrix-data.c
+++ b/src/language/data-io/matrix-data.c
@@ -940,7 +940,7 @@ cmd_matrix_data (struct lexer *lexer, struct dataset *ds)
     .cells = -1,
   };
 
-  bool *taken_vars = xzalloc (n_input_vars);
+  bool *taken_vars = XCALLOC (n_input_vars, bool);
   if (input_rowtype)
     taken_vars[var_get_dict_index (rowtype)] = true;
 
diff --git a/src/language/stats/chisquare.c b/src/language/stats/chisquare.c
index eb21644d4a..d1d81bab39 100644
--- a/src/language/stats/chisquare.c
+++ b/src/language/stats/chisquare.c
@@ -141,8 +141,8 @@ chisquare_execute (const struct dataset *ds,
   struct one_sample_test *ost = &cst->parent;
   double total_expected = 0.0;
 
-  double *df = xzalloc (sizeof (*df) * ost->n_vars);
-  double *xsq = xzalloc (sizeof (*df) * ost->n_vars);
+  double *df = XCALLOC (ost->n_vars, double);
+  double *xsq = XCALLOC (ost->n_vars, double);
   bool ok;
 
   for (i = 0 ; i < cst->n_expected ; ++i)
diff --git a/src/language/stats/crosstabs.c b/src/language/stats/crosstabs.c
index 32e1a7736a..465bdcc278 100644
--- a/src/language/stats/crosstabs.c
+++ b/src/language/stats/crosstabs.c
@@ -606,7 +606,7 @@ parse_crosstabs_tables (struct lexer *lexer, struct dataset *ds,
 	}
     }
 
-  int *by_iter = xcalloc (n_by, sizeof *by_iter);
+  int *by_iter = XCALLOC (n_by, int);
   proc->pivots = xnrealloc (proc->pivots,
                             proc->n_pivots + nx, sizeof *proc->pivots);
   for (int i = 0; i < nx; i++)
@@ -912,7 +912,7 @@ postcalc (struct crosstabs_proc *proc)
       if (proc->barchart)
         {
           int n_vars = (xt->n_vars > 2 ? 2 : xt->n_vars);
-          const struct variable **vars = xcalloc (n_vars, sizeof *vars);
+          const struct variable **vars = XCALLOC (n_vars, const struct variable*);
           for (size_t i = 0; i < n_vars; i++)
             vars[i] = xt->vars[i].var;
           chart_submit (barchart_create (vars, n_vars, _("Count"),
diff --git a/src/language/stats/examine.c b/src/language/stats/examine.c
index 00fb801fce..1b668c2b77 100644
--- a/src/language/stats/examine.c
+++ b/src/language/stats/examine.c
@@ -1197,13 +1197,12 @@ calculate_n (const void *aux1, void *aux2 UNUSED, void *user_data)
 
       {
 	const int n_os = 5 + examine->n_percentiles;
-	struct order_stats **os ;
 	es[v].percentiles = pool_calloc (examine->pool, examine->n_percentiles, sizeof (*es[v].percentiles));
 
 	es[v].trimmed_mean = trimmed_mean_create (es[v].cc, 0.05);
 	es[v].shapiro_wilk = NULL;
 
-	os = xcalloc (n_os, sizeof *os);
+	struct order_stats **os = XCALLOC (n_os, struct order_stats *);
 	os[0] = &es[v].trimmed_mean->parent;
 
 	es[v].quartiles[0] = percentile_create (0.25, es[v].cc);
@@ -1806,8 +1805,7 @@ cmd_examine (struct lexer *lexer, struct dataset *ds)
   if (percentiles_seen && examine.n_percentiles == 0)
     {
       examine.n_percentiles = 7;
-      examine.ptiles = xcalloc (examine.n_percentiles,
-                                sizeof (*examine.ptiles));
+      examine.ptiles = xcalloc (examine.n_percentiles, sizeof (*examine.ptiles));
 
       examine.ptiles[0] = 5;
       examine.ptiles[1] = 10;
diff --git a/src/language/stats/frequencies.c b/src/language/stats/frequencies.c
index 484ce1af1e..e5462d083d 100644
--- a/src/language/stats/frequencies.c
+++ b/src/language/stats/frequencies.c
@@ -631,7 +631,7 @@ cmd_frequencies (struct lexer *lexer, struct dataset *ds)
 			      PV_NO_DUPLICATE))
     goto error;
 
-  frq.vars = xzalloc (frq.n_vars * sizeof (*frq.vars));
+  frq.vars = xcalloc (frq.n_vars, sizeof (*frq.vars));
   for (i = 0; i < frq.n_vars; ++i)
     {
       frq.vars[i].var = vars[i];
diff --git a/src/language/stats/graph.c b/src/language/stats/graph.c
index 425fd24157..2dd98e3db2 100644
--- a/src/language/stats/graph.c
+++ b/src/language/stats/graph.c
@@ -240,7 +240,7 @@ parse_function (struct lexer *lexer, struct graph *graph)
       if (!lex_force_match (lexer, T_LPAREN))
 	goto error;
 
-      graph->dep_vars = xzalloc (sizeof (graph->dep_vars) * graph->n_dep_vars);
+      graph->dep_vars = xcalloc (graph->n_dep_vars, sizeof (graph->dep_vars));
       for (v = 0; v < ag_func[i].arity; ++v)
 	{
 	  graph->dep_vars[v] = parse_variable (lexer, graph->dict);
diff --git a/src/language/stats/means.c b/src/language/stats/means.c
index 0a3b413df7..8ebbca0f35 100644
--- a/src/language/stats/means.c
+++ b/src/language/stats/means.c
@@ -970,10 +970,8 @@ prepare_means (struct means *cmd)
         {
           struct workspace *ws = mt->ws + i;
 	  ws->root_cell = NULL;
-          ws->control_idx = xzalloc (mt->n_layers
-					 * sizeof *ws->control_idx);
-          ws->instances = xzalloc (mt->n_layers
-					 * sizeof *ws->instances);
+          ws->control_idx = xcalloc (mt->n_layers, sizeof *ws->control_idx);
+          ws->instances = xcalloc (mt->n_layers, sizeof *ws->instances);
           int cmb = i;
           for (int l = mt->n_layers - 1; l >= 0; --l)
             {
@@ -1149,9 +1147,9 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
 	for (int t = 0; t < means.n_tables; ++t)
 	{
 	  struct mtable *mt = means.table + t;
-	  mt->summ = xzalloc (mt->n_combinations * mt->n_dep_vars
-			      * sizeof (*mt->summ));
-	  mt->ws = xzalloc (mt->n_combinations * sizeof (*mt->ws));
+	  mt->summ = xcalloc (mt->n_combinations * mt->n_dep_vars,
+			      sizeof (*mt->summ));
+	  mt->ws = xcalloc (mt->n_combinations, sizeof (*mt->ws));
 	}
       	run_means (&means, group, ds);
 	for (int t = 0; t < means.n_tables; ++t)
diff --git a/src/language/stats/median.c b/src/language/stats/median.c
index 8fa38c5a69..2e09ab02ce 100644
--- a/src/language/stats/median.c
+++ b/src/language/stats/median.c
@@ -253,7 +253,7 @@ median_execute (const struct dataset *ds,
 	  }
 
 	results[v].n = count;
-	results[v].sorted_array = xcalloc (hmap_count (&map), sizeof (void*));
+	results[v].sorted_array = XCALLOC (hmap_count (&map), void*);
 	results[v].var = var;
 
 	HMAP_FOR_EACH (vn, struct val_node, node, &map)
diff --git a/src/language/stats/oneway.c b/src/language/stats/oneway.c
index a612862556..e20d55198b 100644
--- a/src/language/stats/oneway.c
+++ b/src/language/stats/oneway.c
@@ -709,8 +709,8 @@ run_oneway (const struct oneway_spec *cmd,
   struct oneway_workspace ws;
 
   ws.actual_number_of_groups = 0;
-  ws.vws = xzalloc (cmd->n_vars * sizeof (*ws.vws));
-  ws.dd_total = xmalloc (sizeof (struct descriptive_data) * cmd->n_vars);
+  ws.vws = xcalloc (cmd->n_vars, sizeof (*ws.vws));
+  ws.dd_total = XCALLOC (cmd->n_vars, struct descriptive_data*);
 
   for (v = 0 ; v < cmd->n_vars; ++v)
     ws.dd_total[v] = dd_create (cmd->vars[v]);
diff --git a/src/language/stats/regression.c b/src/language/stats/regression.c
index 7c98dba047..e08f4a1d76 100644
--- a/src/language/stats/regression.c
+++ b/src/language/stats/regression.c
@@ -671,8 +671,7 @@ run_regression_get_models (const struct regression *cmd,
 			   bool output)
 {
   size_t i;
-  struct linreg **models = NULL;
-  struct model_container *model_container = xzalloc (sizeof (*model_container) * cmd->n_vars);
+  struct model_container *model_container = XCALLOC (cmd->n_vars, struct model_container);
 
   struct ccase *c;
   struct covariance *cov;
@@ -731,7 +730,7 @@ run_regression_get_models (const struct regression *cmd,
     casereader_destroy (r);
   }
 
-  models = xcalloc (cmd->n_dep_vars, sizeof (*models));
+  struct linreg **models = XCALLOC (cmd->n_dep_vars, struct linreg*);
 
   for (int k = 0; k < cmd->n_dep_vars; k++)
     {
diff --git a/src/language/stats/reliability.c b/src/language/stats/reliability.c
index 86c1add760..5816a2304c 100644
--- a/src/language/stats/reliability.c
+++ b/src/language/stats/reliability.c
@@ -170,13 +170,13 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
     /* Create a default Scale */
 
     reliability.n_sc = 1;
-    reliability.sc = xzalloc (sizeof (struct cronbach) * reliability.n_sc);
+    reliability.sc = xcalloc (reliability.n_sc, sizeof (struct cronbach));
 
     ds_assign_cstr (&reliability.scale_name, "ANY");
 
     c = &reliability.sc[0];
     c->n_items = reliability.n_variables;
-    c->items = xzalloc (sizeof (struct variable*) * c->n_items);
+    c->items = xcalloc (c->n_items, sizeof (struct variable*));
 
     for (i = 0 ; i < c->n_items ; ++i)
       c->items[i] = reliability.variables[i];
@@ -310,11 +310,8 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
 	(reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
 
       reliability.sc[2].n_items = s->n_items - reliability.sc[1].n_items;
-      reliability.sc[1].items = xzalloc (sizeof (struct variable *)
-				 * reliability.sc[1].n_items);
-
-      reliability.sc[2].items = xzalloc (sizeof (struct variable *) *
-				 reliability.sc[2].n_items);
+      reliability.sc[1].items = XCALLOC (reliability.sc[1].n_items, const struct variable *);
+      reliability.sc[2].items = XCALLOC (reliability.sc[2].n_items, const struct variable *);
 
       for  (i = 0; i < reliability.sc[1].n_items ; ++i)
 	reliability.sc[1].items[i] = s->items[i];
@@ -344,7 +341,7 @@ cmd_reliability (struct lexer *lexer, struct dataset *ds)
 	  struct cronbach *s = &reliability.sc[i + base_sc];
 
 	  s->n_items = reliability.sc[0].n_items - 1;
-	  s->items = xzalloc (sizeof (struct variable *) * s->n_items);
+	  s->items = xcalloc (s->n_items, sizeof (struct variable *));
 	  for (v_src = 0 ; v_src < reliability.sc[0].n_items ; ++v_src)
 	    {
 	      if (v_src != i)
@@ -391,7 +388,7 @@ run_reliability (struct dataset *ds, const struct reliability *reliability)
       struct cronbach *s = &reliability->sc[si];
       int i;
 
-      s->m = xzalloc (sizeof *s->m * s->n_items);
+      s->m = xcalloc (s->n_items, sizeof *s->m);
       s->total = moments1_create (MOMENT_VARIANCE);
 
       for (i = 0 ; i < s->n_items ; ++i)
diff --git a/src/libpspp/bit-vector.c b/src/libpspp/bit-vector.c
index 79367248f0..5198c89c21 100644
--- a/src/libpspp/bit-vector.c
+++ b/src/libpspp/bit-vector.c
@@ -25,8 +25,7 @@
 unsigned long int *
 bitvector_allocate(size_t n)
 {
-  return xcalloc (DIV_RND_UP (n, BITS_PER_ULONG),
-                  sizeof (unsigned long int));
+  return XCALLOC (DIV_RND_UP (n, BITS_PER_ULONG), unsigned long int);
 }
 
 size_t
diff --git a/src/math/box-whisker.c b/src/math/box-whisker.c
index 4bbe0f5850..506251682c 100644
--- a/src/math/box-whisker.c
+++ b/src/math/box-whisker.c
@@ -85,7 +85,7 @@ acc (struct statistic *s, const struct ccase *cx,
 
   /* y is an outlier */
 
-  struct outlier *o = xzalloc (sizeof *o) ;
+  struct outlier *o = XZALLOC (struct outlier);
   o->value = y;
   o->extreme = extreme;
   ds_init_empty (&o->label);
diff --git a/src/math/wilcoxon-sig.c b/src/math/wilcoxon-sig.c
index 17484f1411..a9d9b70dbc 100644
--- a/src/math/wilcoxon-sig.c
+++ b/src/math/wilcoxon-sig.c
@@ -93,7 +93,6 @@ count_sums_to_W (unsigned long int n, long int w)
      and using int will save some memory on 64-bit systems. */
   unsigned long int total;
   unsigned long int max;
-  int *array;
 
   assert (w >= 0);
   assert (n < CHAR_BIT * sizeof (unsigned long int));
@@ -106,7 +105,7 @@ count_sums_to_W (unsigned long int n, long int w)
   else if (n == 1)
     return 1;
 
-  array = xcalloc (w + 1, sizeof *array);
+  int *array = XCALLOC (w + 1, int);
   array[w] = 1;
 
   max = w;
diff --git a/src/output/pivot-output.c b/src/output/pivot-output.c
index a170b6ee71..c3751d04d8 100644
--- a/src/output/pivot-output.c
+++ b/src/output/pivot-output.c
@@ -233,7 +233,7 @@ compose_headings (struct table *t,
      |aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3|
      +-----+-----+-----+-----+-----+-----+-----+-----+-----+
   */
-  bool *vrules = xzalloc (n_columns + 1);
+  bool *vrules = XCALLOC (n_columns + 1, bool);
   vrules[0] = vrules[n_columns] = true;
   for (int dim_index = h_axis->n_dimensions; --dim_index >= 0; )
     {
@@ -426,7 +426,7 @@ collect_footnotes (const struct pivot_table *pt,
       return NULL;
     }
 
-  bool *refs = xzalloc (pt->n_footnotes);
+  bool *refs = XCALLOC (pt->n_footnotes, bool);
   size_t n_refs = 0;
   add_references (pt, title, refs, &n_refs);
   add_references (pt, layers, refs, &n_refs);
diff --git a/src/output/pivot-table.c b/src/output/pivot-table.c
index 6bb446ed8e..f1e0e73372 100644
--- a/src/output/pivot-table.c
+++ b/src/output/pivot-table.c
@@ -968,7 +968,7 @@ clone_category (struct pivot_category *old,
     .extra_depth = old->extra_depth,
 
     .subs = (old->n_subs
-             ? xzalloc (old->n_subs * sizeof *new->subs)
+             ? xcalloc (old->n_subs, sizeof *new->subs)
              : NULL),
     .n_subs = old->n_subs,
     .allocated_subs = old->n_subs,
@@ -1006,9 +1006,9 @@ clone_dimension (struct pivot_dimension *old, struct pivot_table *new_pt)
     .axis_type = old->axis_type,
     .level = old->level,
     .top_index = old->top_index,
-    .data_leaves = xzalloc (old->n_leaves * sizeof *new->data_leaves),
-    .presentation_leaves = xzalloc (old->n_leaves
-                                    * sizeof *new->presentation_leaves),
+    .data_leaves = xcalloc (old->n_leaves , sizeof *new->data_leaves),
+    .presentation_leaves = xcalloc (old->n_leaves
+                                    , sizeof *new->presentation_leaves),
     .n_leaves = old->n_leaves,
     .allocated_leaves = old->n_leaves,
     .hide_all_labels = old->hide_all_labels,
diff --git a/src/output/render.c b/src/output/render.c
index b88f10badb..7aa80a6977 100644
--- a/src/output/render.c
+++ b/src/output/render.c
@@ -539,9 +539,8 @@ render_page_allocate__ (const struct render_params *params,
 
   for (int i = 0; i < TABLE_N_AXES; i++)
     {
-      page->cp[i] = xmalloc ((2 * n[i] + 2) * sizeof *page->cp[i]);
-      page->join_crossing[i] = xzalloc ((n[i] + 1)
-                                        * sizeof *page->join_crossing[i]);
+      page->cp[i] = xcalloc ((2 * n[i] + 2) , sizeof *page->cp[i]);
+      page->join_crossing[i] = xcalloc ((n[i] + 1) , sizeof *page->join_crossing[i]);
     }
 
   hmap_init (&page->overflows);
@@ -732,7 +731,7 @@ render_page_create (const struct render_params *params, struct table *table,
      span multiple columns. */
   struct render_row *columns[2];
   for (int i = 0; i < 2; i++)
-    columns[i] = xzalloc (nc * sizeof *columns[i]);
+    columns[i] = xcalloc (nc , sizeof *columns[i]);
   for (int y = 0; y < nr; y++)
     for (int x = 0; x < nc;)
       {
@@ -816,7 +815,7 @@ render_page_create (const struct render_params *params, struct table *table,
     }
 
   /* Calculate heights of cells that do not span multiple rows. */
-  struct render_row *rows = xzalloc (nr * sizeof *rows);
+  struct render_row *rows = XCALLOC (nr, struct render_row);
   for (int y = 0; y < nr; y++)
     for (int x = 0; x < nc;)
       {
diff --git a/src/ui/gui/psppire-import-textfile.c b/src/ui/gui/psppire-import-textfile.c
index eedc21a082..d9dcccef28 100644
--- a/src/ui/gui/psppire-import-textfile.c
+++ b/src/ui/gui/psppire-import-textfile.c
@@ -88,7 +88,7 @@ choose_likely_separators (PsppireImportAssistant *ia)
       gchar *line_text = NULL;
       gtk_tree_model_get (GTK_TREE_MODEL (ia->text_file), &iter, 1, &line_text, -1);
 
-      gint *counts = xzalloc (sizeof *counts * SEPARATOR_CNT);
+      gint *counts = XCALLOC (SEPARATOR_CNT, gint);
 
       struct substring cs = ss_cstr (line_text);
       for (;
diff --git a/src/ui/gui/widget-io.c b/src/ui/gui/widget-io.c
index 9612e44e7f..589eac36eb 100644
--- a/src/ui/gui/widget-io.c
+++ b/src/ui/gui/widget-io.c
@@ -43,7 +43,6 @@ widget_printf (const gchar *fmt, ...)
   char_directives d;
   arguments a;
   GString *output;
-  GtkWidget **widgets;
   gchar *text;
   va_list ap;
   const char *s = fmt;
@@ -51,7 +50,7 @@ widget_printf (const gchar *fmt, ...)
   if (0 !=  printf_parse (fmt, &d, &a))
     return NULL;
 
-  widgets = xcalloc (d.count, sizeof (*widgets));
+  GtkWidget **widgets = XCALLOC (d.count, GtkWidget*);
   va_start (ap, fmt);
   for (i = 0 ; i < d.count ; ++i)
     {
-- 
2.30.2