Skip to content

Commit

Permalink
Fix illegal memory access
Browse files Browse the repository at this point in the history
As reported by Coverity Scan
  • Loading branch information
tbeu committed Jul 23, 2024
1 parent a148eec commit 11cc1e4
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions src/mat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1186,43 +1186,56 @@ Mat_VarCreate(const char *name, enum matio_classes class_type, enum matio_types
matvar->data = (void *)data;
matvar->mem_conserve = 1;
} else if ( MAT_C_SPARSE == matvar->class_type ) {
mat_sparse_t *sparse_data;
const mat_sparse_t *sparse_data_in;

sparse_data_in = (const mat_sparse_t *)data;
sparse_data = (mat_sparse_t *)malloc(sizeof(mat_sparse_t));
const mat_sparse_t *sparse_data_in = (const mat_sparse_t *)data;
mat_sparse_t *sparse_data = (mat_sparse_t *)malloc(sizeof(mat_sparse_t));
if ( NULL != sparse_data ) {
sparse_data->nzmax = sparse_data_in->nzmax;
sparse_data->nir = sparse_data_in->nir;
sparse_data->njc = sparse_data_in->njc;
sparse_data->ndata = sparse_data_in->ndata;
sparse_data->ir = (mat_uint32_t *)malloc(sparse_data->nir * sizeof(*sparse_data->ir));
if ( NULL != sparse_data->ir )
memcpy(sparse_data->ir, sparse_data_in->ir,
sparse_data->nir * sizeof(*sparse_data->ir));
sparse_data->jc = (mat_uint32_t *)malloc(sparse_data->njc * sizeof(*sparse_data->jc));
if ( NULL != sparse_data->jc )
memcpy(sparse_data->jc, sparse_data_in->jc,
sparse_data->njc * sizeof(*sparse_data->jc));
if ( matvar->isComplex ) {
sparse_data->data = malloc(sizeof(mat_complex_split_t));
if ( NULL != sparse_data->data ) {
mat_complex_split_t *complex_data = (mat_complex_split_t *)sparse_data->data;
const mat_complex_split_t *complex_data_in =
(mat_complex_split_t *)sparse_data_in->data;
complex_data->Re = malloc(sparse_data->ndata * data_size);
complex_data->Im = malloc(sparse_data->ndata * data_size);
if ( NULL != complex_data->Re )
memcpy(complex_data->Re, complex_data_in->Re,
sparse_data->ndata * data_size);
if ( NULL != complex_data->Im )
memcpy(complex_data->Im, complex_data_in->Im,
if ( NULL != sparse_data_in->ir ) {
sparse_data->ir =
(mat_uint32_t *)malloc(sparse_data->nir * sizeof(*sparse_data->ir));
if ( NULL != sparse_data->ir )
memcpy(sparse_data->ir, sparse_data_in->ir,
sparse_data->nir * sizeof(*sparse_data->ir));
} else {
sparse_data->ir = NULL;
}
if ( NULL != sparse_data_in->jc ) {
sparse_data->jc =
(mat_uint32_t *)malloc(sparse_data->njc * sizeof(*sparse_data->jc));
if ( NULL != sparse_data->jc )
memcpy(sparse_data->jc, sparse_data_in->jc,
sparse_data->njc * sizeof(*sparse_data->jc));
} else {
sparse_data->jc = NULL;
}
if ( NULL != sparse_data_in->data ) {
if ( matvar->isComplex ) {
sparse_data->data = malloc(sizeof(mat_complex_split_t));
if ( NULL != sparse_data->data ) {
mat_complex_split_t *complex_data =
(mat_complex_split_t *)sparse_data->data;
const mat_complex_split_t *complex_data_in =
(mat_complex_split_t *)sparse_data_in->data;
complex_data->Re = malloc(sparse_data->ndata * data_size);
complex_data->Im = malloc(sparse_data->ndata * data_size);
if ( NULL != complex_data->Re )
memcpy(complex_data->Re, complex_data_in->Re,
sparse_data->ndata * data_size);
if ( NULL != complex_data->Im )
memcpy(complex_data->Im, complex_data_in->Im,
sparse_data->ndata * data_size);
}
} else {
sparse_data->data = malloc(sparse_data->ndata * data_size);
if ( NULL != sparse_data->data )
memcpy(sparse_data->data, sparse_data_in->data,
sparse_data->ndata * data_size);
}
} else {
sparse_data->data = malloc(sparse_data->ndata * data_size);
if ( NULL != sparse_data->data )
memcpy(sparse_data->data, sparse_data_in->data, sparse_data->ndata * data_size);
sparse_data->data = NULL;
}
}
matvar->data = sparse_data;
Expand Down

0 comments on commit 11cc1e4

Please sign in to comment.