Skip to content

Commit 6673df1

Browse files
authored
Merge pull request #11224 from devreal/han_dynamic_rules_free
coll/han: reorder free calls and avoid read-after-free in debug builds
2 parents bc47684 + 0bb10c4 commit 6673df1

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

ompi/mca/coll/han/coll_han_dynamic_file.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
22
/*
3-
* Copyright (c) 2018-2020 The University of Tennessee and The University
3+
* Copyright (c) 2018-2022 The University of Tennessee and The University
44
* of Tennessee Research Foundation. All rights
55
* reserved.
66
* Copyright (c) 2022 IBM Corporation. All rights reserved
@@ -65,6 +65,7 @@ mca_coll_han_init_dynamic_rules(void)
6565
int algorithm_id;
6666
char * coll_name = NULL;
6767
char * algorithm_name = NULL;
68+
char * target_comp_name = NULL;
6869
collective_rule_t *coll_rules;
6970

7071
/* Topo information */
@@ -135,6 +136,7 @@ mca_coll_han_init_dynamic_rules(void)
135136
mca_coll_han_component.dynamic_rules.nb_collectives = i+1;
136137

137138
/* Get the collective identifier */
139+
free(coll_name);
138140
if( getnext_string(fptr, &coll_name) < 0 ) {
139141
opal_output_verbose(5, mca_coll_han_component.han_output,
140142
"coll:han:mca_coll_han_init_dynamic_rules invalid collective at line %d."
@@ -155,9 +157,7 @@ mca_coll_han_init_dynamic_rules(void)
155157
coll_name, fileline, ALLGATHER, COLLCOUNT);
156158
goto file_reading_error;
157159
}
158-
if( NULL != coll_name ) {
159-
free(coll_name);
160-
}
160+
free(coll_name);
161161
coll_name = strdup(mca_coll_base_colltype_to_str(coll_id));
162162
}
163163

@@ -321,7 +321,6 @@ mca_coll_han_init_dynamic_rules(void)
321321

322322
/* Iterate on message size rules */
323323
for( l = 0; l < nb_msg_size; l++ ) {
324-
char* target_comp_name = NULL;
325324
conf_rules[k].nb_msg_size = l+1;
326325

327326
/* Get the message size */
@@ -338,6 +337,7 @@ mca_coll_han_init_dynamic_rules(void)
338337
}
339338

340339
/* Get the component identifier for this message size rule */
340+
free(target_comp_name);
341341
if( getnext_string(fptr, &target_comp_name) < 0 ) {
342342
opal_output_verbose(5, mca_coll_han_component.han_output,
343343
"coll:han:mca_coll_han_init_dynamic_rules found an error on dynamic rules file %s "
@@ -353,38 +353,32 @@ mca_coll_han_init_dynamic_rules(void)
353353
"reader encountered an unexpected EOF. Collective component id must be at "
354354
"least %d and less than %d\n",
355355
fname, fileline, target_comp_name, SELF, COMPONENTS_COUNT);
356-
free(target_comp_name);
357-
target_comp_name = NULL;
358356
goto file_reading_error;
359357
}
360358

361359
/* Get the optionnal algorithm for han */
362360
algorithm_id = 0; // default for all collectives
363361
if ((component == HAN) && (1 == ompi_coll_base_file_peek_next_char_is(fptr, &fileline, '@')) ) {
364362

363+
free(algorithm_name);
364+
algorithm_name = NULL;
365365
if( getnext_string(fptr, &algorithm_name) < 0 ) {
366366
opal_output_verbose(5, mca_coll_han_component.han_output,
367367
"coll:han:mca_coll_han_init_dynamic_rules found an error on dynamic rules file %s "
368368
"at line %d: cannot read the name/id of an algorithm\n",
369369
fname, fileline);
370-
free(target_comp_name);
371-
target_comp_name = NULL;
372370
goto file_reading_error;
373371
}
374372
algorithm_id = mca_coll_han_algorithm_name_to_id(coll_id, algorithm_name);
375373
if (algorithm_id < 0) {
376374
char *endp;
377375
algorithm_id = (int)strtol(algorithm_name, &endp, 10);
378376
char endc = *endp;
379-
free(algorithm_name);
380-
algorithm_name = NULL;
381377
if (('\0' != endc ) || !mca_coll_han_algorithm_id_is_valid(coll_id, algorithm_id)) {
382378
opal_output_verbose(5, mca_coll_han_component.han_output,
383379
"coll:han:mca_coll_han_init_dynamic_rules found an error on dynamic rules file %s "
384380
"at line %d: unknown algorithm '%s' for %s\n",
385381
fname, fileline, algorithm_name, coll_name);
386-
free(target_comp_name);
387-
target_comp_name = NULL;
388382
goto file_reading_error;
389383
}
390384
}
@@ -422,19 +416,13 @@ mca_coll_han_init_dynamic_rules(void)
422416
"file %s line %d found end of file while reading the optional list "
423417
"of segment lengths for collective %s component %s\n",
424418
fname, fileline, coll_name, target_comp_name);
425-
free(target_comp_name);
426419
goto file_reading_error;
427420
}
428421
}
429422
}
430-
free(target_comp_name);
431423
}
432424
}
433425
}
434-
if( NULL != coll_name ) {
435-
free(coll_name);
436-
coll_name = NULL;
437-
}
438426
}
439427

440428
if( getnext_long(fptr, &nb_coll) > 0 ) {
@@ -455,7 +443,9 @@ mca_coll_han_init_dynamic_rules(void)
455443
fclose(fptr);
456444

457445
check_dynamic_rules();
446+
free(coll_name);
458447
free(algorithm_name);
448+
free(target_comp_name);
459449
return OMPI_SUCCESS;
460450

461451
cannot_allocate:
@@ -465,10 +455,9 @@ mca_coll_han_init_dynamic_rules(void)
465455
opal_output_verbose(0, mca_coll_han_component.han_output,
466456
"coll:han:mca_coll_han_init_dynamic_rules "
467457
"cannot allocate dynamic rules\n");
468-
if( NULL != coll_name ) {
469-
free(coll_name);
470-
}
458+
free(coll_name);
471459
free(algorithm_name);
460+
free(target_comp_name);
472461
fclose (fptr);
473462
/* We disable the module, we don't need to keep the rules */
474463
mca_coll_han_free_dynamic_rules();
@@ -481,10 +470,9 @@ mca_coll_han_init_dynamic_rules(void)
481470
"Will use mca parameters defined rules. "
482471
"To see error detail, please set "
483472
"collective verbosity level over 5\n");
484-
if( NULL != coll_name ) {
485-
free(coll_name);
486-
}
473+
free(coll_name);
487474
free(algorithm_name);
475+
free(target_comp_name);
488476
fclose (fptr);
489477
/* We disable the module, we don't need to keep the rules */
490478
mca_coll_han_free_dynamic_rules();

0 commit comments

Comments
 (0)