Skip to content

Commit 2ff7d6b

Browse files
committed
Replace sprintf with snprintf
Per note in the OMPI project, at least one compiler family is removing the "sprintf" function. Replace all uses of that function with the safer "snprintf" version. Signed-off-by: Ralph Castain <rhc@pmix.org>
1 parent 58130c6 commit 2ff7d6b

File tree

7 files changed

+14
-133
lines changed

7 files changed

+14
-133
lines changed

examples/debugger/direct-multi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ static pmix_status_t spawn_app(void)
564564
} else {
565565
PMIX_INFO_LIST_ADD(rc, tinfo, PMIX_DEBUG_STOP_IN_INIT, NULL, PMIX_BOOL); // All procs stop in PMIx_Init
566566
}
567-
sprintf(map_str, "ppr:%d:node", app_npernode);
567+
snprintf(map_str, 30, "ppr:%d:node", app_npernode);
568568
PMIX_INFO_LIST_ADD(rc, tinfo, PMIX_MAPBY, map_str, PMIX_STRING); // app procs/node
569569
PMIX_INFO_LIST_ADD(rc, tinfo, PMIX_RANKBY, "slot", PMIX_STRING); // match baseline
570570
PMIX_INFO_LIST_ADD(rc, tinfo, PMIX_FWD_STDOUT, NULL, PMIX_BOOL); // forward stdout to me

examples/debugger/direct.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static int cospawn_launch(myrel_t *myrel)
338338
/* Process that is spawning processes is a tool process */
339339
PMIX_INFO_LIST_ADD(rc, dirs, PMIX_REQUESTOR_IS_TOOL, NULL, PMIX_BOOL);
340340
/* Map spawned processes by slot */
341-
sprintf(map_str, "ppr:%d:node", app_npernode);
341+
snprintf(map_str, 128, "ppr:%d:node", app_npernode);
342342
PMIX_INFO_LIST_ADD(rc, dirs, PMIX_MAPBY, map_str, PMIX_STRING);
343343
PMIX_INFO_LIST_CONVERT(rc, dirs, &darray);
344344
PMIX_INFO_LIST_RELEASE(dirs);
@@ -824,7 +824,7 @@ int main(int argc, char **argv)
824824
// procs are to pause in PMIx_Init for debugger attach
825825
PMIX_INFO_LIST_ADD(rc, dirs, PMIX_DEBUG_STOP_IN_INIT, NULL, PMIX_BOOL);
826826
}
827-
sprintf(map_str, "ppr:%d:node", app_npernode);
827+
snprintf(map_str, 128, "ppr:%d:node", app_npernode);
828828
PMIX_INFO_LIST_ADD(rc, dirs, PMIX_MAPBY, map_str, PMIX_STRING); // 1 per node
829829
PMIX_INFO_LIST_ADD(rc, dirs, PMIX_FWD_STDOUT, NULL, PMIX_BOOL); // forward stdout to me
830830
PMIX_INFO_LIST_ADD(rc, dirs, PMIX_FWD_STDERR, NULL, PMIX_BOOL); // forward stderr to me

src/mca/rmaps/rank_file/rmaps_rank_file.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ static int prte_rmaps_rank_file_parse(const char *rankfile)
562562
case PRTE_RANKFILE_INT:
563563
case PRTE_RANKFILE_RELATIVE:
564564
if (PRTE_RANKFILE_INT == token) {
565-
sprintf(buff, "%d", prte_rmaps_rank_file_value.ival);
565+
snprintf(buff,RMAPS_RANK_FILE_MAX_SLOTS, "%d", prte_rmaps_rank_file_value.ival);
566566
value = buff;
567567
} else {
568568
value = prte_rmaps_rank_file_value.sval;
@@ -627,7 +627,7 @@ static int prte_rmaps_rank_file_parse(const char *rankfile)
627627
goto unlock;
628628
} else {
629629
/* prepare rank assignment string for the help message in case of a bad-assign */
630-
sprintf(tmp_rank_assignment, "%s slot=%s", node_name, value);
630+
snprintf(tmp_rank_assignment, RMAPS_RANK_FILE_MAX_SLOTS, "%s slot=%s", node_name, value);
631631
pmix_pointer_array_set_item(assigned_ranks_array, 0, tmp_rank_assignment);
632632
}
633633

