1818struct feature_node * * dense_to_sparse (double * x , npy_intp * dims , double bias )
1919{
2020 struct feature_node * * sparse ;
21- register int i , j ; /* number of nonzero elements in row i */
21+ int i , j ; /* number of nonzero elements in row i */
2222 struct feature_node * temp ; /* stack for nonzero elements */
2323 struct feature_node * T ; /* pointer to the top of the stack */
2424 int count ;
2525
26- sparse = (struct feature_node * * ) malloc (dims [0 ] * sizeof (struct feature_node * ));
27- temp = (struct feature_node * ) malloc ((dims [1 ]+ 2 ) * sizeof (struct feature_node ));
26+ sparse = malloc (dims [0 ] * sizeof (struct feature_node * ));
27+ if (sparse == NULL )
28+ goto sparse_error ;
2829
29- if (sparse == NULL || temp == NULL ) return NULL ;
30+ temp = malloc ((dims [1 ]+ 2 ) * sizeof (struct feature_node ));
31+ if (temp == NULL )
32+ goto temp_error ;
3033
3134 for (i = 0 ; i < dims [0 ]; ++ i ) {
3235 T = temp ; /* reset stack pointer */
@@ -53,13 +56,25 @@ struct feature_node **dense_to_sparse (double *x, npy_intp *dims, double bias)
5356
5457 /* allocate memory and copy collected items*/
5558 count = T - temp ;
56- sparse [i ] = (struct feature_node * ) malloc (count * sizeof (struct feature_node ));
57- if (sparse [i ] == NULL ) return NULL ;
59+ sparse [i ] = malloc (count * sizeof (struct feature_node ));
60+ if (sparse [i ] == NULL ) {
61+ int k ;
62+ for (k = 0 ; k < i ; k ++ )
63+ free (sparse [i ]);
64+ goto sparse_i_error ;
65+ }
5866 memcpy (sparse [i ], temp , count * sizeof (struct feature_node ));
5967 }
6068
6169 free (temp );
6270 return sparse ;
71+
72+ sparse_i_error :
73+ free (temp );
74+ temp_error :
75+ free (sparse );
76+ sparse_error :
77+ return NULL ;
6378}
6479
6580
@@ -72,11 +87,22 @@ struct feature_node **csr_to_sparse (double *values, npy_intp *shape_indices,
7287{
7388 struct feature_node * * sparse , * temp ;
7489 int i , j = 0 , k = 0 , n ;
75- sparse = (struct feature_node * * ) malloc ((shape_indptr [0 ]- 1 )* sizeof (struct feature_node * ));
90+
91+ sparse = malloc ((shape_indptr [0 ]- 1 )* sizeof (struct feature_node * ));
92+ if (sparse == NULL )
93+ return NULL ;
7694
7795 for (i = 0 ; i < shape_indptr [0 ]- 1 ; ++ i ) {
7896 n = indptr [i + 1 ] - indptr [i ]; /* count elements in row i */
79- sparse [i ] = (struct feature_node * ) malloc ((n + 2 ) * sizeof (struct feature_node ));
97+
98+ sparse [i ] = malloc ((n + 2 ) * sizeof (struct feature_node ));
99+ if (sparse [i ] == NULL ) {
100+ int l ;
101+ for (l = 0 ; l < i ; l ++ )
102+ free (sparse [i ]);
103+ break ;
104+ }
105+
80106 temp = sparse [i ];
81107 for (j = 0 ; j < n ; ++ j ) {
82108 temp [j ].value = values [k ];
@@ -101,7 +127,7 @@ struct problem * set_problem(char *X,char *Y, npy_intp *dims, double bias)
101127{
102128 struct problem * problem ;
103129 /* not performant but simple */
104- problem = ( struct problem * ) malloc (sizeof (struct problem ));
130+ problem = malloc (sizeof (struct problem ));
105131 if (problem == NULL ) return NULL ;
106132 problem -> l = (int ) dims [0 ];
107133
@@ -127,7 +153,7 @@ struct problem * csr_set_problem (char *values, npy_intp *n_indices,
127153 npy_intp n_features , double bias ) {
128154
129155 struct problem * problem ;
130- problem = ( struct problem * ) malloc (sizeof (struct problem ));
156+ problem = malloc (sizeof (struct problem ));
131157 if (problem == NULL ) return NULL ;
132158 problem -> l = (int ) n_indptr [0 ] - 1 ;
133159
@@ -155,7 +181,7 @@ struct problem * csr_set_problem (char *values, npy_intp *n_indices,
155181struct parameter * set_parameter (int solver_type , double eps , double C , npy_intp nr_weight , char * weight_label , char * weight )
156182{
157183 struct parameter * param ;
158- param = ( struct parameter * ) malloc (sizeof (struct parameter ));
184+ param = malloc (sizeof (struct parameter ));
159185 if (param == NULL ) return NULL ;
160186 param -> solver_type = solver_type ;
161187 param -> eps = eps ;
@@ -174,9 +200,12 @@ struct model * set_model(struct parameter *param, char *coef, npy_intp *dims,
174200 struct model * model ;
175201
176202 if (m == 1 ) m = 2 ; /* liblinear collapses the weight vector in the case of two classes */
177- model = (struct model * ) malloc (sizeof (struct model ));
178- model -> w = (double * ) malloc ( len_w * sizeof (double ));
179- model -> label = (int * ) malloc ( m * sizeof (int ));
203+ if ((model = malloc (sizeof (struct model ))) == NULL )
204+ goto model_error ;
205+ if ((model -> w = malloc ( len_w * sizeof (double ))) == NULL )
206+ goto w_error ;
207+ if ((model -> label = malloc ( m * sizeof (int ))) == NULL )
208+ goto label_error ;
180209
181210 memcpy (model -> label , label , m * sizeof (int ));
182211 memcpy (model -> w , coef , len_w * sizeof (double ));
@@ -188,6 +217,13 @@ struct model * set_model(struct parameter *param, char *coef, npy_intp *dims,
188217 model -> bias = bias ;
189218
190219 return model ;
220+
221+ label_error :
222+ free (model -> w );
223+ w_error :
224+ free (model );
225+ model_error :
226+ return NULL ;
191227}
192228
193229
@@ -218,7 +254,7 @@ int copy_predict(char *train, struct model *model_, npy_intp *train_dims,
218254 char * dec_values )
219255{
220256 int * t = (int * ) dec_values ;
221- register int i , n ;
257+ int i , n ;
222258 struct feature_node * * train_nodes ;
223259 n = train_dims [0 ];
224260 train_nodes = dense_to_sparse ((double * ) train , train_dims , model_ -> bias );
@@ -238,7 +274,8 @@ int copy_predict(char *train, struct model *model_, npy_intp *train_dims,
238274int csr_copy_predict (npy_intp n_features , npy_intp * data_size , char * data ,
239275 npy_intp * index_size ,
240276 char * index , npy_intp * indptr_shape , char * intptr , struct model * model_ ,
241- char * dec_values ) {
277+ char * dec_values )
278+ {
242279 int * t = (int * ) dec_values ;
243280 struct feature_node * * predict_nodes ;
244281 npy_intp i ;
@@ -279,9 +316,8 @@ int csr_copy_predict_values(npy_intp n_features, npy_intp *data_size,
279316 char * data , npy_intp * index_size , char
280317 * index , npy_intp * indptr_shape , char
281318 * intptr , struct model * model_ , char
282- * dec_values , int nr_class ) {
283-
284- int * t = (int * ) dec_values ;
319+ * dec_values , int nr_class )
320+ {
285321 struct feature_node * * predict_nodes ;
286322 npy_intp i ;
287323
@@ -304,7 +340,7 @@ int copy_prob_predict(char *predict, struct model *model_, npy_intp *predict_dim
304340 char * dec_values )
305341{
306342 struct feature_node * * predict_nodes ;
307- register int i ;
343+ int i ;
308344 int n , m ;
309345 n = predict_dims [0 ];
310346 m = model_ -> nr_class ;
@@ -328,8 +364,7 @@ int csr_copy_predict_proba(npy_intp n_features, npy_intp *data_size,
328364 char * dec_values )
329365{
330366 struct feature_node * * predict_nodes ;
331- register int i ;
332- double temp ;
367+ int i ;
333368 double * tx = (double * ) dec_values ;
334369
335370 predict_nodes = csr_to_sparse ((double * ) data , index_size ,
0 commit comments