Skip to content

Commit 176278f

Browse files
authored
Merge pull request #13187 from lrbison/coverity
Coverity Fixes for coll/tuned and coll/han/alltoallv
2 parents 01da1c4 + e7337c9 commit 176278f

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

ompi/mca/coll/han/coll_han_alltoallv.c

+11-7
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ int mca_coll_han_alltoallv_using_smsc(
804804
low_gather_out = malloc(sizeof(*low_gather_out) * low_size);
805805
struct peer_data *peers = malloc(sizeof(*peers) * low_size);
806806
opal_datatype_t *peer_send_types = malloc(sizeof(*peer_send_types) * low_size);
807+
bool have_bufs_and_types = false;
807808

808809
low_gather_in.serialization_buffer = serialization_buf;
809810
low_gather_in.sbuf = (void*)sbuf; // cast to discard the const
@@ -896,6 +897,7 @@ int mca_coll_han_alltoallv_using_smsc(
896897
peers[jrank].sendtype = &peer_send_types[jrank];
897898
}
898899

900+
have_bufs_and_types = true;
899901
send_from_addrs = malloc(sizeof(*send_from_addrs)*low_size);
900902
recv_to_addrs = malloc(sizeof(*recv_to_addrs)*low_size);
901903
send_counts = malloc(sizeof(*send_counts)*low_size);
@@ -964,14 +966,16 @@ int mca_coll_han_alltoallv_using_smsc(
964966
free(recv_types);
965967
}
966968

967-
for (int jlow=0; jlow<low_size; jlow++) {
968-
if (jlow != low_rank) {
969-
OBJ_DESTRUCT(&peer_send_types[jlow]);
970-
}
969+
if (have_bufs_and_types) {
970+
for (int jlow=0; jlow<low_size; jlow++) {
971+
if (jlow != low_rank) {
972+
OBJ_DESTRUCT(&peer_send_types[jlow]);
973+
}
971974

972-
for (int jbuf=0; jbuf<2; jbuf++) {
973-
if (peers[jlow].map_ctx[jbuf]) {
974-
mca_smsc->unmap_peer_region(peers[jlow].map_ctx[jbuf]);
975+
for (int jbuf=0; jbuf<2; jbuf++) {
976+
if (peers[jlow].map_ctx[jbuf]) {
977+
mca_smsc->unmap_peer_region(peers[jlow].map_ctx[jbuf]);
978+
}
975979
}
976980
}
977981
}

ompi/mca/coll/tuned/coll_tuned_component.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ mca_coll_tuned_module_construct(mca_coll_tuned_module_t *module)
304304

305305
int coll_tuned_alg_from_str(int collective_id, const char *alg_name, int *alg_value) {
306306
int rc;
307-
if (collective_id > COLLCOUNT || collective_id < 0) { return OPAL_ERROR; };
307+
if (collective_id >= COLLCOUNT || collective_id < 0) { return OPAL_ERROR; };
308308
rc = coll_tuned_algorithm_enums[collective_id]->value_from_string(
309309
coll_tuned_algorithm_enums[collective_id],
310310
alg_name, alg_value );
@@ -314,7 +314,7 @@ int coll_tuned_alg_from_str(int collective_id, const char *alg_name, int *alg_va
314314
/* return the enum's value and string. caller's responsibility to free alg_string if NULL was not provided. */
315315
int coll_tuned_alg_to_str(int collective_id, int alg_value, char **alg_string) {
316316
int rc;
317-
if (collective_id > COLLCOUNT || collective_id < 0) { return OPAL_ERROR; };
317+
if (collective_id >= COLLCOUNT || collective_id < 0) { return OPAL_ERROR; };
318318
rc = coll_tuned_algorithm_enums[collective_id]->string_from_value(
319319
coll_tuned_algorithm_enums[collective_id],
320320
alg_value, alg_string );
@@ -326,7 +326,7 @@ int coll_tuned_alg_register_options(int collective_id, mca_base_var_enum_t *opti
326326
/* use the same enum used for mca parameters to allow tuning files to use
327327
algorithm names rather than just numbers.*/
328328
if (!options) { return OPAL_ERROR; }
329-
if (collective_id > COLLCOUNT || collective_id < 0) {
329+
if (collective_id >= COLLCOUNT || collective_id < 0) {
330330
return OPAL_ERROR;
331331
}
332332

ompi/mca/coll/tuned/coll_tuned_dynamic_file.c

+17-12
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,11 @@ static int ompi_coll_tuned_read_rules_json (const opal_json_t *json_root, ompi_c
244244

245245
const opal_json_t *collectives_obj = NULL;
246246
const opal_json_t *comm_rule_array = NULL;
247+
const opal_json_t *comm_rule = NULL;
248+
const opal_json_t *msg_size_array = NULL;
247249

248250
size_t num_collectives = 0;
249-
size_t num_comm_rules;
251+
size_t num_comm_rules = 0;
250252
rc = opal_json_get_key(json_root, "collectives", &collectives_obj);
251253
if (rc != OPAL_SUCCESS) {
252254
opal_output_verbose(1, ompi_coll_tuned_stream,
@@ -269,15 +271,14 @@ static int ompi_coll_tuned_read_rules_json (const opal_json_t *json_root, ompi_c
269271
opal_output_verbose(1, ompi_coll_tuned_stream,
270272
"Internal json error when attempting to parse the collective at index %ld\n",
271273
jcol);
272-
goto error_bad_coll;
274+
goto error_cleanup;
273275
}
274276
coll_id = mca_coll_base_name_to_colltype(coll_name);
275277
if (coll_id < 0) {
276278
opal_output_verbose(1, ompi_coll_tuned_stream,
277279
"Unrecognized collective: \"%s\". Use all lowercase such as \"allgather\"",
278280
coll_name);
279-
opal_json_free(&comm_rule_array);
280-
goto error_bad_coll;
281+
goto error_cleanup;
281282
}
282283

283284
alg_p = &alg_rules[coll_id];
@@ -287,14 +288,12 @@ static int ompi_coll_tuned_read_rules_json (const opal_json_t *json_root, ompi_c
287288
opal_output_verbose(1, ompi_coll_tuned_stream,
288289
"Problem parsing the collective at index %ld (for %s). Expected an array of comm-related rules.",
289290
jcol, mca_coll_base_colltype_to_str(coll_id) );
290-
goto error_bad_coll;
291+
goto error_cleanup;
291292
}
292293
alg_p->n_com_sizes = (int)num_comm_rules;
293294
alg_p->com_rules = ompi_coll_tuned_mk_com_rules (num_comm_rules, coll_id);
294295

295296
for (jcomm_rule=0; jcomm_rule < num_comm_rules; jcomm_rule++) {
296-
const opal_json_t *comm_rule;
297-
const opal_json_t *msg_size_array;
298297
size_t num_msg_rules;
299298
rc = opal_json_get_index(comm_rule_array, jcomm_rule, &comm_rule);
300299
com_p = &(alg_p->com_rules[jcomm_rule]);
@@ -353,15 +352,16 @@ static int ompi_coll_tuned_read_rules_json (const opal_json_t *json_root, ompi_c
353352
"Problem occurred within collective %s, "
354353
"comm_size_min=%d, comm_size_max=%d (entry number %ld in the comm_size array), "
355354
"message size entry number %ld.", coll_name, com_p->mpi_comsize_min, com_p->mpi_comsize_max, 1+jcomm_rule, 1+jmsg_rule);
356-
opal_json_free(&collectives_obj);
357-
return OMPI_ERROR;
355+
goto error_cleanup;
358356
error_bad_comm_rule:
359357
opal_output_verbose(1, ompi_coll_tuned_stream,
360358
"Problem occurred within collective %s, "
361359
"in entry number %ld of the comm_size array", coll_name, 1+jcomm_rule);
362-
opal_json_free(&collectives_obj);
363-
return OMPI_ERROR;
364-
error_bad_coll:
360+
goto error_cleanup;
361+
error_cleanup:
362+
opal_json_free(&msg_size_array);
363+
opal_json_free(&comm_rule);
364+
opal_json_free(&comm_rule_array);
365365
opal_json_free(&collectives_obj);
366366
return OMPI_ERROR;
367367
}
@@ -494,6 +494,11 @@ static int ompi_coll_tuned_read_rules_config_file_classic (char *fname, ompi_col
494494
}
495495
opal_output_verbose(25, ompi_coll_tuned_stream,
496496
"Read communicator count %ld for dynamic rule for collective ID %ld\n", NCOMSIZES, COLID);
497+
if( NCOMSIZES > INT_MAX) {
498+
opal_output_verbose(25, ompi_coll_tuned_stream,
499+
"Refusing to proceed: suspiciously large number found for the number of communicator-based rules: %ld\n", NCOMSIZES);
500+
goto on_file_error;
501+
}
497502
alg_p->n_com_sizes = NCOMSIZES;
498503
alg_p->com_rules = ompi_coll_tuned_mk_com_rules (NCOMSIZES, COLID);
499504
if (NULL == alg_p->com_rules) {

opal/util/json/opal_json.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ int opal_json_get_key_by_index(const opal_json_t *json, const size_t index, cons
203203

204204
json_object_entry entry = {0};
205205

206-
if (index < 0 || index >= in->value->u.object.length) {
206+
if (index >= in->value->u.object.length) {
207207
opal_show_help("help-json.txt", "Index out of bound", true, index,
208208
in->value->u.array.length);
209209
return ret;

0 commit comments

Comments
 (0)