@@ -671,7 +671,7 @@ static char *prte_rmaps_rank_file_parse_string_or_int(void)
671671
case PRTE_RANKFILE_STRING:
672672
return strdup(prte_rmaps_rank_file_value.sval);
673673
case PRTE_RANKFILE_INT:
674-
sprintf(tmp_str, "%d", prte_rmaps_rank_file_value.ival);
674+
snprintf(tmp_str, RMAPS_RANK_FILE_MAX_SLOTS, "%d", prte_rmaps_rank_file_value.ival);
675675
return strdup(tmp_str);
676676
default:
677677
return NULL;
@@ -787,7 +787,7 @@ static int prte_rmaps_rf_lsf_convert_affinity_to_rankfile(char *affinity_file, c
787787
// session dir + / (1) + lsf_rf. (7) + XXXXXX (6) + \0 (1)
788788
len = strlen(prte_process_info.top_session_dir) + 1 + 7 + 6 + 1;
789789
(*aff_rankfile) = (char*) malloc(sizeof(char) * len);
790-
sprintf(*aff_rankfile, "%s/lsf_rf.XXXXXX", prte_process_info.top_session_dir);
790+
snprintf(*aff_rankfile, len, "%s/lsf_rf.XXXXXX", prte_process_info.top_session_dir);
791791

792792
/* open the file */
793793
fp = fopen(affinity_file, "r");
@@ -882,7 +882,7 @@ static int prte_rmaps_rf_lsf_convert_affinity_to_rankfile(char *affinity_file, c
882882
free(cpus[i]);
883883
// 10 max number of digits in an int
884884
cpus[i] = (char*)malloc(sizeof(char) * 10);
885-
sprintf(cpus[i], "%d", obj->logical_index);
885+
snprintf(cpus[i], 10, "%d", obj->logical_index);
886886
}
887887
sep = PMIX_ARGV_JOIN_COMPAT(cpus, ',');
888888
PMIX_ARGV_FREE_COMPAT(cpus);
@@ -893,7 +893,7 @@ static int prte_rmaps_rf_lsf_convert_affinity_to_rankfile(char *affinity_file, c
893893
// "rank " (5) + id (max 10) + = (1) + host (?) + " slot=" (6) + ids (?) + '\0' (1)
894894
len = 5 + 10 + 1 + strlen(hstname) + 6 + strlen(sep) + 1;
895895
tmp_str = (char *)malloc(sizeof(char) * len);
896-
sprintf(tmp_str, "rank %d=%s slot=%s\n", cur_rank, hstname, sep);
896+
snprintf(tmp_str, len, "rank %d=%s slot=%s\n", cur_rank, hstname, sep);
897897
pmix_fd_write(fp_rank, strlen(tmp_str), tmp_str);
898898
free(tmp_str);
899899
++cur_rank;

src/rml/oob/oob_tcp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ int prte_oob_open(void)
287287
sizeof(copied_interface->if_mac));
288288
copied_interface->ifmtu = selected_interface->ifmtu;
289289
/* Add the if_mask to the list */
290-
sprintf(string, "%d", selected_interface->if_mask);
290+
snprintf(string, 50, "%d", selected_interface->if_mask);
291291
PMIX_ARGV_APPEND_NOSIZE_COMPAT(&prte_oob_base.if_masks, string);
292292
pmix_list_append(&prte_oob_base.local_ifs, &(copied_interface->super));
293293
}

test/double-get.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ int main(int argc, char *argv[])
154154
PMIX_VALUE_RELEASE(pvalue);
155155

156156
/* the below two lines break the subsequent PMIx_Get query on a key set later */
157-
sprintf(data, "FIRST TIME rank %d", myproc.rank);
158-
pmi_set_string("test-key-1", data, 256);
157+
snprintf(data, 256, "FIRST TIME rank %d", myproc.rank);
158+
pnmi_set_string("test-key-1", data, 256);
159159
pmix_exchange(true);
160160

161161
if (1 == myproc.rank) {
@@ -166,7 +166,7 @@ int main(int argc, char *argv[])
166166
}
167167
}
168168

169-
sprintf(data, "SECOND TIME rank %d", myproc.rank);
169+
snprintf(data, 256, "SECOND TIME rank %d", myproc.rank);
170170
if (0 == myproc.rank) {
171171
pmi_set_string("test-key-2", data, 256);
172172
} else {

test/get-nofence.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ int main(int argc, char *argv[])
163163
PMIX_VALUE_RELEASE(pvalue);
164164

165165
/* the below two lines break the subsequent PMIx_Get query on a key set later */
166-
sprintf(data, "FIRST TIME rank %d", myproc.rank);
166+
snprintf(data, 256, "FIRST TIME rank %d", myproc.rank);
167167
pmi_set_string("test-key-1", data, 256);
168168

169169
if (timeout) {

test/ptrace/ptrace_spawn_stopped.cxx

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)