Skip to content

Commit 3a2e908

Browse files
authored
Merge pull request #13468 from devreal/sprintf-is-deprecated
Replace sprintf with snprintf
2 parents fa0cc00 + 588ae90 commit 3a2e908

File tree

22 files changed

+83
-80
lines changed

22 files changed

+83
-80
lines changed

ompi/communicator/comm_cid.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ static int ompi_comm_ext_cid_new_block (ompi_communicator_t *newcomm, ompi_commu
417417
char msg_string[1024];
418418
switch (rc) {
419419
case PMIX_ERR_UNREACH:
420-
sprintf(msg_string,"PMIx server unreachable");
420+
snprintf(msg_string, sizeof(msg_string), "PMIx server unreachable");
421421
opal_show_help("help-comm.txt",
422422
"MPI function not supported",
423423
true,
@@ -427,7 +427,7 @@ static int ompi_comm_ext_cid_new_block (ompi_communicator_t *newcomm, ompi_commu
427427
rc = MPI_ERR_UNSUPPORTED_OPERATION;
428428
break;
429429
case PMIX_ERR_NOT_SUPPORTED:
430-
sprintf(msg_string,"PMIx server does not support PMIx Group operations");
430+
snprintf(msg_string, sizeof(msg_string), "PMIx server does not support PMIx Group operations");
431431
opal_show_help("help-comm.txt",
432432
"MPI function not supported",
433433
true,
@@ -577,7 +577,7 @@ int ompi_comm_nextcid_nb (ompi_communicator_t *newcomm, ompi_communicator_t *com
577577
functions but the pml does not support these functions so return not supported */
578578
if (NULL == comm) {
579579
char msg_string[1024];
580-
sprintf(msg_string,"The PML being used - %s - does not support MPI sessions related features",
580+
snprintf(msg_string, sizeof(msg_string), "The PML being used - %s - does not support MPI sessions related features",
581581
mca_pml_base_selected_component.pmlm_version.mca_component_name);
582582
opal_show_help("help-comm.txt",
583583
"MPI function not supported",
@@ -1066,7 +1066,7 @@ int ompi_comm_get_remote_cid_from_pmix (ompi_communicator_t *comm, int dest, uin
10661066
pmix_value_t *val = NULL;
10671067
ompi_comm_extended_cid_t excid;
10681068
int rc = OMPI_SUCCESS;
1069-
size_t remote_cid64;
1069+
size_t remote_cid64 = 0;
10701070

10711071
assert(NULL != remote_cid);
10721072

@@ -1082,7 +1082,7 @@ int ompi_comm_get_remote_cid_from_pmix (ompi_communicator_t *comm, int dest, uin
10821082
PMIX_INFO_LOAD(&tinfo[1], PMIX_GROUP_CONTEXT_ID, &excid.cid_base, PMIX_SIZE);
10831083
PMIX_INFO_SET_QUALIFIER(&tinfo[1]);
10841084
if (PMIX_SUCCESS != (rc = PMIx_Get(&pmix_proc, PMIX_GROUP_LOCAL_CID, tinfo, 2, &val))) {
1085-
OPAL_OUTPUT_VERBOSE((10, ompi_comm_output, "PMIx_Get failed for PMIX_GROUP_LOCAL_CID cid_base %ld %s", excid.cid_base, PMIx_Error_string(rc)));
1085+
OPAL_OUTPUT_VERBOSE((10, ompi_comm_output, "PMIx_Get failed for PMIX_GROUP_LOCAL_CID cid_base %"PRIu64" %s", excid.cid_base, PMIx_Error_string(rc)));
10861086
rc = OMPI_ERR_NOT_FOUND;
10871087
goto done;
10881088
}
@@ -1103,7 +1103,7 @@ int ompi_comm_get_remote_cid_from_pmix (ompi_communicator_t *comm, int dest, uin
11031103
rc = OMPI_SUCCESS;
11041104
*remote_cid = (uint32_t)remote_cid64;
11051105
comm->c_index_vec[dest] = (uint32_t)remote_cid64;
1106-
OPAL_OUTPUT_VERBOSE((10, ompi_comm_output, "PMIx_Get PMIX_GROUP_LOCAL_CID %d for cid_base %ld", *remote_cid, excid.cid_base));
1106+
OPAL_OUTPUT_VERBOSE((10, ompi_comm_output, "PMIx_Get PMIX_GROUP_LOCAL_CID %d for cid_base %"PRIu64, *remote_cid, excid.cid_base));
11071107

11081108
done:
11091109
if (NULL != val) {

ompi/debuggers/ompi_common_dll_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ typedef struct
247247
int MPI_TAG;
248248
int MPI_ERROR;
249249
int _cancelled;
250-
size_t _ucount;
250+
int _ucount;
251251
} offset;
252252
} ompi_status_public_t;
253253
/* datatype structure */

ompi/instance/instance.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ static int ompi_instance_group_pmix_pset (ompi_instance_t *instance, const char
12921292
ret = MPI_ERR_ARG; /* pset_name not valid */
12931293
break;
12941294
case PMIX_ERR_UNREACH:
1295-
sprintf(msg_string,"PMIx server unreachable");
1295+
snprintf(msg_string, sizeof(msg_string), "PMIx server unreachable");
12961296
opal_show_help("help-comm.txt",
12971297
"MPI function not supported",
12981298
true,
@@ -1301,7 +1301,7 @@ static int ompi_instance_group_pmix_pset (ompi_instance_t *instance, const char
13011301
ret = MPI_ERR_UNSUPPORTED_OPERATION;
13021302
break;
13031303
case PMIX_ERR_NOT_SUPPORTED:
1304-
sprintf(msg_string,"PMIx server does not support PMIX_QUERY_PSET_MEMBERSHIP operation");
1304+
snprintf(msg_string, sizeof(msg_string), "PMIx server does not support PMIX_QUERY_PSET_MEMBERSHIP operation");
13051305
opal_show_help("help-comm.txt",
13061306
"MPI function not supported",
13071307
true,

ompi/mca/coll/acoll/coll_acoll_barrier.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ int mca_coll_acoll_barrier_shm_h(struct ompi_communicator_t *comm, mca_coll_base
119119
int root = 0;
120120
int rank = ompi_comm_rank(comm);
121121
int size = ompi_comm_size(comm);
122-
mca_coll_acoll_module_t *acoll_module = (mca_coll_acoll_module_t *) module;
123122
coll_acoll_init(module, comm, subc->data, subc, root);
124123
coll_acoll_data_t *data = subc->data;
125124

@@ -227,7 +226,6 @@ int mca_coll_acoll_barrier_shm_f(struct ompi_communicator_t *comm, mca_coll_base
227226
int root = 0;
228227
int rank = ompi_comm_rank(comm);
229228
int size = ompi_comm_size(comm);
230-
mca_coll_acoll_module_t *acoll_module = (mca_coll_acoll_module_t *) module;
231229

232230
coll_acoll_init(module, comm, subc->data, subc, root);
233231
coll_acoll_data_t *data = subc->data;

ompi/mca/coll/ftagree/coll_ftagree_earlyreturning.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static void era_debug_print_group(int lvl, ompi_group_t *group, ompi_communicato
497497
}
498498
s = 128 + n * 16;
499499
str = (char*)malloc(s);
500-
sprintf(str, "Group of size %d. Ranks in %d.%d: (", n, comm->c_index, comm->c_epoch);
500+
snprintf(str, s, "Group of size %d. Ranks in %d.%d: (", n, comm->c_index, comm->c_epoch);
501501
p = strlen(str);
502502
for(i = 0; i < n; i++) {
503503
snprintf(str + p, s - p, "%d%s", gra[i], i==n-1 ? "" : ", ");
@@ -2285,7 +2285,7 @@ static void send_msg(ompi_communicator_t *comm,
22852285
b++;
22862286
} while(w < 256);
22872287
if( strlen(strbytes) >= 252 ) {
2288-
sprintf(strbytes + 252, "...");
2288+
snprintf(strbytes + 252, 256 - 252, "...");
22892289
}
22902290

22912291
OPAL_OUTPUT_VERBOSE((30, ompi_ftmpi_output_handle,

ompi/mca/coll/ucc/coll_ucc_module.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,22 +312,22 @@ static int mca_coll_ucc_init_ctx(ompi_communicator_t* comm)
312312
goto cleanup_lib;
313313
}
314314

315-
sprintf(str_buf, "%u", ompi_proc_world_size());
315+
snprintf(str_buf, sizeof(str_buf), "%u", ompi_proc_world_size());
316316
if (UCC_OK != ucc_context_config_modify(ctx_config, NULL, "ESTIMATED_NUM_EPS",
317317
str_buf)) {
318318
UCC_ERROR("UCC context config modify failed for estimated_num_eps");
319319
goto cleanup_lib;
320320
}
321321

322-
sprintf(str_buf, "%u", opal_process_info.num_local_peers + 1);
322+
snprintf(str_buf, sizeof(str_buf), "%u", opal_process_info.num_local_peers + 1);
323323
if (UCC_OK != ucc_context_config_modify(ctx_config, NULL, "ESTIMATED_NUM_PPN",
324324
str_buf)) {
325325
UCC_ERROR("UCC context config modify failed for estimated_num_eps");
326326
goto cleanup_lib;
327327
}
328328

329329
if (ucc_api_major > 1 || (ucc_api_major == 1 && ucc_api_minor >= 6)) {
330-
sprintf(str_buf, "%u", opal_process_info.my_local_rank);
330+
snprintf(str_buf, sizeof(str_buf), "%u", opal_process_info.my_local_rank);
331331
if (UCC_OK != ucc_context_config_modify(ctx_config, NULL, "NODE_LOCAL_ID",
332332
str_buf)) {
333333
UCC_ERROR("UCC context config modify failed for node_local_id");

ompi/mca/common/monitoring/common_monitoring_coll.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ static inline void mca_common_monitoring_coll_cache(mca_monitoring_coll_data_t*d
7070
assert( 0 < size );
7171
/* Allocate enough space for list (add 1 to keep the final '\0' if already exact size) */
7272
max_length = snprintf(NULL, 0, "%d,", world_size - 1) + 1;
73-
tmp_procs = malloc((1 + max_length * size) * sizeof(char));
73+
int bufsize = (1 + max_length * size) * sizeof(char);
74+
tmp_procs = malloc(bufsize);
7475
if( NULL == tmp_procs ) {
7576
OPAL_MONITORING_PRINT_ERR("Cannot allocate memory for caching proc list.");
7677
} else {
7778
tmp_procs[0] = '\0';
7879
/* Build procs list */
7980
for(i = 0; i < size; ++i) {
8081
if( OPAL_SUCCESS == mca_common_monitoring_get_world_rank(i, data->p_comm->c_remote_group, &world_rank) )
81-
pos += sprintf(&tmp_procs[pos], "%d,", world_rank);
82+
pos += snprintf(&tmp_procs[pos], bufsize - pos, "%d,", world_rank);
8283
}
8384
tmp_procs[pos - 1] = '\0'; /* Remove final coma */
8485
data->procs = realloc(tmp_procs, pos * sizeof(char)); /* Adjust to size required */

ompi/mca/hook/comm_method/hook_comm_method_fns.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ abbreviate_list_into_string(char *str, int max, int *list, int nlist)
325325
strcpy(&str[strlen(str)], ", ");
326326
}
327327
if (lo != hi) {
328-
sprintf(&str[strlen(str)], "%d - %d", lo, hi);
328+
snprintf(&str[strlen(str)], max - strlen(str), "%d - %d", lo, hi);
329329
} else {
330-
sprintf(&str[strlen(str)], "%d", lo);
330+
snprintf(&str[strlen(str)], max - strlen(str), "%d", lo);
331331
}
332332
}
333333
/*
@@ -352,9 +352,9 @@ abbreviate_list_into_string(char *str, int max, int *list, int nlist)
352352
strcpy(&str[strlen(str)], ", ");
353353
}
354354
if (lo != hi) {
355-
sprintf(&str[strlen(str)], "%d - %d", lo, hi);
355+
snprintf(&str[strlen(str)], max - strlen(str), "%d - %d", lo, hi);
356356
} else {
357-
sprintf(&str[strlen(str)], "%d", lo);
357+
snprintf(&str[strlen(str)], max - strlen(str), "%d", lo);
358358
}
359359
}
360360
}
@@ -460,7 +460,7 @@ ompi_report_comm_methods(int called_from_location)
460460

461461
len = strlen(opal_process_info.nodename) + 100;
462462
hoststring = malloc(len + 1);
463-
sprintf(hoststring, "Host %d [%s] ranks ",
463+
snprintf(hoststring, len + 1, "Host %d [%s] ranks ",
464464
myleaderrank, opal_process_info.nodename);
465465

466466
abbreviate_list_into_string(&hoststring[strlen(hoststring)],
@@ -548,7 +548,7 @@ ompi_report_comm_methods(int called_from_location)
548548
ompi_count_array_t lens_desc;
549549
ompi_disp_array_t disps_desc;
550550

551-
// First get the array of host strings (host names and task lists)
551+
// First get the array of host strings (host names and task lists)
552552
// for all nodes.
553553
len = strlen(hoststring) + 1;
554554
if (myleaderrank == 0) {
@@ -642,7 +642,7 @@ ompi_report_comm_methods(int called_from_location)
642642
// 2: 2d table
643643
if (nleaderranks <= max2Dprottable) {
644644
char *str, *p;
645-
int tmp, per, has_ucx_transport;
645+
int tmp, per, has_ucx_transport, bufsize;
646646
int strlens[NUM_COMM_METHODS];
647647

648648
// characters per entry in the 2d table, must be large enough
@@ -668,11 +668,11 @@ ompi_report_comm_methods(int called_from_location)
668668
if (tmp+1 > per) { per = tmp+1; }
669669
}
670670
}
671-
672-
str = malloc(nleaderranks * per + 1);
671+
bufsize = nleaderranks * per + 1;
672+
str = malloc(bufsize);
673673
p = str;
674674
for (i=0; i<nleaderranks; ++i) {
675-
sprintf(p, "%d", i);
675+
snprintf(p, bufsize - (p - str), "%d", i);
676676
for (j=(int)strlen(p); j<per; ++j) {
677677
p[j] = ' ';
678678
}
@@ -698,12 +698,12 @@ ompi_report_comm_methods(int called_from_location)
698698
for (k=0; k<nleaderranks; ++k) {
699699
char *method_string;
700700
char ucx_label[20];
701-
701+
702702
method_string = comm_method_to_string(method[i * nleaderranks + k]);
703703
if (0 == strncmp(method_string, UCX_TAG, strlen(UCX_TAG))) {
704704
n = lookup_string_in_conversion_struct(&comm_method_string_conversion,
705705
method_string);
706-
sprintf(ucx_label, "ucx[%3d]", n);
706+
snprintf(ucx_label, sizeof(ucx_label), "ucx[%3d]", n);
707707
strcat(p, ucx_label);
708708
methods_used[n / 8] |= (1 << (n % 8));
709709
has_ucx_transport = 1;
@@ -755,7 +755,7 @@ ompi_report_comm_methods(int called_from_location)
755755
}
756756
else if (nleaderranks <= max2D1Cprottable) {
757757
char *str, *p;
758-
int tmp, per, done;
758+
int tmp, per, done, bufsize;
759759
char char_code[NUM_COMM_METHODS], next_char;
760760
int method_count[NUM_COMM_METHODS];
761761

@@ -798,12 +798,13 @@ ompi_report_comm_methods(int called_from_location)
798798
}
799799
}
800800

801-
str = malloc(per + 32 + nleaderranks * 2 + 1);
801+
bufsize = per + 32 + nleaderranks * 2 + 1;
802+
str = malloc(bufsize);
802803
p = str;
803-
sprintf(p, "0 1 2 3 ");
804+
snprintf(p, bufsize, "0 1 2 3 ");
804805
p += 8;
805806
for (i=4; i<nleaderranks; i+=4) {
806-
sprintf(p, "%d", i);
807+
snprintf(p, bufsize - (p - str), "%d", i);
807808
for (j=(int)strlen(p); j<8; ++j) {
808809
p[j] = ' ';
809810
}
@@ -972,15 +973,16 @@ ompi_report_comm_methods(int called_from_location)
972973
}
973974
}
974975
if (is_nonconformist) {
975-
char *str = malloc(1024);
976-
// int first = 1;
977-
sprintf(str, " host %d:", i);
976+
const size_t bufsize = 1024;
977+
char *str = malloc(bufsize);
978+
snprintf(str, bufsize, " host %d:", i);
978979
for (k=0; k<NUM_COMM_METHODS; ++k) {
979980
if (method_count[k] > 0) {
980981
// if (!first) {
981982
// strcat(str, " /");
982983
// }
983-
sprintf(&str[strlen(str)],
984+
snprintf(&str[strlen(str)],
985+
1024 - strlen(str),
984986
" [%dx %s]",
985987
method_count[k],
986988
comm_method_to_string(k));

ompi/mca/sharedfp/lockedfile/sharedfp_lockedfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct mca_sharedfp_base_module_2_0_0_t * mca_sharedfp_lockedfile_component_file
117117

118118
/* Set the filename. */
119119
/*data filename created by appending .locktest.$rank to the original filename*/
120-
sprintf(filename,"%s%s%d",fh->f_filename,".locktest.",rank);
120+
snprintf(filename, sizeof(filename), "%s%s%d",fh->f_filename,".locktest.",rank);
121121

122122
lock.l_type = F_WRLCK;
123123
lock.l_start = 0;

opal/mca/btl/smcuda/btl_smcuda.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ static struct mca_btl_base_endpoint_t *create_sm_endpoint(int local_proc, struct
486486
OBJ_CONSTRUCT(&ep->pending_sends, opal_list_t);
487487
OBJ_CONSTRUCT(&ep->endpoint_lock, opal_mutex_t);
488488
#if OPAL_ENABLE_PROGRESS_THREADS == 1
489-
sprintf(path, "%s" OPAL_PATH_SEP "sm_fifo.%lu", opal_process_info.job_session_dir,
489+
snprintf(path, sizeof(path), "%s" OPAL_PATH_SEP "sm_fifo.%lu", opal_process_info.job_session_dir,
490490
(unsigned long) proc->proc_name);
491491
ep->fifo_fd = open(path, O_WRONLY);
492492
if (ep->fifo_fd < 0) {

0 commit comments

Comments
 (0)