+Tue Oct 25 21:56:23 2005 Ben Pfaff <blp@gnu.org>
+
+ 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 <john@darrington.wattle.id.au>
Dictionary abstraction part #2
{
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;
}
#include <config.h>
#include "alloc.h"
-#include <stdio.h>
#include <stdlib.h>
-#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;
}
/* malloc() wrapper functions. */
#include "xalloc.h"
-void out_of_memory (void) NO_RETURN;
-\f
+
+void *nmalloc (size_t n, size_t s);
+
/* alloca() wrapper functions. */
#if defined (HAVE_ALLOCA) || defined (C_ALLOCA)
#ifdef HAVE_ALLOCA_H
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;
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;
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];
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] = ' ';
}
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,
case_create (struct ccase *c, size_t value_cnt)
{
if (!case_try_create (c, value_cnt))
- out_of_memory ();
+ xalloc_die ();
}
#ifdef GLOBAL_DEBUGGING
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);
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;
}
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);
}
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;
}
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];
}
}
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]);
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++;
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++)
}
}
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;
/* 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;
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++];
/* 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;
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 ())
/* 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;
/* 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;
/* 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;
/* 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;
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;
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;
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;
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.;
}
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;
}
}
/* 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;
}
/* 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;
}
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,
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;
{
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.;
/* 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;
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;
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];
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;
}
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);
}
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];
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];
}
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))))
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++;
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++)
{
/* 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]);
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++)
{
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);
}
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]));
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;
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;
assert(n_dependent_vars);
- totals = xmalloc( sizeof(struct metrics) * n_dependent_vars);
+ totals = xnmalloc (n_dependent_vars, sizeof *totals);
if ( lex_match(T_BY))
{
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)
{
/* 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 )
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;
(*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;
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;
{
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;
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)
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)
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)
{
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;
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];
*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 )
{
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);
{
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++)
{
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;
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;
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++)
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;
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;
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++)
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;
/* 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++)
{
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 )
{
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)
{
/* 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);
}
/* 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;
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++)
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);
else
{
compare = 0;
- wr->split_values = xmalloc (split_cnt * sizeof *wr->split_values);
+ wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
}
{
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;
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;
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,
/* 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);
/* 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,
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];
/* 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);
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++)
{
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;
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 )
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;
*++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));
}
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);
if (d == NULL)
return NULL;
- chart = xmalloc(sizeof(struct chart) );
+ chart = xmalloc (sizeof *chart);
d->class->initialise_chart(d, chart);
if (!chart->lp)
{
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;
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;
}
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;
}
symbol *iter, *sym;
int x;
- sym = xmalloc (sizeof (symbol));
+ sym = xmalloc (sizeof *sym);
sym->name = xstrdup (name);
sym->unique = unique;
sym->value = value;
for (;;)
{
- *s = xmalloc (sizeof (setting));
+ *s = xmalloc (sizeof **s);
parse_setting (*s, spec);
if (token == ',' || token == ';' || token == '.')
break;
for (;;)
{
- *spec = xmalloc (sizeof (specifier));
+ *spec = xmalloc (sizeof **spec);
parse_specifier (*spec, sbc);
if (token == ';' || token == '.')
break;
for (;;)
{
- *sbc = xmalloc (sizeof (subcommand));
+ *sbc = xmalloc (sizeof **sbc);
(*sbc)->next = NULL;
parse_subcommand (*sbc);
for (;;)
{
- sbc = xmalloc(sizeof(aux_subcommand));
+ sbc = xmalloc (sizeof *sbc);
sbc->next = prevsbc;
sbc->name = xstrdup (tokstr);
lex_get();
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"))
{
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];
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);
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]);
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)
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 ();
lex_match (',');
}
while (token != '/' && token != '.');
- e->replacement = xrealloc (string, n * sizeof *e->replacement);
+ e->replacement = xnrealloc (string, n, sizeof *e->replacement);
return n;
}
{
rng = gsl_rng_alloc (gsl_rng_mt19937);
if (rng == NULL)
- out_of_memory ();
+ xalloc_die ();
gsl_rng_set (rng, seed);
}
*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);
}
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
}
/* 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;
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;
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))
{
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);
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++;
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++)
{
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;
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);
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. */
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;
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];
#include "subclist.h"
#include <stdlib.h>
+#include "xalloc.h"
/* I call these objects `lists' but they are in fact simple dynamic arrays */
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;
}
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);
}
}
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)
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;
/* 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 )
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;
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])
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++)
if (nvar >= mvar)
{
mvar += 16;
- *names = xrealloc (*names, mvar * sizeof **names);
+ *names = xnrealloc (*names, mvar, sizeof **names);
}
(*names)[nvar++] = xstrdup (name1);
}
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);
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]);
}
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++;