Skip to content

Commit 50b0aef

Browse files
committed
misc: Replace sprintf usage with snprintf
Modern toolchains are warning that sprintf is deprecated and unsafe to use. Replace all usage with snprintf to eliminate those warnings.
1 parent d139078 commit 50b0aef

File tree

19 files changed

+51
-50
lines changed

19 files changed

+51
-50
lines changed

src/mpi/coll/algorithms/treealgo/treealgo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ static void dump_node(MPIR_Treealgo_tree_t * node, FILE * output_stream)
228228
static void dump_tree(int tree_type, int rank, MPIR_Treealgo_tree_t * ct)
229229
{
230230
char outfile_name[PATH_MAX];
231-
sprintf(outfile_name, "%s%d.json", "colltree", rank);
231+
snprintf(outfile_name, sizeof(outfile_name), "%s%d.json", "colltree", rank);
232232
fprintf(stdout, "tree_type=%d: dumping %s\n", tree_type, outfile_name);
233233
FILE *outfile = fopen(outfile_name, "w");
234234
dump_node(ct, outfile);

src/mpi/coll/algorithms/treealgo/treeutil.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ static int MPII_Treeutil_hierarchy_populate(MPIR_Comm * comm, int rank, int nran
556556
/* Dump hierarchy for debugging */
557557
if (MPIR_CVAR_HIERARCHY_DUMP) {
558558
char outfile_name[PATH_MAX];
559-
sprintf(outfile_name, "%s%d", "hierarchy", rank);
559+
snprintf(outfile_name, sizeof(outfile_name), "%s%d", "hierarchy", rank);
560560
FILE *outfile = fopen(outfile_name, "w");
561561
tree_topology_dump_hierarchy(hierarchy, rank, outfile);
562562
fclose(outfile);

src/mpi/romio/adio/common/ad_set_view.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ int check_type(ADIOI_Flatlist_node * flat_type,
1717
/* MPI standard requires the displacements of etype and filetype be
1818
* non-negative */
1919
if (flat_type->flag & ADIOI_TYPE_NEGATIVE) {
20-
sprintf(err_msg, "displacements of %s must be non-negative", type_kind);
20+
snprintf(err_msg, sizeof(err_msg), "displacements of %s must be non-negative", type_kind);
2121
goto err_check;
2222
}
2323

2424
/* MPI standard requires the displacements of etype and filetype be in a
2525
* monotonically nondecreasing order */
2626
if (flat_type->flag & ADIOI_TYPE_DECREASE) {
27-
sprintf(err_msg, "displacements of %s must be in a monotonically nondecreasing order",
28-
type_kind);
27+
snprintf(err_msg, sizeof(err_msg),
28+
"displacements of %s must be in a monotonically nondecreasing order", type_kind);
2929
goto err_check;
3030
}
3131

@@ -34,7 +34,8 @@ int check_type(ADIOI_Flatlist_node * flat_type,
3434
*/
3535
if (((access_mode & ADIO_WRONLY) || (access_mode & ADIO_RDWR)) &&
3636
(flat_type->flag & ADIOI_TYPE_OVERLAP)) {
37-
sprintf(err_msg, "%s is not permitted to contain overlapping regions", type_kind);
37+
snprintf(err_msg, sizeof(err_msg), "%s is not permitted to contain overlapping regions",
38+
type_kind);
3839
goto err_check;
3940
}
4041

src/mpi/romio/test/async-multiple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int main(int argc, char **argv)
7272
/* each process opens a separate file called filename.'myrank' */
7373
tmp = (char *) malloc(len + 10);
7474
strcpy(tmp, filename);
75-
sprintf(filename, "%s.%d", tmp, rank);
75+
snprintf(filename, len + 10, "%s.%d", tmp, rank);
7676

7777
errcode = MPI_File_open(MPI_COMM_SELF, filename,
7878
MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);

src/mpi/romio/test/async.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int main(int argc, char **argv)
7070
/* each process opens a separate file called filename.'myrank' */
7171
tmp = (char *) malloc(len + 10);
7272
strcpy(tmp, filename);
73-
sprintf(filename, "%s.%d", tmp, rank);
73+
snprintf(filename, len + 10, "%s.%d", tmp, rank);
7474

7575
errcode = MPI_File_open(MPI_COMM_SELF, filename,
7676
MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);

src/mpi/romio/test/error.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int main(int argc, char **argv)
7272
/* each process opens a separate file called filename.'myrank' */
7373
tmp = (char *) malloc(len + 10);
7474
strcpy(tmp, filename);
75-
sprintf(filename, "%s.%d", tmp, rank);
75+
snprintf(filename, len + 10, "%s.%d", tmp, rank);
7676

7777
MPI_CHECK(MPI_File_open(MPI_COMM_SELF, filename, MPI_MODE_CREATE + MPI_MODE_RDWR,
7878
MPI_INFO_NULL, &fh));

src/mpi/romio/test/file_info.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ int main(int argc, char **argv)
251251
MPI_Info_set(info, "cb_buffer_size", "8388608");
252252

253253
/* number of processes that actually perform I/O in collective I/O */
254-
sprintf(value, "%d", nprocs / 2);
254+
snprintf(value, sizeof(value), "%d", nprocs / 2);
255255
MPI_Info_set(info, "cb_nodes", value);
256256

257257
/* buffer size for data sieving in independent reads */
@@ -270,7 +270,7 @@ int main(int argc, char **argv)
270270
* If not set by the file system, the default value is 0.
271271
*/
272272
if (default_striping_factor > 0) {
273-
sprintf(value, "%d", default_striping_factor);
273+
snprintf(value, sizeof(value), "%d", default_striping_factor);
274274
MPI_Info_set(info, "striping_factor", value);
275275
}
276276

@@ -286,7 +286,7 @@ int main(int argc, char **argv)
286286
* accepted only if 0 <= value < default_striping_factor - 1
287287
* ignored otherwise */
288288
if (default_striping_factor > 1)
289-
sprintf(value, "%d", default_striping_factor - 2);
289+
snprintf(value, sizeof(value), "%d", default_striping_factor - 2);
290290
else
291291
strcpy(value, "0");
292292
MPI_Info_set(info, "start_iodevice", value);

src/mpi/romio/test/fstype_prefix.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ int main(int argc, char **argv)
164164
}
165165

166166
/* test a file system type prefix unknown to ROMIO */
167-
sprintf(out_fname, "nosuch_fstype:%s.out", filename);
167+
snprintf(out_fname, sizeof(out_fname), "nosuch_fstype:%s.out", filename);
168168
if (verbose && rank == 0)
169169
fprintf(stdout, "Testing file name prefix (unknown to ROMIO): %s", out_fname);
170170
err = MPI_File_open(MPI_COMM_WORLD, out_fname, MPI_MODE_CREATE | MPI_MODE_RDWR,
@@ -190,7 +190,7 @@ int main(int argc, char **argv)
190190
MPI_Barrier(MPI_COMM_WORLD);
191191

192192
/* test a file system type prefix known to ROMIO and enabled at configure */
193-
sprintf(out_fname, "%s%s.out", enabled_prefix, filename);
193+
snprintf(out_fname, sizeof(out_fname), "%s%s.out", enabled_prefix, filename);
194194
if (verbose && rank == 0)
195195
fprintf(stdout, "Testing file name prefix (known and enabled): %s", enabled_prefix);
196196
err = MPI_File_open(MPI_COMM_WORLD, out_fname, MPI_MODE_CREATE | MPI_MODE_RDWR,
@@ -204,7 +204,7 @@ int main(int argc, char **argv)
204204
MPI_Barrier(MPI_COMM_WORLD);
205205

206206
/* strip the known prefix */
207-
sprintf(out_fname, "%s.out", filename);
207+
snprintf(out_fname, sizeof(out_fname), "%s.out", filename);
208208
err = check_file_exist(out_fname);
209209
if (err != 0)
210210
goto err_out;
@@ -219,7 +219,7 @@ int main(int argc, char **argv)
219219

220220
/* set a known file system type prefix to ROMIO in environment variable ROMIO_FSTYPE_FORCE */
221221
setenv("ROMIO_FSTYPE_FORCE", enabled_prefix, 1);
222-
sprintf(out_fname, "%s.out", filename);
222+
snprintf(out_fname, sizeof(out_fname), "%s.out", filename);
223223
if (verbose && rank == 0)
224224
fprintf(stdout, "Testing ROMIO_FSTYPE_FORCE prefix (known and enabled): %s",
225225
enabled_prefix);
@@ -246,7 +246,7 @@ int main(int argc, char **argv)
246246
MPI_Barrier(MPI_COMM_WORLD);
247247

248248
/* test a file system type prefix known to ROMIO but disabled at configure */
249-
sprintf(out_fname, "%s%s.out", disabled_prefix, filename);
249+
snprintf(out_fname, sizeof(out_fname), "%s%s.out", disabled_prefix, filename);
250250
if (verbose && rank == 0)
251251
fprintf(stdout, "Testing file name prefix (known but disabled): %s", disabled_prefix);
252252
err = MPI_File_open(MPI_COMM_WORLD, out_fname, MPI_MODE_CREATE | MPI_MODE_RDWR,
@@ -259,7 +259,7 @@ int main(int argc, char **argv)
259259

260260
/* set a known file system type prefix to ROMIO in environment variable ROMIO_FSTYPE_FORCE */
261261
setenv("ROMIO_FSTYPE_FORCE", disabled_prefix, 1);
262-
sprintf(out_fname, "%s.out", filename);
262+
snprintf(out_fname, sizeof(out_fname), "%s.out", filename);
263263
if (verbose && rank == 0)
264264
fprintf(stdout, "Testing ROMIO_FSTYPE_FORCE prefix (known but disabled): %s",
265265
disabled_prefix);

src/mpi/romio/test/psimple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int main(int argc, char **argv)
7373
/* each process opens a separate file called filename.'myrank' */
7474
tmp = (char *) malloc(len + 10);
7575
strcpy(tmp, filename);
76-
sprintf(filename, "%s.%d", tmp, rank);
76+
snprintf(filename, len + 10, "%s.%d", tmp, rank);
7777

7878
MPI_CHECK(PMPI_File_open(MPI_COMM_SELF, filename,
7979
MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh));

src/mpi/romio/test/simple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int main(int argc, char **argv)
6767
/* each process opens a separate file called filename.'myrank' */
6868
tmp = (char *) malloc(len + 10);
6969
strcpy(tmp, filename);
70-
sprintf(filename, "%s.%d", tmp, rank);
70+
snprintf(filename, len + 10, "%s.%d", tmp, rank);
7171

7272
errcode = MPI_File_open(MPI_COMM_SELF, filename,
7373
MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);

0 commit comments

Comments
 (0)