From 8cf495e615e4feca5777f3592de98321d4fcdc0b Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 26 Oct 2005 05:06:14 +0000 Subject: [PATCH] Fix up potential overflows in size calculations by replacing instances of xmalloc(x * sizeof *y) by xnmalloc(x, sizeof *y) everywhere I could find them. Similarly by xrealloc(), malloc(). (Order is important: xnmalloc(sizeof *y, x) will divide by 0 if x is 0.) --- src/ChangeLog | 18 ++++++++++++++++++ src/aggregate.c | 2 +- src/alloc.c | 14 +++++++------- src/alloc.h | 5 +++-- src/ascii.c | 10 +++++----- src/autorecode.c | 4 ++-- src/case.c | 2 +- src/casefile.c | 12 ++++++------ src/cat.c | 16 ++++++++-------- src/count.c | 16 ++++++++-------- src/crosstabs.q | 36 ++++++++++++++++++------------------ src/data-list.c | 2 +- src/descript.c | 6 +++--- src/dictionary.c | 22 +++++++++++----------- src/error.c | 2 +- src/examine.q | 8 ++++---- src/factor_stats.c | 10 +++++----- src/file-type.c | 2 +- src/flip.c | 6 +++--- src/frequencies.q | 4 ++-- src/get.c | 6 +++--- src/groff-font.c | 8 ++++---- src/hash.c | 6 +++--- src/inpt-pgm.c | 2 +- src/levene.c | 4 ++-- src/list.q | 11 +++++------ src/matrix-data.c | 12 ++++++------ src/means.q | 4 ++-- src/modify-vars.c | 14 +++++++------- src/moments.c | 4 ++-- src/oneway.q | 6 ++---- src/output.c | 2 +- src/pfm-write.c | 2 +- src/plot-chart.c | 2 +- src/postscript.c | 6 +++--- src/q2c.c | 10 +++++----- src/rank.q | 4 ++-- src/recode.c | 2 +- src/regression.q | 4 ++-- src/repeat.c | 14 +++++++------- src/set.q | 2 +- src/sfm-read.c | 16 ++++++++-------- src/sfm-write.c | 4 ++-- src/sort-prs.c | 4 ++-- src/sort.c | 25 +++++++++++++------------ src/subclist.c | 5 +++-- src/sysfile-info.c | 2 +- src/t-test.q | 9 ++++----- src/vars-prs.c | 8 ++++---- src/vfm.c | 4 ++-- 50 files changed, 208 insertions(+), 191 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index d81b5b6a..7e5f4057 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,21 @@ +Tue Oct 25 21:56:23 2005 Ben Pfaff + + Fix up potential overflows in size calculations by replacing + instances of xmalloc(x * sizeof *y) by xnmalloc(x, sizeof *y) + everywhere I could find them. Similarly by xrealloc(), malloc(). + (Order is important: xnmalloc(sizeof *y, x) will divide by 0 if x + is 0.) + + * alloc.c: (nmalloc) New function. + (out_of_memory) Removed. Replaced references by xalloc_die(). + + * sort.c: (allocate_cases) Fix segfault if memory allocation + fails. + + * subclist.c: (subc_list_double_create) Use xnmalloc() instead of + malloc(). + (subc_list_double_push) Use xnrealloc() instead of realloc(). + Wed Oct 26 08:43:51 WST 2005 John Darrington Dictionary abstraction part #2 diff --git a/src/aggregate.c b/src/aggregate.c index 91d7d9bd..86f35287 100644 --- a/src/aggregate.c +++ b/src/aggregate.c @@ -371,7 +371,7 @@ parse_aggregate_functions (struct agr_proc *agr) { int j; - dest_label = xrealloc (dest_label, sizeof *dest_label * n_dest); + dest_label = xnrealloc (dest_label, n_dest, sizeof *dest_label); for (j = n_dest_prev; j < n_dest; j++) dest_label[j] = NULL; } diff --git a/src/alloc.c b/src/alloc.c index 015d3397..a7b20283 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -19,14 +19,14 @@ #include #include "alloc.h" -#include #include -#include "error.h" -#include "str.h" -/* Report an out-of-memory condition and abort execution. */ -void -out_of_memory (void) +/* Allocates and returns N elements of S bytes each. + N must be nonnegative, S must be positive. + Returns a null pointer if the memory cannot be obtained, + including the case where N * S overflows the range of size_t. */ +void * +nmalloc (size_t n, size_t s) { - xalloc_die (); + return !xalloc_oversized (n, s) ? malloc (n * s) : NULL; } diff --git a/src/alloc.h b/src/alloc.h index c1148413..0f4492e3 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -24,8 +24,9 @@ /* malloc() wrapper functions. */ #include "xalloc.h" -void out_of_memory (void) NO_RETURN; - + +void *nmalloc (size_t n, size_t s); + /* alloca() wrapper functions. */ #if defined (HAVE_ALLOCA) || defined (C_ALLOCA) #ifdef HAVE_ALLOCA_H diff --git a/src/ascii.c b/src/ascii.c index ae3c805b..bd55d0ef 100644 --- a/src/ascii.c +++ b/src/ascii.c @@ -195,7 +195,7 @@ static struct outp_option_info *option_info; static int ascii_open_global (struct outp_class *this UNUSED) { - option_info = xmalloc ( sizeof (struct outp_option_info ) ) ; + option_info = xmalloc (sizeof *option_info); option_info->initial = 0; option_info->options = 0; return 1; @@ -231,7 +231,7 @@ ascii_preopen_driver (struct outp_driver *this) assert (this->driver_open == 0); msg (VM (1), _("ASCII driver initializing as `%s'..."), this->name); - this->ext = x = xmalloc (sizeof (struct ascii_driver_ext)); + this->ext = x = xmalloc (sizeof *x); x->char_set = CHS_ASCII; x->headers = 1; x->page_length = 66; @@ -716,7 +716,7 @@ ascii_open_page (struct outp_driver *this) if (x->l > x->lines_cap) { - x->lines = xrealloc (x->lines, sizeof *x->lines * x->l); + x->lines = xnrealloc (x->lines, x->l, sizeof *x->lines); for (i = x->lines_cap; i < x->l; i++) { struct line *line = &x->lines[i]; @@ -746,8 +746,8 @@ expand_line (struct ascii_driver_ext *x, int i, int l) if (l > line->char_cap) { line->char_cap = l * 2; - line->chars = xrealloc (line->chars, - line->char_cap * sizeof *line->chars); + line->chars = xnrealloc (line->chars, + line->char_cap, sizeof *line->chars); } for (j = line->char_cnt; j < l; j++) line->chars[j] = ' '; diff --git a/src/autorecode.c b/src/autorecode.c index 619d894e..259d0956 100644 --- a/src/autorecode.c +++ b/src/autorecode.c @@ -164,8 +164,8 @@ cmd_autorecode (void) } arc.src_values_pool = pool_create (); - arc.dst_vars = xmalloc (sizeof *arc.dst_vars * arc.var_cnt); - arc.src_values = xmalloc (sizeof *arc.src_values * arc.var_cnt); + arc.dst_vars = xnmalloc (arc.var_cnt, sizeof *arc.dst_vars); + arc.src_values = xnmalloc (arc.var_cnt, sizeof *arc.src_values); for (i = 0; i < dst_cnt; i++) if (arc.src_vars[i]->type == ALPHA) arc.src_values[i] = hsh_create (10, compare_alpha_value, diff --git a/src/case.c b/src/case.c index fee69d4d..1384791c 100644 --- a/src/case.c +++ b/src/case.c @@ -92,7 +92,7 @@ void case_create (struct ccase *c, size_t value_cnt) { if (!case_try_create (c, value_cnt)) - out_of_memory (); + xalloc_die (); } #ifdef GLOBAL_DEBUGGING diff --git a/src/casefile.c b/src/casefile.c index 9c3da660..aea4a147 100644 --- a/src/casefile.c +++ b/src/casefile.c @@ -344,12 +344,12 @@ casefile_append (struct casefile *cf, const struct ccase *c) if ((block_idx & (block_idx - 1)) == 0) { size_t block_cap = block_idx == 0 ? 1 : block_idx * 2; - cf->cases = xrealloc (cf->cases, - sizeof *cf->cases * block_cap); + cf->cases = xnrealloc (cf->cases, + block_cap, sizeof *cf->cases); } - cf->cases[block_idx] = xmalloc (sizeof **cf->cases - * CASES_PER_BLOCK); + cf->cases[block_idx] = xnmalloc (CASES_PER_BLOCK, + sizeof **cf->cases); } case_move (&cf->cases[block_idx][case_idx], &new_case); @@ -424,7 +424,7 @@ casefile_to_disk (const struct casefile *cf_) cf->storage = DISK; if (!make_temp_file (&cf->fd, &cf->filename)) err_failure (); - cf->buffer = xmalloc (cf->buffer_size * sizeof *cf->buffer); + cf->buffer = xnmalloc (cf->buffer_size, sizeof *cf->buffer); memset (cf->buffer, 0, cf->buffer_size * sizeof *cf->buffer); case_bytes -= cf->case_cnt * cf->case_acct_size; @@ -549,7 +549,7 @@ reader_open_file (struct casereader *reader) } else { - reader->buffer = xmalloc (cf->buffer_size * sizeof *cf->buffer); + reader->buffer = xnmalloc (cf->buffer_size, sizeof *cf->buffer); memset (reader->buffer, 0, cf->buffer_size * sizeof *cf->buffer); } diff --git a/src/cat.c b/src/cat.c index afdb44ef..09cdebde 100644 --- a/src/cat.c +++ b/src/cat.c @@ -65,7 +65,7 @@ cr_recoded_categorical_create (const struct variable *v) rc->v = v; rc->n_categories = 0; rc->n_allocated_categories = N_INITIAL_CATEGORIES; - rc->vals = xmalloc (N_INITIAL_CATEGORIES * sizeof (*rc->vals)); + rc->vals = xnmalloc (N_INITIAL_CATEGORIES, sizeof *rc->vals); return rc; } @@ -85,7 +85,7 @@ cr_recoded_cat_ar_create (int n_variables, struct variable *v_variables[]) struct recoded_categorical_array *ca; struct variable *v; - ca = (struct recoded_categorical_array *) xmalloc (sizeof (*ca)); + ca = xmalloc (sizeof *ca); for (i = 0; i < n_variables; i++) { v = v_variables[i]; @@ -95,7 +95,7 @@ cr_recoded_cat_ar_create (int n_variables, struct variable *v_variables[]) } } ca->n_vars = n_categoricals; - ca->a = xmalloc (n_categoricals * sizeof (*(ca->a))); + ca->a = xnmalloc (n_categoricals, sizeof *ca->a); for (i = 0; i < n_categoricals; i++) { *(ca->a + i) = cr_recoded_categorical_create (v_variables[i]); @@ -145,8 +145,8 @@ cr_value_update (struct recoded_categorical *rc, const union value *v) if (rc->n_categories >= rc->n_allocated_categories) { rc->n_allocated_categories *= 2; - rc->vals = xrealloc (rc->vals, rc->n_allocated_categories - * sizeof (*(rc->vals))); + rc->vals = xnrealloc (rc->vals, + rc->n_allocated_categories, sizeof *rc->vals); } rc->vals[rc->n_categories] = *v; rc->n_categories++; @@ -319,8 +319,8 @@ design_matrix_create (int n_variables, size_t n_cols = 0; size_t col; - dm = xmalloc (sizeof (*dm)); - dm->vars = xmalloc (n_variables * sizeof (struct variable *)); + dm = xmalloc (sizeof *dm); + dm->vars = xnmalloc (n_variables, sizeof *dm->vars); dm->n_vars = n_variables; for (i = 0; i < n_variables; i++) @@ -341,7 +341,7 @@ design_matrix_create (int n_variables, } } dm->m = gsl_matrix_calloc (n_data, n_cols); - dm->vars = xmalloc (dm->n_vars * sizeof (*(dm->vars))); + dm->vars = xnmalloc (dm->n_vars, sizeof *dm->vars); assert (dm->vars != NULL); col = 0; diff --git a/src/count.c b/src/count.c index 8988f3cb..876c0793 100644 --- a/src/count.c +++ b/src/count.c @@ -230,7 +230,7 @@ fail: /* Parses a set of numeric criterion values. */ static int -parse_numeric_criteria (struct counting * c) +parse_numeric_criteria (struct counting *c) { size_t n = 0; size_t m = 0; @@ -243,7 +243,7 @@ parse_numeric_criteria (struct counting * c) if (n + 1 >= m) { m += 16; - c->crit.n = xrealloc (c->crit.n, m * sizeof (struct cnt_num)); + c->crit.n = xnrealloc (c->crit.n, m, sizeof *c->crit.n); } cur = &c->crit.n[n++]; @@ -324,7 +324,7 @@ parse_numeric_criteria (struct counting * c) /* Parses a set of string criteria values. The skeleton is the same as parse_numeric_criteria(). */ static int -parse_string_criteria (struct counting * c) +parse_string_criteria (struct counting *c) { int len = 0; @@ -344,7 +344,7 @@ parse_string_criteria (struct counting * c) if (n + 1 >= m) { m += 16; - c->crit.n = xrealloc (c->crit.n, m * sizeof (struct cnt_str)); + c->crit.s = xnrealloc (c->crit.s, m, sizeof *c->crit.s); } if (!lex_force_string ()) @@ -368,7 +368,7 @@ parse_string_criteria (struct counting * c) /* Counts the number of values in case C matching counting CNT. */ static inline int -count_numeric (struct counting * cnt, struct ccase * c) +count_numeric (struct counting *cnt, struct ccase *c) { int counter = 0; size_t i; @@ -433,7 +433,7 @@ count_numeric (struct counting * cnt, struct ccase * c) /* Counts the number of values in case C matching counting CNT. */ static inline int -count_string (struct counting * cnt, struct ccase * c) +count_string (struct counting *cnt, struct ccase *c) { int counter = 0; size_t i; @@ -466,7 +466,7 @@ count_string (struct counting * cnt, struct ccase * c) /* Performs the COUNT transformation T on case C. */ static int -count_trns_proc (struct trns_header * trns, struct ccase * c, +count_trns_proc (struct trns_header *trns, struct ccase *c, int case_num UNUSED) { struct cnt_var_info *info; @@ -488,7 +488,7 @@ count_trns_proc (struct trns_header * trns, struct ccase * c, /* Destroys all dynamic data structures associated with T. */ static void -count_trns_free (struct trns_header * t) +count_trns_free (struct trns_header *t) { struct cnt_var_info *iter, *next; diff --git a/src/crosstabs.q b/src/crosstabs.q index aefac603..7d9f334f 100644 --- a/src/crosstabs.q +++ b/src/crosstabs.q @@ -321,8 +321,8 @@ crs_custom_tables (struct cmd_crosstabs *cmd UNUSED) for (n_by = 0; ;) { - by = xrealloc (by, sizeof *by * (n_by + 1)); - by_nvar = xrealloc (by_nvar, sizeof *by_nvar * (n_by + 1)); + by = xnrealloc (by, n_by + 1, sizeof *by); + by_nvar = xnrealloc (by_nvar, n_by + 1, sizeof *by_nvar); if (!parse_var_set_vars (var_set, &by[n_by], &by_nvar[n_by], PV_NO_DUPLICATE | PV_NO_SCRATCH)) goto done; @@ -350,7 +350,7 @@ crs_custom_tables (struct cmd_crosstabs *cmd UNUSED) int *by_iter = xcalloc (n_by, sizeof *by_iter); int i; - xtab = xnrealloc (xtab, sizeof *xtab, nxtab + nx); + xtab = xnrealloc (xtab, nxtab + nx, sizeof *xtab); for (i = 0; i < nx; i++) { struct crosstab *x; @@ -509,8 +509,8 @@ precalc (void *aux UNUSED) for (j = 2; j < x->nvar; j++) count *= get_var_range (x->vars[j - 2])->count; - sorted_tab = xrealloc (sorted_tab, - sizeof *sorted_tab * (n_sorted_tab + count)); + sorted_tab = xnrealloc (sorted_tab, + n_sorted_tab + count, sizeof *sorted_tab); v = local_alloc (sizeof *v * x->nvar); for (j = 2; j < x->nvar; j++) v[j] = get_var_range (x->vars[j])->min; @@ -529,7 +529,7 @@ precalc (void *aux UNUSED) const int mat_size = row_cnt * col_cnt; int m; - te->u.data = xmalloc (sizeof *te->u.data * mat_size); + te->u.data = xnmalloc (mat_size, sizeof *te->u.data); for (m = 0; m < mat_size; m++) te->u.data[m] = 0.; } @@ -548,8 +548,8 @@ precalc (void *aux UNUSED) local_free (v); } - sorted_tab = xrealloc (sorted_tab, - sizeof *sorted_tab * (n_sorted_tab + 1)); + sorted_tab = xnrealloc (sorted_tab, + n_sorted_tab + 1, sizeof *sorted_tab); sorted_tab[n_sorted_tab] = NULL; } } @@ -1197,13 +1197,13 @@ output_pivot_table (struct table_entry **pb, struct table_entry **pe, /* Allocate memory space for the column and row totals. */ if (n_rows > *maxrows) { - *row_totp = xrealloc (*row_totp, sizeof **row_totp * n_rows); + *row_totp = xnrealloc (*row_totp, n_rows, sizeof **row_totp); row_tot = *row_totp; *maxrows = n_rows; } if (n_cols > *maxcols) { - *col_totp = xrealloc (*col_totp, sizeof **col_totp * n_cols); + *col_totp = xnrealloc (*col_totp, n_cols, sizeof **col_totp); col_tot = *col_totp; *maxcols = n_cols; } @@ -1219,7 +1219,7 @@ output_pivot_table (struct table_entry **pb, struct table_entry **pe, /* Allocate memory space for the matrix. */ if (n_cols * n_rows > *maxcells) { - *matp = xrealloc (*matp, sizeof **matp * n_cols * n_rows); + *matp = xnrealloc (*matp, n_cols * n_rows, sizeof **matp); *maxcells = n_cols * n_rows; } @@ -1625,7 +1625,7 @@ enum_var_values (struct table_entry **entries, int entry_cnt, int var_idx, int width = v->width; int i; - *values = xmalloc (sizeof **values * entry_cnt); + *values = xnmalloc (entry_cnt, sizeof **values); for (i = 0; i < entry_cnt; i++) (*values)[i] = entries[i]->values[var_idx]; *value_cnt = sort_unique (*values, entry_cnt, sizeof **values, @@ -1637,7 +1637,7 @@ enum_var_values (struct table_entry **entries, int entry_cnt, int var_idx, int i; assert (mode == INTEGER); - *values = xmalloc (sizeof **values * vr->count); + *values = xnmalloc (vr->count, sizeof **values); for (i = 0; i < vr->count; i++) (*values)[i].f = i + vr->min; *value_cnt = vr->count; @@ -2520,7 +2520,7 @@ calc_symmetric (double v[N_SYMMETRIC], double ase[N_SYMMETRIC], { int r, c; - cum = xmalloc (sizeof *cum * n_cols * n_rows); + cum = xnmalloc (n_cols * n_rows, sizeof *cum); for (c = 0; c < n_cols; c++) { double ct = 0.; @@ -2863,10 +2863,10 @@ calc_directional (double v[N_DIRECTIONAL], double ase[N_DIRECTIONAL], /* Lambda. */ if (cmd.a_statistics[CRS_ST_LAMBDA]) { - double *fim = xmalloc (sizeof *fim * n_rows); - int *fim_index = xmalloc (sizeof *fim_index * n_rows); - double *fmj = xmalloc (sizeof *fmj * n_cols); - int *fmj_index = xmalloc (sizeof *fmj_index * n_cols); + double *fim = xnmalloc (n_rows, sizeof *fim); + int *fim_index = xnmalloc (n_rows, sizeof *fim_index); + double *fmj = xnmalloc (n_cols, sizeof *fmj); + int *fmj_index = xnmalloc (n_cols, sizeof *fmj_index); double sum_fim, sum_fmj; double rm, cm; int rm_index, cm_index; diff --git a/src/data-list.c b/src/data-list.c index ad8bcc7e..d1dbbc24 100644 --- a/src/data-list.c +++ b/src/data-list.c @@ -1557,7 +1557,7 @@ cmd_repeating_data (void) goto error; find_variable_input_spec (rpd->id_var, &rpd->id_spec); - rpd->id_value = xmalloc (sizeof *rpd->id_value * rpd->id_var->nv); + rpd->id_value = xnmalloc (rpd->id_var->nv, sizeof *rpd->id_value); } else if (lex_match_id ("TABLE")) table = 1; diff --git a/src/descript.c b/src/descript.c index 1e52abf6..0a96b65e 100644 --- a/src/descript.c +++ b/src/descript.c @@ -311,7 +311,7 @@ cmd_descriptives (void) PV_APPEND | PV_NO_DUPLICATE | PV_NUMERIC)) goto error; - dsc->vars = xrealloc (dsc->vars, sizeof *dsc->vars * var_cnt); + dsc->vars = xnrealloc (dsc->vars, var_cnt, sizeof *dsc->vars); for (i = dsc->var_cnt; i < var_cnt; i++) { struct dsc_var *dv = &dsc->vars[i]; @@ -634,14 +634,14 @@ setup_z_trns (struct dsc_proc *dsc) t = xmalloc (sizeof *t); t->h.proc = descriptives_trns_proc; t->h.free = descriptives_trns_free; - t->z_scores = xmalloc (cnt * sizeof *t->z_scores); + t->z_scores = xnmalloc (cnt, sizeof *t->z_scores); t->z_score_cnt = cnt; t->missing_type = dsc->missing_type; t->include_user_missing = dsc->include_user_missing; if ( t->missing_type == DSC_LISTWISE ) { t->var_cnt = dsc->var_cnt; - t->vars = xmalloc(t->var_cnt * sizeof *t->vars); + t->vars = xnmalloc (t->var_cnt, sizeof *t->vars); for (i = 0; i < t->var_cnt; i++) t->vars[i] = dsc->vars[i].v; } diff --git a/src/dictionary.c b/src/dictionary.c index 0bfa02cd..c3ed8ac0 100644 --- a/src/dictionary.c +++ b/src/dictionary.c @@ -100,7 +100,7 @@ dict_clone (const struct dictionary *s) d->split_cnt = s->split_cnt; if (d->split_cnt > 0) { - d->split = xmalloc (d->split_cnt * sizeof *d->split); + d->split = xnmalloc (d->split_cnt, sizeof *d->split); for (i = 0; i < d->split_cnt; i++) d->split[i] = dict_lookup_var_assert (d, s->split[i]->name); } @@ -116,7 +116,7 @@ dict_clone (const struct dictionary *s) dict_set_documents (d, dict_get_documents (s)); d->vector_cnt = s->vector_cnt; - d->vector = xmalloc (d->vector_cnt * sizeof *d->vector); + d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector); for (i = 0; i < s->vector_cnt; i++) { struct vector *sv = s->vector[i]; @@ -126,7 +126,7 @@ dict_clone (const struct dictionary *s) dv->idx = i; strcpy (dv->name, sv->name); dv->cnt = sv->cnt; - dv->var = xmalloc (dv->cnt * sizeof *dv->var); + dv->var = xnmalloc (dv->cnt, sizeof *dv->var); for (j = 0; j < dv->cnt; j++) dv->var[j] = d->var[sv->var[j]->index]; } @@ -240,7 +240,7 @@ dict_get_vars (const struct dictionary *d, struct variable ***vars, if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name)))) count++; - *vars = xmalloc (count * sizeof **vars); + *vars = xnmalloc (count, sizeof **vars); *cnt = 0; for (i = 0; i < d->var_cnt; i++) if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name)))) @@ -304,7 +304,7 @@ dict_create_var (struct dictionary *d, const char *name, int width) if (d->var_cnt >= d->var_cap) { d->var_cap = 8 + 2 * d->var_cap; - d->var = xrealloc (d->var, d->var_cap * sizeof *d->var); + d->var = xnrealloc (d->var, d->var_cap, sizeof *d->var); } d->var[v->index] = v; d->var_cnt++; @@ -545,7 +545,7 @@ dict_reorder_vars (struct dictionary *d, assert (count == 0 || order != NULL); assert (count <= d->var_cnt); - new_var = xmalloc (d->var_cnt * sizeof *new_var); + new_var = xnmalloc (d->var_cnt, sizeof *new_var); memcpy (new_var, order, count * sizeof *new_var); for (i = 0; i < count; i++) { @@ -609,7 +609,7 @@ dict_rename_vars (struct dictionary *d, /* Remove the variables to be renamed from the name hash, save their names, and rename them. */ - old_names = xmalloc (count * sizeof *old_names); + old_names = xnmalloc (count, sizeof *old_names); for (i = 0; i < count; i++) { assert (d->var[vars[i]->index] == vars[i]); @@ -854,7 +854,7 @@ dict_get_compacted_idx_to_fv (const struct dictionary *d) size_t next_value_idx; int *idx_to_fv; - idx_to_fv = xmalloc (d->var_cnt * sizeof *idx_to_fv); + idx_to_fv = xnmalloc (d->var_cnt, sizeof *idx_to_fv); next_value_idx = 0; for (i = 0; i < d->var_cnt; i++) { @@ -901,7 +901,7 @@ dict_set_split_vars (struct dictionary *d, assert (cnt == 0 || split != NULL); d->split_cnt = cnt; - d->split = xrealloc (d->split, cnt * sizeof *d->split); + d->split = xnrealloc (d->split, cnt, sizeof *d->split); memcpy (d->split, split, cnt * sizeof *d->split); } @@ -979,11 +979,11 @@ dict_create_vector (struct dictionary *d, if (dict_lookup_vector (d, name) != NULL) return 0; - d->vector = xrealloc (d->vector, (d->vector_cnt + 1) * sizeof *d->vector); + d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector); vector = d->vector[d->vector_cnt] = xmalloc (sizeof *vector); vector->idx = d->vector_cnt++; str_copy_trunc (vector->name, sizeof vector->name, name); - vector->var = xmalloc (cnt * sizeof *var); + vector->var = xnmalloc (cnt, sizeof *var); for (i = 0; i < cnt; i++) { assert (dict_contains_var (d, var[i])); diff --git a/src/error.c b/src/error.c index 0d1a5879..28da450e 100644 --- a/src/error.c +++ b/src/error.c @@ -126,7 +126,7 @@ err_push_file_locator (const struct file_locator *f) else mfile_loc *= 2; - file_loc = xrealloc (file_loc, mfile_loc * sizeof *file_loc); + file_loc = xnrealloc (file_loc, mfile_loc, sizeof *file_loc); } file_loc[nfile_loc++] = f; diff --git a/src/examine.q b/src/examine.q index 7f5396a4..fc0c0c9a 100644 --- a/src/examine.q +++ b/src/examine.q @@ -394,7 +394,7 @@ list_to_ptile_hash(const subc_list_double *l) for ( i = 0 ; i < subc_list_double_count(l) ; ++i ) { - struct percentile *p = xmalloc (sizeof (struct percentile)); + struct percentile *p = xmalloc (sizeof *p); p->p = subc_list_double_at(l,i); p->v = SYSMIS; @@ -511,7 +511,7 @@ xmn_custom_variables(struct cmd_examine *cmd ) assert(n_dependent_vars); - totals = xmalloc( sizeof(struct metrics) * n_dependent_vars); + totals = xnmalloc (n_dependent_vars, sizeof *totals); if ( lex_match(T_BY)) { @@ -534,7 +534,7 @@ static int examine_parse_independent_vars(struct cmd_examine *cmd) { int success; - struct factor *sf = xmalloc(sizeof(struct factor)); + struct factor *sf = xmalloc (sizeof *sf); if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL) && token != T_ALL) @@ -1858,7 +1858,7 @@ np_plot(const struct metrics *m, const char *factorname) { /* We have to cache the detrended data, beacause we need to find its limits before we can plot it */ - double *d_data = xmalloc (m->n_data * sizeof(double)); + double *d_data = xnmalloc (m->n_data, sizeof *d_data); double d_max = -DBL_MAX; double d_min = DBL_MAX; for ( i = 0 ; i < m->n_data; ++i ) diff --git a/src/factor_stats.c b/src/factor_stats.c index e090517b..29eebed2 100644 --- a/src/factor_stats.c +++ b/src/factor_stats.c @@ -93,7 +93,7 @@ metrics_calc(struct metrics *fs, const union value *val, assert( (*wv)->v.f == val->f ); (*wv)->w += weight; - cn = xmalloc( sizeof (struct case_node) ) ; + cn = xmalloc ( sizeof *cn); cn->next = (*wv)->case_nos ; cn->num = case_no; @@ -107,7 +107,7 @@ metrics_calc(struct metrics *fs, const union value *val, (*wv)->v = *val; (*wv)->w = weight; - cn = xmalloc( sizeof (struct case_node) ) ; + cn = xmalloc (sizeof *cn); cn->next=0; cn->num = case_no; (*wv)->case_nos = cn; @@ -215,7 +215,7 @@ struct weighted_value * weighted_value_create(void) { struct weighted_value *wv; - wv = xmalloc (sizeof (struct weighted_value )); + wv = xmalloc (sizeof *wv); wv->cc = 0; wv->case_nos = 0; @@ -256,11 +256,11 @@ create_factor_statistics (int n, union value *id0, union value *id1) { struct factor_statistics *f; - f = xmalloc( sizeof ( struct factor_statistics )); + f = xmalloc (sizeof *f); f->id[0] = *id0; f->id[1] = *id1; - f->m = xmalloc( sizeof ( struct metrics ) * n ) ; + f->m = xnmalloc (n, sizeof *f->m); memset (f->m, 0, sizeof(struct metrics) * n); f->n_var = n; diff --git a/src/file-type.c b/src/file-type.c index 5751fd40..0774c683 100644 --- a/src/file-type.c +++ b/src/file-type.c @@ -445,7 +445,7 @@ cmd_record_type (void) if (rct->nv >= mv) { mv += 16; - rct->v = xrealloc (rct->v, mv * sizeof *rct->v); + rct->v = xnrealloc (rct->v, mv, sizeof *rct->v); } if (formats[fty->record.fmt].cat & FCAT_STRING) diff --git a/src/flip.c b/src/flip.c index 8d703170..9fbea2a4 100644 --- a/src/flip.c +++ b/src/flip.c @@ -285,7 +285,7 @@ flip_sink_create (struct flip_pgm *flip) size_t i; info->flip = flip; - info->output_buf = xmalloc (sizeof *info->output_buf * flip->var_cnt); + info->output_buf = xnmalloc (flip->var_cnt, sizeof *info->output_buf); flip->file = tmpfile (); if (!flip->file) @@ -316,7 +316,7 @@ flip_sink_write (struct case_sink *sink, const struct ccase *c) if (flip->new_names != NULL) { - struct varname *v = xmalloc (sizeof (struct varname)); + struct varname *v = xmalloc (sizeof *v); v->next = NULL; if (flip->new_names->type == NUMERIC) { @@ -499,7 +499,7 @@ flip_source_read (struct case_source *source, union value *input_buf; size_t i; - input_buf = xmalloc (sizeof *input_buf * flip->case_cnt); + input_buf = xnmalloc (flip->case_cnt, sizeof *input_buf); for (i = 0; i < flip->var_cnt; i++) { size_t j; diff --git a/src/frequencies.q b/src/frequencies.q index 460efea6..5cff5892 100644 --- a/src/frequencies.q +++ b/src/frequencies.q @@ -720,7 +720,7 @@ postprocess_freq_tab (struct variable *v) data = hsh_data (ft->data); /* Copy dereferenced data into freqs. */ - freqs = xmalloc (count * sizeof *freqs); + freqs = xnmalloc (count, sizeof *freqs); for (i = 0; i < count; i++) { struct freq *f = data[i]; @@ -1603,7 +1603,7 @@ freq_tab_to_slice_array(const struct freq_tab *frq_tab, *n_slices = frq_tab->n_valid; - slices = xmalloc ( *n_slices * sizeof (struct slice ) ); + slices = xnmalloc (*n_slices, sizeof *slices); for (i = 0 ; i < *n_slices ; ++i ) { diff --git a/src/get.c b/src/get.c index a00663a5..af6607ba 100644 --- a/src/get.c +++ b/src/get.c @@ -681,7 +681,7 @@ keep_variables (struct dictionary *dict) dict_reorder_vars (dict, v, nv); /* Delete the remaining variables. */ - v = xrealloc (v, (dict_get_var_cnt (dict) - nv) * sizeof *v); + v = xnrealloc (v, dict_get_var_cnt (dict) - nv, sizeof *v); for (i = nv; i < dict_get_var_cnt (dict); i++) v[i - nv] = dict_get_var (dict, i); dict_delete_vars (dict, v, dict_get_var_cnt (dict) - nv); @@ -921,7 +921,7 @@ cmd_match_files (void) { size_t i; - iter->by = xmalloc (sizeof *iter->by * mtf.by_cnt); + iter->by = xnmalloc (mtf.by_cnt, sizeof *iter->by); for (i = 0; i < mtf.by_cnt; i++) { @@ -1714,7 +1714,7 @@ finish_case_map (struct dictionary *d) map = xmalloc (sizeof *map); map->value_cnt = dict_get_next_value_idx (d); - map->map = xmalloc (sizeof *map->map * map->value_cnt); + map->map = xnmalloc (map->value_cnt, sizeof *map->map); for (i = 0; i < map->value_cnt; i++) map->map[i] = -1; diff --git a/src/groff-font.c b/src/groff-font.c index e5811ef4..33a0b56b 100644 --- a/src/groff-font.c +++ b/src/groff-font.c @@ -492,7 +492,7 @@ font_char_name_to_index (const char *name) hash.size = 128; hash.used = 0; hash.next_index = 256; - hash.tab = xmalloc (sizeof *hash.tab * hash.size); + hash.tab = xnmalloc (hash.size, sizeof *hash.tab); hash.ar = pool_create (); for (i = 0; i < hash.size; i++) hash.tab[i].name = NULL; @@ -514,7 +514,7 @@ font_char_name_to_index (const char *name) int i, j; hash.size *= 2; - hash.tab = xmalloc (sizeof *hash.tab * hash.size); + hash.tab = xnmalloc (hash.size, sizeof *hash.tab); for (i = 0; i < hash.size; i++) hash.tab[i].name = NULL; for (i = 0; i < old_size; i++) @@ -819,8 +819,8 @@ groff_read_DESC (const char *dev_name, struct groff_device_info * dev) if (dev->n_sizes + 2 >= m_sizes) { m_sizes += 1; - dev->sizes = xrealloc (dev->sizes, - m_sizes * sizeof *dev->sizes); + dev->sizes = xnrealloc (dev->sizes, + m_sizes, sizeof *dev->sizes); } dev->sizes[dev->n_sizes++][0] = lower; dev->sizes[dev->n_sizes][1] = upper; diff --git a/src/hash.c b/src/hash.c index dd6bc3d2..bcf5244f 100644 --- a/src/hash.c +++ b/src/hash.c @@ -178,7 +178,7 @@ hsh_create (int size, hsh_compare_func *compare, hsh_hash_func *hash, if (size < 4) size = 4; h->size = next_power_of_2 (size); - h->entries = xmalloc (sizeof *h->entries * h->size); + h->entries = xnmalloc (h->size, sizeof *h->entries); for (i = 0; i < h->size; i++) h->entries[i] = NULL; h->aux = aux; @@ -268,7 +268,7 @@ rehash (struct hsh_table *h, size_t new_size) end = begin + h->size; h->size = new_size; - h->entries = xmalloc (sizeof *h->entries * h->size); + h->entries = xnmalloc (h->size, sizeof *h->entries); for (i = 0; i < h->size; i++) h->entries[i] = NULL; for (table_p = begin; table_p < end; table_p++) @@ -385,7 +385,7 @@ hsh_data_copy (struct hsh_table *h) void **copy; assert (h != NULL); - copy = xmalloc ((h->used + 1) * sizeof *copy); + copy = xnmalloc ((h->used + 1), sizeof *copy); copy_if (h->entries, h->size, sizeof *h->entries, copy, not_null, NULL); copy[h->used] = NULL; return copy; diff --git a/src/inpt-pgm.c b/src/inpt-pgm.c index 0225f3f7..eb2503c7 100644 --- a/src/inpt-pgm.c +++ b/src/inpt-pgm.c @@ -96,7 +96,7 @@ cmd_end_input_program (void) /* Figure out how to initialize each input case. */ inp = xmalloc (sizeof *inp); inp->init_cnt = dict_get_next_value_idx (default_dict); - inp->init = xmalloc (inp->init_cnt * sizeof *inp->init); + inp->init = xnmalloc (inp->init_cnt, sizeof *inp->init); for (i = 0; i < inp->init_cnt; i++) inp->init[i] = -1; for (i = 0; i < dict_get_var_cnt (default_dict); i++) diff --git a/src/levene.c b/src/levene.c index 04f5a1de..5de52205 100644 --- a/src/levene.c +++ b/src/levene.c @@ -158,7 +158,7 @@ levene_precalc (const struct levene_info *l) { size_t i; - lz = xmalloc (sizeof (struct lz_stats ) * l->n_dep ) ; + lz = xnmalloc (l->n_dep, sizeof *lz); for(i = 0; i < l->n_dep ; ++i ) { @@ -265,7 +265,7 @@ levene2_precalc (void *_l) struct levene_info *l = (struct levene_info *) _l; - lz_denominator = (double *) xmalloc(sizeof(double) * l->n_dep); + lz_denominator = xnmalloc (l->n_dep, sizeof *lz_denominator); /* This stuff could go in the first post calc . . . */ for (v = 0; v < l->n_dep; ++v) diff --git a/src/list.q b/src/list.q index d55e76cc..f595dc0c 100644 --- a/src/list.q +++ b/src/list.q @@ -191,9 +191,8 @@ cmd_list (void) { /* Add the weight variable to the end of the variable list. */ cmd.n_variables++; - cmd.v_variables = xrealloc (cmd.v_variables, - (cmd.n_variables - * sizeof *cmd.v_variables)); + cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables, + sizeof *cmd.v_variables); cmd.v_variables[cmd.n_variables - 1] = dict_get_weight (default_dict); } @@ -215,8 +214,8 @@ cmd_list (void) /* Add the weight variable at the beginning of the variable list. */ cmd.n_variables++; - cmd.v_variables = xrealloc (cmd.v_variables, - cmd.n_variables * sizeof *cmd.v_variables); + cmd.v_variables = xnrealloc (cmd.v_variables, + cmd.n_variables, sizeof *cmd.v_variables); memmove (&cmd.v_variables[1], &cmd.v_variables[0], (cmd.n_variables - 1) * sizeof *cmd.v_variables); cmd.v_variables[0] = &casenum_var; @@ -303,7 +302,7 @@ write_header (struct outp_driver *d) size_t x; /* Allocate, initialize header. */ - prc->header = xmalloc (sizeof (char *) * prc->header_rows); + prc->header = xnmalloc (prc->header_rows, sizeof *prc->header); { int w = n_chars_width (d); for (i = 0; i < prc->header_rows; i++) diff --git a/src/matrix-data.c b/src/matrix-data.c index 03b06ebc..db6189d1 100644 --- a/src/matrix-data.c +++ b/src/matrix-data.c @@ -961,11 +961,11 @@ read_matrices_without_rowtype (struct matrix_data_pgm *mx) nr.mx = mx; nr.data = NULL; - nr.factor_values = xmalloc (sizeof *nr.factor_values - * mx->n_factors * mx->cells); + nr.factor_values = xnmalloc (mx->n_factors * mx->cells, + sizeof *nr.factor_values); nr.max_cell_idx = 0; - nr.split_values = xmalloc (sizeof *nr.split_values - * dict_get_split_cnt (default_dict)); + nr.split_values = xnmalloc (dict_get_split_cnt (default_dict), + sizeof *nr.split_values); vfm_source = create_case_source (&matrix_data_without_rowtype_source_class, &nr); @@ -1549,7 +1549,7 @@ wr_read_splits (struct wr_aux_data *wr, else { compare = 0; - wr->split_values = xmalloc (split_cnt * sizeof *wr->split_values); + wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values); } { @@ -1644,7 +1644,7 @@ wr_output_data (struct wr_aux_data *wr, struct factor_data *iter; int i; - factors = xmalloc (sizeof *factors * mx->cells); + factors = xnmalloc (mx->cells, sizeof *factors); for (i = 0, iter = wr->data; iter; iter = iter->next, i++) factors[i] = iter; diff --git a/src/means.q b/src/means.q index baad486b..887cbfd8 100644 --- a/src/means.q +++ b/src/means.q @@ -153,8 +153,8 @@ mns_custom_tables (struct cmd_means *cmd) goto lossage; n_dim++; - nv_dim = xrealloc (nv_dim, n_dim * sizeof (int)); - v_dim = xrealloc (v_dim, n_dim * sizeof (struct variable **)); + nv_dim = xnrealloc (nv_dim, n_dim, sizeof *nv_dim); + v_dim = xnrealloc (v_dim, n_dim, sizeof *v_dim); nv_dim[n_dim - 1] = nvl; v_dim[n_dim - 1] = vl; diff --git a/src/modify-vars.c b/src/modify-vars.c index 5f1ad4c9..4d01b6a9 100644 --- a/src/modify-vars.c +++ b/src/modify-vars.c @@ -250,7 +250,7 @@ cmd_modify_vars (void) assert (all_cnt >= keep_cnt); drop_cnt = all_cnt - keep_cnt; - drop_vars = xmalloc (drop_cnt * sizeof *keep_vars); + drop_vars = xnmalloc (drop_cnt, sizeof *keep_vars); if (set_difference (all_vars, all_cnt, keep_vars, keep_cnt, sizeof *all_vars, @@ -403,7 +403,7 @@ validate_var_modification (const struct dictionary *d, /* Drop variables, in index order. */ drop_cnt = vm->drop_cnt; - drop_vars = xmalloc (drop_cnt * sizeof *drop_vars); + drop_vars = xnmalloc (drop_cnt, sizeof *drop_vars); memcpy (drop_vars, vm->drop_vars, drop_cnt * sizeof *drop_vars); sort (drop_vars, drop_cnt, sizeof *drop_vars, compare_variables_given_ordering, &forward_positional_ordering); @@ -411,7 +411,7 @@ validate_var_modification (const struct dictionary *d, /* Keep variables, in index order. */ assert (all_cnt >= drop_cnt); keep_cnt = all_cnt - drop_cnt; - keep_vars = xmalloc (keep_cnt * sizeof *keep_vars); + keep_vars = xnmalloc (keep_cnt, sizeof *keep_vars); if (set_difference (all_vars, all_cnt, drop_vars, drop_cnt, sizeof *all_vars, @@ -421,7 +421,7 @@ validate_var_modification (const struct dictionary *d, assert (0); /* Copy variables into var_renaming array. */ - var_renaming = xmalloc (keep_cnt * sizeof *var_renaming); + var_renaming = xnmalloc (keep_cnt, sizeof *var_renaming); for (i = 0; i < keep_cnt; i++) { var_renaming[i].var = keep_vars[i]; @@ -486,7 +486,7 @@ rearrange_dict (struct dictionary *d, const struct var_modification *vm) /* Record the old names of variables to rename. After variables are deleted, we can't depend on the variables to still exist, but we can still look them up by name. */ - rename_old_names = xmalloc (vm->rename_cnt * sizeof *rename_old_names); + rename_old_names = xnmalloc (vm->rename_cnt, sizeof *rename_old_names); for (i = 0; i < vm->rename_cnt; i++) rename_old_names[i] = xstrdup (vm->rename_vars[i]->name); @@ -495,8 +495,8 @@ rearrange_dict (struct dictionary *d, const struct var_modification *vm) dict_delete_vars (d, vm->drop_vars, vm->drop_cnt); /* Compose lists of variables to rename and their new names. */ - rename_vars = xmalloc (vm->rename_cnt * sizeof *rename_vars); - rename_new_names = xmalloc (vm->rename_cnt * sizeof *rename_new_names); + rename_vars = xnmalloc (vm->rename_cnt, sizeof *rename_vars); + rename_new_names = xnmalloc (vm->rename_cnt, sizeof *rename_new_names); rename_cnt = 0; for (i = 0; i < vm->rename_cnt; i++) { diff --git a/src/moments.c b/src/moments.c index 00e0ac80..3c5e3840 100644 --- a/src/moments.c +++ b/src/moments.c @@ -523,8 +523,8 @@ read_values (double **values, double **weights, size_t *cnt) if (*cnt >= cap) { cap = 2 * (cap + 8); - *values = xrealloc (*values, sizeof **values * cap); - *weights = xrealloc (*weights, sizeof **weights * cap); + *values = xnrealloc (*values, cap, sizeof **values); + *weights = xnrealloc (*weights, cap, sizeof **weights); } (*values)[*cnt] = value; diff --git a/src/oneway.q b/src/oneway.q index 26f2f0ce..f413cda1 100644 --- a/src/oneway.q +++ b/src/oneway.q @@ -157,7 +157,7 @@ output_oneway(void) size_t i; short *bad_contrast ; - bad_contrast = xmalloc ( sizeof (short) * cmd.sbc_contrast ); + bad_contrast = xnmalloc (cmd.sbc_contrast, sizeof *bad_contrast); /* Check the sanity of the given contrast values */ for (i = 0 ; i < cmd.sbc_contrast ; ++i ) @@ -949,9 +949,7 @@ run_oneway(const struct casefile *cf, void *cmd_) if ( ! gs ) { - gs = (struct group_statistics *) - xmalloc (sizeof(struct group_statistics)); - + gs = xmalloc (sizeof *gs); gs->id = *indep_val; gs->sum=0; gs->n=0; diff --git a/src/output.c b/src/output.c index ab8c10f1..292a334c 100644 --- a/src/output.c +++ b/src/output.c @@ -911,7 +911,7 @@ outp_match_keyword (const char *s, struct outp_option *tab, *++cp = 0; info->initial = xstrdup (s); - info->options = xmalloc (sizeof *info->options * (cp - s)); + info->options = xnmalloc (cp - s, sizeof *info->options); memcpy (info->options, ptr, sizeof *info->options * (cp - s)); } diff --git a/src/pfm-write.c b/src/pfm-write.c index be9812c5..c3562f34 100644 --- a/src/pfm-write.c +++ b/src/pfm-write.c @@ -129,7 +129,7 @@ pfm_open_writer (struct file_handle *fh, struct dictionary *dict, w->vars = NULL; w->var_cnt = dict_get_var_cnt (dict); - w->vars = xmalloc (sizeof *w->vars * w->var_cnt); + w->vars = xnmalloc (w->var_cnt, sizeof *w->vars); for (i = 0; i < w->var_cnt; i++) { const struct variable *dv = dict_get_var (dict, i); diff --git a/src/plot-chart.c b/src/plot-chart.c index a866f4f3..bfe38c81 100644 --- a/src/plot-chart.c +++ b/src/plot-chart.c @@ -58,7 +58,7 @@ chart_create(void) if (d == NULL) return NULL; - chart = xmalloc(sizeof(struct chart) ); + chart = xmalloc (sizeof *chart); d->class->initialise_chart(d, chart); if (!chart->lp) { diff --git a/src/postscript.c b/src/postscript.c index fd738aba..5acb02f5 100644 --- a/src/postscript.c +++ b/src/postscript.c @@ -320,7 +320,7 @@ ps_preopen_driver (struct outp_driver *this) assert (this->driver_open == 0); msg (VM (1), _("PostScript driver initializing as `%s'..."), this->name); - this->ext = x = xmalloc (sizeof (struct ps_driver_ext)); + this->ext = x = xmalloc (sizeof *x); this->res = PSUS; this->horiz = this->vert = 1; this->width = this->length = 0; @@ -1902,7 +1902,7 @@ line (struct outp_driver *this, int type, int ind, int dep1, int dep2) if ((*f)->ndep >= (*f)->mdep) { (*f)->mdep += 16; - *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1))); + *f = xrealloc (*f, sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)); } (*f)->dep[(*f)->ndep][0] = dep1; (*f)->dep[(*f)->ndep][1] = dep2; @@ -2735,7 +2735,7 @@ text (struct outp_driver *this, struct outp_text *t, int draw) } else { - buf = xrealloc (buf, sizeof *buf * buf_len * 2); + buf = xnrealloc (buf, buf_len * 2, sizeof *buf); buf_loc = buf + buf_len; buf_end = buf + buf_len * 2; } diff --git a/src/q2c.c b/src/q2c.c index bc7af967..3bba2390 100644 --- a/src/q2c.c +++ b/src/q2c.c @@ -284,7 +284,7 @@ add_symbol (const char *name, int unique, int value) symbol *iter, *sym; int x; - sym = xmalloc (sizeof (symbol)); + sym = xmalloc (sizeof *sym); sym->name = xstrdup (name); sym->unique = unique; sym->value = value; @@ -724,7 +724,7 @@ parse_specifier (specifier *spec, subcommand *sbc) for (;;) { - *s = xmalloc (sizeof (setting)); + *s = xmalloc (sizeof **s); parse_setting (*s, spec); if (token == ',' || token == ';' || token == '.') break; @@ -749,7 +749,7 @@ parse_specifiers (subcommand *sbc) for (;;) { - *spec = xmalloc (sizeof (specifier)); + *spec = xmalloc (sizeof **spec); parse_specifier (*spec, sbc); if (token == ';' || token == '.') break; @@ -893,7 +893,7 @@ parse_subcommands (void) for (;;) { - *sbc = xmalloc (sizeof (subcommand)); + *sbc = xmalloc (sizeof **sbc); (*sbc)->next = NULL; parse_subcommand (*sbc); @@ -2251,7 +2251,7 @@ aux_parse (void) for (;;) { - sbc = xmalloc(sizeof(aux_subcommand)); + sbc = xmalloc (sizeof *sbc); sbc->next = prevsbc; sbc->name = xstrdup (tokstr); lex_get(); diff --git a/src/rank.q b/src/rank.q index 56493cfe..77a6dbe8 100644 --- a/src/rank.q +++ b/src/rank.q @@ -224,11 +224,11 @@ parse_rank_function(struct cmd_rank *cmd UNUSED, enum RANK_FUNC f) int var_count = 0; n_rank_specs++; - rank_specs = xrealloc(rank_specs, n_rank_specs * sizeof *rank_specs); + rank_specs = xnrealloc(rank_specs, n_rank_specs, sizeof *rank_specs); rank_specs[n_rank_specs - 1].rfunc = f; rank_specs[n_rank_specs - 1].destvars = - xcalloc(sc->crit_cnt ,sizeof (struct variable *)); + xcalloc (sc->crit_cnt, sizeof (struct variable *)); if (lex_match_id("INTO")) { diff --git a/src/recode.c b/src/recode.c index b630d029..b364a1ac 100644 --- a/src/recode.c +++ b/src/recode.c @@ -524,7 +524,7 @@ parse_src_spec (struct rcd_var * rcd, int type, size_t max_src_width) if (rcd->nmap + 1 >= rcd->mmap) { rcd->mmap += 16; - rcd->map = xrealloc (rcd->map, rcd->mmap * sizeof *rcd->map); + rcd->map = xnrealloc (rcd->map, rcd->mmap, sizeof *rcd->map); } c = &rcd->map[rcd->nmap]; diff --git a/src/regression.q b/src/regression.q index d3a81e40..ca0b65f5 100644 --- a/src/regression.q +++ b/src/regression.q @@ -506,11 +506,11 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED) n_data = casefile_get_case_cnt (cf); n_indep = cmd.n_variables - cmd.n_dependent; - indep_vars = (size_t *) malloc (n_indep * sizeof (*indep_vars)); + indep_vars = xnmalloc (n_indep, sizeof *indep_vars); Y = gsl_vector_alloc (n_data); lopts.get_depvar_mean_std = 1; - lopts.get_indep_mean_std = (int *) malloc (n_indep * sizeof (int)); + lopts.get_indep_mean_std = xnmalloc (n_indep, sizeof (int)); lcache = pspp_linreg_cache_alloc (n_data, n_indep); lcache->indep_means = gsl_vector_alloc (n_indep); diff --git a/src/repeat.c b/src/repeat.c index 593263f8..c0510c5d 100644 --- a/src/repeat.c +++ b/src/repeat.c @@ -355,8 +355,8 @@ parse_ids (struct repeat_entry * e) if (!parse_mixed_vars (&names, &nnames, PV_NONE)) return 0; - e->replacement = xrealloc (e->replacement, - (nnames + n) * sizeof *e->replacement); + e->replacement = xnrealloc (e->replacement, + nnames + n, sizeof *e->replacement); for (i = 0; i < nnames; i++) { e->replacement[n + i] = xstrdup (names[i]); @@ -418,8 +418,8 @@ parse_numbers (struct repeat_entry *e) if (n + (abs (b - a) + 1) > m) { m = n + (abs (b - a) + 1) + 16; - e->replacement = array = xrealloc (array, - m * sizeof *e->replacement); + e->replacement = array = xnrealloc (array, + m, sizeof *e->replacement); } if (a == b) @@ -470,8 +470,8 @@ parse_strings (struct repeat_entry * e) if (n + 1 > m) { m += 16; - e->replacement = string = xrealloc (string, - m * sizeof *e->replacement); + e->replacement = string = xnrealloc (string, + m, sizeof *e->replacement); } string[n++] = lex_token_representation (); lex_get (); @@ -479,7 +479,7 @@ parse_strings (struct repeat_entry * e) lex_match (','); } while (token != '/' && token != '.'); - e->replacement = xrealloc (string, n * sizeof *e->replacement); + e->replacement = xnrealloc (string, n, sizeof *e->replacement); return n; } diff --git a/src/set.q b/src/set.q index 6bbfd5dc..eb0a7331 100644 --- a/src/set.q +++ b/src/set.q @@ -1406,7 +1406,7 @@ set_rng (unsigned long seed) { rng = gsl_rng_alloc (gsl_rng_mt19937); if (rng == NULL) - out_of_memory (); + xalloc_die (); gsl_rng_set (rng, seed); } diff --git a/src/sfm-read.c b/src/sfm-read.c index 226c8b31..109c9a1a 100644 --- a/src/sfm-read.c +++ b/src/sfm-read.c @@ -758,10 +758,10 @@ read_variables (struct sfm_reader *r, *var_by_idx = 0; /* Pre-allocate variables. */ - if ( r->value_cnt != -1 ) + if (r->value_cnt != -1) { - *var_by_idx = xmalloc(r->value_cnt * sizeof (**var_by_idx)); - r->vars = xmalloc( r->value_cnt * sizeof (*r->vars) ); + *var_by_idx = xnmalloc (r->value_cnt, sizeof **var_by_idx); + r->vars = xnmalloc (r->value_cnt, sizeof *r->vars); } @@ -799,8 +799,8 @@ read_variables (struct sfm_reader *r, if ( -1 == r->value_cnt ) { - *var_by_idx = xrealloc (*var_by_idx, sizeof **var_by_idx * (i + 1)); - r->vars = xrealloc(r->vars, (i + 1) * sizeof (*r->vars) ); + *var_by_idx = xnrealloc (*var_by_idx, i + 1, sizeof **var_by_idx); + r->vars = xnrealloc (r->vars, i + 1, sizeof *r->vars); } /* If there was a long string previously, make sure that the @@ -1066,7 +1066,7 @@ read_value_labels (struct sfm_reader *r, } /* Allocate memory. */ - labels = xcalloc (n_labels , sizeof *labels); + labels = xcalloc (n_labels, sizeof *labels); for (i = 0; i < n_labels; i++) labels[i].label = NULL; @@ -1118,7 +1118,7 @@ read_value_labels (struct sfm_reader *r, handle_get_filename (r->fh), n_vars, dict_get_var_cnt (dict))); /* Read the list of variables. */ - var = xmalloc (n_vars * sizeof *var); + var = xnmalloc (n_vars, sizeof *var); for (i = 0; i < n_vars; i++) { int32 var_idx; @@ -1306,7 +1306,7 @@ buffer_input (struct sfm_reader *r) size_t amt; if (r->buf == NULL) - r->buf = xmalloc (sizeof *r->buf * 128); + r->buf = xnmalloc (128, sizeof *r->buf); amt = fread (r->buf, sizeof *r->buf, 128, r->file); if (ferror (r->file)) { diff --git a/src/sfm-write.c b/src/sfm-write.c index bdde3cf6..d06a65f5 100644 --- a/src/sfm-write.c +++ b/src/sfm-write.c @@ -173,7 +173,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, w->x = w->y = NULL; w->var_cnt = dict_get_var_cnt (d); - w->vars = xmalloc (sizeof *w->vars * w->var_cnt); + w->vars = xnmalloc (w->var_cnt, sizeof *w->vars); for (i = 0; i < w->var_cnt; i++) { const struct variable *dv = dict_get_var (d, i); @@ -242,7 +242,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, if (w->compress) { - w->buf = xmalloc (sizeof *w->buf * 128); + w->buf = xnmalloc (128, sizeof *w->buf); w->ptr = w->buf; w->end = &w->buf[128]; w->x = (unsigned char *) w->ptr++; diff --git a/src/sort-prs.c b/src/sort-prs.c index 3966eac1..6ef6a6fa 100644 --- a/src/sort-prs.c +++ b/src/sort-prs.c @@ -105,8 +105,8 @@ sort_parse_criteria (const struct dictionary *dict, else direction = SRT_ASCEND; - criteria->crits = xrealloc (criteria->crits, - sizeof *criteria->crits * *var_cnt); + criteria->crits = xnrealloc (criteria->crits, + *var_cnt, sizeof *criteria->crits); criteria->crit_cnt = *var_cnt; for (; prev_var_cnt < criteria->crit_cnt; prev_var_cnt++) { diff --git a/src/sort.c b/src/sort.c index 80380e7d..47cd6a09 100644 --- a/src/sort.c +++ b/src/sort.c @@ -195,7 +195,7 @@ do_internal_sort (struct casereader *reader, dst = casefile_create (casefile_get_value_cnt (src)); if (case_cnt != 0) { - struct indexed_case *cases = malloc (sizeof *cases * case_cnt); + struct indexed_case *cases = nmalloc (sizeof *cases, case_cnt); if (cases != NULL) { unsigned long i; @@ -278,7 +278,7 @@ do_external_sort (struct casereader *reader, xsrt->value_cnt = casefile_get_value_cnt (casereader_get_casefile (reader)); xsrt->run_cap = 512; xsrt->run_cnt = 0; - xsrt->runs = xmalloc (sizeof *xsrt->runs * xsrt->run_cap); + xsrt->runs = xnmalloc (xsrt->run_cap, sizeof *xsrt->runs); if (write_runs (xsrt, reader)) { struct casefile *output = merge (xsrt); @@ -446,13 +446,14 @@ allocate_cases (struct initial_run_state *irs) max_cases = get_max_workspace() / approx_case_cost; if (max_cases > max_buffers) max_cases = max_buffers; - irs->records = malloc (sizeof *irs->records * max_cases); - for (i = 0; i < max_cases; i++) - if (!case_try_create (&irs->records[i].record, irs->xsrt->value_cnt)) - { - max_cases = i; - break; - } + irs->records = nmalloc (sizeof *irs->records, max_cases); + if (irs->records != NULL) + for (i = 0; i < max_cases; i++) + if (!case_try_create (&irs->records[i].record, irs->xsrt->value_cnt)) + { + max_cases = i; + break; + } irs->record_cap = max_cases; /* Fail if we didn't allocate an acceptable number of cases. */ @@ -548,8 +549,8 @@ end_run (struct initial_run_state *irs) if (xsrt->run_cnt >= xsrt->run_cap) { xsrt->run_cap *= 2; - xsrt->runs = xrealloc (xsrt->runs, - sizeof *xsrt->runs * xsrt->run_cap); + xsrt->runs = xnrealloc (xsrt->runs, + xsrt->run_cap, sizeof *xsrt->runs); } xsrt->runs[xsrt->run_cnt++] = irs->casefile; irs->casefile = NULL; @@ -674,7 +675,7 @@ merge_once (struct external_sort *xsrt, int i; /* Open input files. */ - runs = xmalloc (sizeof *runs * run_cnt); + runs = xnmalloc (run_cnt, sizeof *runs); for (i = 0; i < run_cnt; i++) { struct run *r = &runs[i]; diff --git a/src/subclist.c b/src/subclist.c index 6f5898f1..95ea455d 100644 --- a/src/subclist.c +++ b/src/subclist.c @@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA #include "subclist.h" #include +#include "xalloc.h" /* I call these objects `lists' but they are in fact simple dynamic arrays */ @@ -32,7 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA void subc_list_double_create(subc_list_double *l) { - l->data = (double *) malloc(CHUNKSIZE * sizeof (double)); + l->data = xnmalloc (CHUNKSIZE, sizeof *l->data); l->sz = CHUNKSIZE; l->n_data = 0; } @@ -46,7 +47,7 @@ subc_list_double_push(subc_list_double *l, double d) if (l->n_data >= l->sz ) { l->sz += CHUNKSIZE; - l->data = realloc(l->data, l->sz * sizeof(double)); + l->data = xnrealloc (l->data, l->sz, sizeof *l->data); } } diff --git a/src/sysfile-info.c b/src/sysfile-info.c index fe26a7c0..b5f559e5 100644 --- a/src/sysfile-info.c +++ b/src/sysfile-info.c @@ -576,7 +576,7 @@ display_vectors (int sorted) return; } - vl = xmalloc (sizeof *vl * nvec); + vl = xnmalloc (nvec, sizeof *vl); for (i = 0; i < nvec; i++) vl[i] = dict_get_vector (default_dict, i); if (sorted) diff --git a/src/t-test.q b/src/t-test.q index 5334a27a..5fdb802d 100644 --- a/src/t-test.q +++ b/src/t-test.q @@ -310,8 +310,8 @@ cmd_t_test(void) assert(cmd.n_variables == 0); cmd.n_variables = hsh_count(hash); - cmd.v_variables = xrealloc(cmd.v_variables, - sizeof(struct variable) * cmd.n_variables); + cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables, + sizeof *cmd.v_variables); /* Iterate through the hash */ for (i=0,v = (struct variable *) hsh_first(hash,&hi); v != 0; @@ -509,7 +509,7 @@ tts_custom_pairs (struct cmd_t_test *cmd UNUSED) /* Allocate storage for the pairs */ - pairs = xrealloc(pairs, sizeof(struct pair) * (n_pairs + n_pairs_local) ); + pairs = xnrealloc (pairs, n_pairs + n_pairs_local, sizeof *pairs); /* Populate the pairs with the appropriate variables */ if ( paired ) @@ -1711,8 +1711,7 @@ group_precalc (struct cmd_t_test *cmd ) for (j=0 ; j < 2 ; ++j) { - struct group_statistics *gs = (struct group_statistics *) - xmalloc (sizeof(struct group_statistics)); + struct group_statistics *gs = xmalloc (sizeof *gs); gs->sum = 0; gs->n = 0; diff --git a/src/vars-prs.c b/src/vars-prs.c index bed6031d..920f8c91 100644 --- a/src/vars-prs.c +++ b/src/vars-prs.c @@ -164,7 +164,7 @@ add_variable (struct variable ***v, size_t *nv, size_t *mv, if (*nv >= *mv) { *mv = 2 * (*nv + 1); - *v = xrealloc (*v, *mv * sizeof **v); + *v = xnrealloc (*v, *mv, sizeof **v); } if ((pv_opts & PV_DUPLICATE) || !included[idx]) @@ -426,7 +426,7 @@ parse_DATA_LIST_vars (char ***names, size_t *nnames, int pv_opts) if (mvar < nvar + (n2 - n1 + 1)) { mvar += ROUND_UP (n2 - n1 + 1, 16); - *names = xrealloc (*names, mvar * sizeof **names); + *names = xnrealloc (*names, mvar, sizeof **names); } for (n = n1; n <= n2; n++) @@ -442,7 +442,7 @@ parse_DATA_LIST_vars (char ***names, size_t *nnames, int pv_opts) if (nvar >= mvar) { mvar += 16; - *names = xrealloc (*names, mvar * sizeof **names); + *names = xnrealloc (*names, mvar, sizeof **names); } (*names)[nvar++] = xstrdup (name1); } @@ -495,7 +495,7 @@ parse_mixed_vars (char ***names, size_t *nnames, int pv_opts) if (!parse_variables (default_dict, &v, &nv, PV_NONE)) goto fail; - *names = xrealloc (*names, (*nnames + nv) * sizeof **names); + *names = xnrealloc (*names, *nnames + nv, sizeof **names); for (i = 0; i < nv; i++) (*names)[*nnames + i] = xstrdup (v[i]->name); free (v); diff --git a/src/vfm.c b/src/vfm.c index b88d3eb1..08e3ca30 100644 --- a/src/vfm.c +++ b/src/vfm.c @@ -227,7 +227,7 @@ open_active_file (void) lag_count = 0; lag_head = 0; - lag_queue = xmalloc (n_lag * sizeof *lag_queue); + lag_queue = xnmalloc (n_lag, sizeof *lag_queue); for (i = 0; i < n_lag; i++) case_nullify (&lag_queue[i]); } @@ -608,7 +608,7 @@ add_transformation (struct trns_header * trns) if (n_trns >= m_trns) { m_trns += 16; - t_trns = xrealloc (t_trns, sizeof *t_trns * m_trns); + t_trns = xnrealloc (t_trns, m_trns, sizeof *t_trns); } t_trns[n_trns] = trns; trns->index = n_trns++; -- 2.30.2