@@ -111,13 +111,14 @@ ladel_sparse_matrix *ladel_sparse_alloc(ladel_int nrow, ladel_int ncol,
111111 M -> nrow = nrow ;
112112 M -> ncol = ncol ;
113113 M -> nzmax = nzmax ;
114+ nzmax = LADEL_MAX (nzmax , 1 );
114115 M -> values = values ;
115116 M -> symmetry = symmetry ;
116117 if (M -> nzmax ) M -> p = (ladel_int * ) ladel_malloc (ncol + 1 , sizeof (ladel_int ));
117118 else M -> p = (ladel_int * ) ladel_calloc (ncol + 1 , sizeof (ladel_int )); //Initialize col counts to zero for empty matrix
118119 M -> i = (ladel_int * ) ladel_malloc (nzmax , sizeof (ladel_int ));
119120 M -> x = values ? (ladel_double * ) ladel_malloc (nzmax , sizeof (ladel_double )) : NULL ;
120- M -> nz = nz ? (ladel_int * ) ladel_malloc (ncol , sizeof (ladel_int )) : NULL ;
121+ M -> nz = ( nz && ncol ) ? (ladel_int * ) ladel_malloc (ncol , sizeof (ladel_int )) : NULL ;
121122 if (!M -> p || !M -> i || (values && !M -> x ) || (nz && !M -> nz )) M = ladel_sparse_free (M );
122123 return M ;
123124}
@@ -136,7 +137,7 @@ ladel_sparse_matrix *ladel_sparse_alloc_empty(ladel_int nrow, ladel_int ncol,
136137 M -> p = (ladel_int * ) ladel_calloc (ncol + 1 , sizeof (ladel_int ));
137138 M -> i = (ladel_int * ) ladel_malloc (1 , sizeof (ladel_int ));
138139 M -> x = values ? (ladel_double * ) ladel_malloc (1 , sizeof (ladel_double )) : NULL ;
139- M -> nz = nz ? (ladel_int * ) ladel_malloc (ncol , sizeof (ladel_int )) : NULL ;
140+ M -> nz = ( nz && ncol ) ? (ladel_int * ) ladel_malloc (ncol , sizeof (ladel_int )) : NULL ;
140141 if (!M -> p || !M -> i || (values && !M -> x ) || (nz && !M -> nz )) M = ladel_sparse_free (M );
141142 return M ;
142143}
0 commit comments