bska fixed integer overflow in malloc call

Signed-off-by: Bård Skaflestad <Bard.Skaflestad@sintef.no>
This commit is contained in:
Halvor M. Nilsen 2011-10-19 07:35:37 +00:00 committed by Bård Skaflestad
parent f2b111f504
commit 1f0b08755c

View File

@ -51,6 +51,7 @@ void free_sparse_table (sparse_table_t *tab)
sparse_table_t *malloc_sparse_table(int m, int n, int datasz) sparse_table_t *malloc_sparse_table(int m, int n, int datasz)
{ {
size_t alloc_sz;
sparse_table_t *tab = malloc(sizeof *tab); sparse_table_t *tab = malloc(sizeof *tab);
tab->m = m; tab->m = m;
tab->n = n; tab->n = n;
@ -61,8 +62,9 @@ sparse_table_t *malloc_sparse_table(int m, int n, int datasz)
return NULL; return NULL;
} }
alloc_sz = datasz;
if(!(tab->data = malloc(n * datasz))){ alloc_sz *= n;
if(!(tab->data = malloc(alloc_sz))){
fprintf(stderr, "Could not allocate space for sparse data(%d)\n", n); fprintf(stderr, "Could not allocate space for sparse data(%d)\n", n);
free_sparse_table(tab); free_sparse_table(tab);
return NULL; return NULL;