From 90a008db8b2f6e5bec903e584eb68e9bb20c061e Mon Sep 17 00:00:00 2001 From: John Darrington Date: Fri, 7 Oct 2011 14:15:35 +0200 Subject: [PATCH] Fix incorrectly ordered arguments to xcalloc The gnulib xcalloc call has the following signature: void *calloc(size_t N, size_t S); where N is the number of objects and S is the size of each object. In many places, we had these arguments transposed. In many implementations this doesn't matter since the two arguments are simply multiplied together. However, on some systems this can cause problems (ie crash), if S is zero. This change fixes all calls where the size was being passes as the first argument instead of the second. --- src/language/stats/ks-one-sample.c | 2 +- src/language/stats/mcnemar.c | 2 +- src/language/stats/sign.c | 2 +- src/language/stats/t-test-indep.c | 2 +- src/language/stats/t-test-parser.c | 3 +-- src/language/stats/wilcoxon.c | 2 +- src/math/covariance.c | 4 ++-- src/math/percentiles.c | 2 +- src/math/trimmed-mean.c | 2 +- src/math/tukey-hinges.c | 2 +- src/math/wilcoxon-sig.c | 2 +- src/ui/gui/psppire-scanf.c | 2 +- src/ui/gui/widget-io.c | 2 +- 13 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/language/stats/ks-one-sample.c b/src/language/stats/ks-one-sample.c index a4a2792f..aaab53fa 100644 --- a/src/language/stats/ks-one-sample.c +++ b/src/language/stats/ks-one-sample.c @@ -145,7 +145,7 @@ ks_one_sample_execute (const struct dataset *ds, int v; struct casereader *r = casereader_clone (input); - struct ks *ks = xcalloc (sizeof *ks, ost->n_vars); + struct ks *ks = xcalloc (ost->n_vars, sizeof *ks); for (v = 0; v < ost->n_vars; ++v) { diff --git a/src/language/stats/mcnemar.c b/src/language/stats/mcnemar.c index 53e9a52f..e26a3c1d 100644 --- a/src/language/stats/mcnemar.c +++ b/src/language/stats/mcnemar.c @@ -82,7 +82,7 @@ mcnemar_execute (const struct dataset *ds, struct casereader *r = input; - struct mcnemar *mc = xcalloc (sizeof *mc, t2s->n_pairs); + struct mcnemar *mc = xcalloc (t2s->n_pairs, sizeof *mc); for (i = 0 ; i < t2s->n_pairs; ++i ) { diff --git a/src/language/stats/sign.c b/src/language/stats/sign.c index 0048eb3e..e208ce9d 100644 --- a/src/language/stats/sign.c +++ b/src/language/stats/sign.c @@ -174,7 +174,7 @@ sign_execute (const struct dataset *ds, const struct two_sample_test *t2s = UP_CAST (test, const struct two_sample_test, parent); struct ccase *c; - struct sign_test_params *stp = xcalloc (sizeof *stp, t2s->n_pairs); + struct sign_test_params *stp = xcalloc (t2s->n_pairs, sizeof *stp); struct casereader *r = input; diff --git a/src/language/stats/t-test-indep.c b/src/language/stats/t-test-indep.c index bc42e327..c8050549 100644 --- a/src/language/stats/t-test-indep.c +++ b/src/language/stats/t-test-indep.c @@ -82,7 +82,7 @@ indep_run (struct tt *tt, const struct variable *gvar, struct ccase *c; struct casereader *r; - struct pair_stats *ps = xcalloc (sizeof (*ps), tt->n_vars); + struct pair_stats *ps = xcalloc (tt->n_vars, sizeof *ps); int v; diff --git a/src/language/stats/t-test-parser.c b/src/language/stats/t-test-parser.c index efb84b32..089d0dd8 100644 --- a/src/language/stats/t-test-parser.c +++ b/src/language/stats/t-test-parser.c @@ -178,8 +178,7 @@ cmd_t_test (struct lexer *lexer, struct dataset *ds) else n_pairs = n_v1 * n_v2; - pairs = xcalloc (sizeof *pairs, n_pairs); - + pairs = xcalloc (n_pairs, sizeof *pairs); if ( with) { diff --git a/src/language/stats/wilcoxon.c b/src/language/stats/wilcoxon.c index ed6225df..5b09f6e1 100644 --- a/src/language/stats/wilcoxon.c +++ b/src/language/stats/wilcoxon.c @@ -81,7 +81,7 @@ wilcoxon_execute (const struct dataset *ds, const struct dictionary *dict = dataset_dict (ds); const struct two_sample_test *t2s = UP_CAST (test, const struct two_sample_test, parent); - struct wilcoxon_state *ws = xcalloc (sizeof (*ws), t2s->n_pairs); + struct wilcoxon_state *ws = xcalloc (t2s->n_pairs, sizeof *ws); const struct variable *weight = dict_get_weight (dict); struct variable *weightx = dict_create_internal_var (WEIGHT_IDX, 0); struct caseproto *proto; diff --git a/src/math/covariance.c b/src/math/covariance.c index aa4a4b8a..be53672d 100644 --- a/src/math/covariance.c +++ b/src/math/covariance.c @@ -160,8 +160,8 @@ covariance_1pass_create (size_t n_vars, const struct variable *const *vars, cov->n_cm = (n_vars * (n_vars - 1) ) / 2; - if (cov->n_cm > 0) - cov->cm = xcalloc (sizeof *cov->cm, cov->n_cm); + + cov->cm = xcalloc (cov->n_cm, sizeof *cov->cm); cov->categoricals = NULL; return cov; diff --git a/src/math/percentiles.c b/src/math/percentiles.c index ed7b1295..2063dd2c 100644 --- a/src/math/percentiles.c +++ b/src/math/percentiles.c @@ -176,7 +176,7 @@ percentile_create (double p, double W) ptl->w = W; os->n_k = 2; - os->k = xcalloc (sizeof (*os->k), 2); + os->k = xcalloc (2, sizeof (*os->k)); os->k[0].tc = W * p; os->k[1].tc = (W + 1.0) * p; diff --git a/src/math/trimmed-mean.c b/src/math/trimmed-mean.c index 5e486896..b9851252 100644 --- a/src/math/trimmed-mean.c +++ b/src/math/trimmed-mean.c @@ -57,7 +57,7 @@ trimmed_mean_create (double W, double tail) struct statistic *stat = &os->parent; os->n_k = 2; - os->k = xcalloc (sizeof (*os->k), 2); + os->k = xcalloc (2, sizeof (*os->k)); assert (tail >= 0); assert (tail <= 1); diff --git a/src/math/tukey-hinges.c b/src/math/tukey-hinges.c index 3e9ea937..99a42cda 100644 --- a/src/math/tukey-hinges.c +++ b/src/math/tukey-hinges.c @@ -81,7 +81,7 @@ tukey_hinges_create (double W, double c_min) assert (c_min >= 0); os->n_k = 3; - os->k = xcalloc (sizeof (*os->k), 3); + os->k = xcalloc (3, sizeof (*os->k)); if ( c_min >= 1.0) { diff --git a/src/math/wilcoxon-sig.c b/src/math/wilcoxon-sig.c index d9a4bcae..a1b43dae 100644 --- a/src/math/wilcoxon-sig.c +++ b/src/math/wilcoxon-sig.c @@ -105,7 +105,7 @@ count_sums_to_W (unsigned long int n, unsigned long int w) else if (n == 1) return 1; - array = xcalloc (sizeof *array, w + 1); + array = xcalloc (w + 1, sizeof *array); array[w] = 1; max = w; diff --git a/src/ui/gui/psppire-scanf.c b/src/ui/gui/psppire-scanf.c index 2396245a..c2c8e55a 100644 --- a/src/ui/gui/psppire-scanf.c +++ b/src/ui/gui/psppire-scanf.c @@ -93,7 +93,7 @@ guts (PsppireScanf *scanf) g_return_if_fail (0 == printf_parse (scanf->format, &scanf->d, &a)); if ( scanf->d.count > 0) - scanf->widgets = xcalloc (sizeof (*scanf->widgets), scanf->d.count); + scanf->widgets = xcalloc (scanf->d.count, sizeof (*scanf->widgets)); /* A is not used, so get rid of it */ if (a.arg != a.direct_alloc_arg) diff --git a/src/ui/gui/widget-io.c b/src/ui/gui/widget-io.c index e04e3222..da9c9cb1 100644 --- a/src/ui/gui/widget-io.c +++ b/src/ui/gui/widget-io.c @@ -51,7 +51,7 @@ widget_printf (const gchar *fmt, ...) if ( 0 != printf_parse (fmt, &d, &a) ) return NULL; - widgets = xcalloc (sizeof (*widgets), d.count); + widgets = xcalloc (d.count, sizeof (*widgets)); va_start (ap, fmt); for (i = 0 ; i < d.count ; ++i ) { -- 2.30.2