@@ -37,25 +37,43 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
3737
3838      bsp_type_t  index_type  =  bsp_pick_integer_type (max_dim );
3939
40-       result .values  =  bsp_copy_construct_array_t (matrix .values );
40+       bsp_error_t  error  = 
41+           bsp_copy_construct_array_t (& result .values , matrix .values );
42+       if  (error  !=  BSP_SUCCESS ) {
43+         return  bsp_construct_default_matrix_t ();
44+       }
4145
4246      // There is a corner case with tall and skinny matrices where we need a 
4347      // higher width for rowind.  In order to keep rowind/colind the same type, 
4448      // we might upcast. 
4549
4650      if  (index_type  ==  matrix .indices_1 .type ) {
47-         result .indices_1  =  bsp_copy_construct_array_t (matrix .indices_1 );
51+         error  =  bsp_copy_construct_array_t (& result .indices_1 , matrix .indices_1 );
52+         if  (error  !=  BSP_SUCCESS ) {
53+           bsp_destroy_array_t (& result .values );
54+           return  bsp_construct_default_matrix_t ();
55+         }
4856      } else  {
49-         result .indices_1  = 
50-             bsp_construct_array_t (matrix .indices_1 .size , index_type );
57+         error  =  bsp_construct_array_t (& result .indices_1 , matrix .indices_1 .size ,
58+                                       index_type );
59+         if  (error  !=  BSP_SUCCESS ) {
60+           bsp_destroy_array_t (& result .values );
61+           return  bsp_construct_default_matrix_t ();
62+         }
63+ 
5164        for  (size_t  i  =  0 ; i  <  matrix .indices_1 .size ; i ++ ) {
5265          size_t  index ;
5366          bsp_array_read (matrix .indices_1 , i , index );
5467          bsp_array_write (result .indices_1 , i , index );
5568        }
5669      }
5770
58-       result .indices_0  =  bsp_construct_array_t (matrix .nnz , index_type );
71+       error  =  bsp_construct_array_t (& result .indices_0 , matrix .nnz , index_type );
72+       if  (error  !=  BSP_SUCCESS ) {
73+         bsp_destroy_array_t (& result .values );
74+         bsp_destroy_array_t (& result .indices_1 );
75+         return  bsp_construct_default_matrix_t ();
76+       }
5977
6078      for  (size_t  i  =  0 ; i  <  matrix .nrows ; i ++ ) {
6179        size_t  row_begin , row_end ;
@@ -109,12 +127,26 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
109127        // indices can be copied exactly.  Values' type will not change, but 
110128        // column indices might, thus the extra branch. 
111129
112-         result .values  =  bsp_copy_construct_array_t (matrix .values );
130+         bsp_error_t  error  = 
131+             bsp_copy_construct_array_t (& result .values , matrix .values );
132+         if  (error  !=  BSP_SUCCESS ) {
133+           return  bsp_construct_default_matrix_t ();
134+         }
113135
114136        if  (index_type  ==  matrix .indices_1 .type ) {
115-           result .indices_1  =  bsp_copy_construct_array_t (matrix .indices_1 );
137+           error  = 
138+               bsp_copy_construct_array_t (& result .indices_1 , matrix .indices_1 );
139+           if  (error  !=  BSP_SUCCESS ) {
140+             bsp_destroy_array_t (& result .values );
141+             return  bsp_construct_default_matrix_t ();
142+           }
116143        } else  {
117-           result .indices_1  =  bsp_construct_array_t (matrix .nnz , index_type );
144+           error  = 
145+               bsp_construct_array_t (& result .indices_1 , matrix .nnz , index_type );
146+           if  (error  !=  BSP_SUCCESS ) {
147+             bsp_destroy_array_t (& result .values );
148+             return  bsp_construct_default_matrix_t ();
149+           }
118150
119151          for  (size_t  i  =  0 ; i  <  matrix .nnz ; i ++ ) {
120152            size_t  index ;
@@ -123,8 +155,13 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
123155          }
124156        }
125157
126-         result .pointers_to_1  = 
127-             bsp_construct_array_t (matrix .nrows  +  1 , index_type );
158+         error  =  bsp_construct_array_t (& result .pointers_to_1 , matrix .nrows  +  1 ,
159+                                       index_type );
160+         if  (error  !=  BSP_SUCCESS ) {
161+           bsp_destroy_array_t (& result .values );
162+           bsp_destroy_array_t (& result .indices_1 );
163+           return  bsp_construct_default_matrix_t ();
164+         }
128165
129166        bsp_array_t  rowptr  =  result .pointers_to_1 ;
130167
0 commit comments