@@ -97,7 +97,11 @@ hwloc_distrib_root_levels(hwloc_topology_t topology,
9797 const unsigned long flags )
9898{
9999 unsigned i ;
100- struct hwloc_distrib_level * levels = malloc (n_types * sizeof (* levels ));
100+ unsigned arity ;
101+ hwloc_obj_t parent ;
102+ struct hwloc_distrib_level * levels ;
103+
104+ levels = malloc (n_types * sizeof (* levels ));
101105 if (levels == NULL )
102106 return NULL ;
103107
@@ -118,8 +122,6 @@ hwloc_distrib_root_levels(hwloc_topology_t topology,
118122 qsort (levels , n_types , sizeof (* levels ), hwloc_distrib_level_cmp_depth );
119123
120124 // Walk from top to bottom and set arity to the maximum arity below root field.
121- unsigned arity ;
122- hwloc_obj_t parent ;
123125
124126 parent = root ;
125127 for (i = 0 ; i < n_types ; i ++ ){
@@ -229,7 +231,9 @@ hwloc_distrib_iterator_scatter(hwloc_topology_t topology,
229231
230232 unsigned i = 0 , n = 0 ;
231233 hwloc_obj_t obj , root = hwloc_get_obj_by_depth (topology , 0 , 0 );
232-
234+ hwloc_obj_type_t * levels ;
235+ struct hwloc_distrib_iterator * it ;
236+
233237 // Count depths with a non empty cpuset.
234238 obj = root ;
235239 while (obj ){
@@ -240,9 +244,8 @@ hwloc_distrib_iterator_scatter(hwloc_topology_t topology,
240244 obj = obj -> first_child ;
241245 }
242246
243- hwloc_obj_type_t levels [n ];
244-
245247 // fill levels array.
248+ levels = malloc (sizeof (* levels ) * n );
246249 obj = root ;
247250 while (obj ){
248251 if ( obj -> cpuset != NULL && !hwloc_bitmap_iszero (obj -> cpuset ) && hwloc_get_type_depth (topology , obj -> type ) >= 0 ){
@@ -254,10 +257,10 @@ hwloc_distrib_iterator_scatter(hwloc_topology_t topology,
254257 obj = obj -> first_child ;
255258 }
256259
257- struct hwloc_distrib_iterator * it = malloc (sizeof (* it ) + sizeof (hwloc_obj_t ) + sizeof (struct hwloc_distrib_level * ));
260+ it = malloc (sizeof (* it ) + sizeof (hwloc_obj_t ) + sizeof (struct hwloc_distrib_level * ));
258261
259262 if (it == NULL )
260- return NULL ;
263+ goto failure ;
261264
262265 it -> roots = (hwloc_obj_t * ) ((char * )it + sizeof (* it ));
263266 * it -> roots = root ;
@@ -268,11 +271,17 @@ hwloc_distrib_iterator_scatter(hwloc_topology_t topology,
268271
269272 * it -> levels = hwloc_distrib_root_levels (topology , root , levels , n , flags );
270273
271- if (* it -> levels == NULL ){
272- free ( it ) ;
273- return NULL ;
274- }
274+ if (* it -> levels == NULL )
275+ goto failure_with_it ;
276+
277+ free ( levels );
275278 return it ;
279+
280+ failure_with_it :
281+ free (it );
282+ failure :
283+ free (levels );
284+ return NULL ;
276285}
277286
278287void hwloc_distrib_destroy_iterator (struct hwloc_distrib_iterator * it ){
0 commit comments