From b5eed1b56324fc07154c2e2d8251d2b87505ca23 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 28 Jan 2022 11:34:57 -0500 Subject: Replaced several uses of sprintf with safer snprintf (#1383) * Replaced several uses of sprintf with safer snprintf * Committing clang-format changes Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- c++/test/tattr.cpp | 6 +++--- c++/test/titerate.cpp | 2 +- c++/test/tvlstr.cpp | 10 +++++----- config/cmake_ext_mod/HDFTests.c | 4 ++-- configure.ac | 2 +- examples/ph5example.c | 2 +- hl/tools/gif2h5/writehdf.c | 2 +- hl/tools/h5watch/h5watch.c | 4 ++-- src/H5AC.c | 2 +- src/H5Dchunk.c | 4 ++-- src/H5Dearray.c | 4 ++-- src/H5Dfarray.c | 4 ++-- src/H5EAdbg.c | 6 +++--- src/H5EAtest.c | 2 +- src/H5FAtest.c | 2 +- src/H5FDmulti.c | 6 +++--- src/H5RS.c | 2 +- src/H5VLpassthru.c | 13 +++++-------- src/H5private.h | 2 +- test/chunk_info.c | 2 +- test/flush1.c | 2 +- test/flush2.c | 2 +- test/unregister.c | 2 +- tools/test/perform/direct_write_perf.c | 2 +- 24 files changed, 43 insertions(+), 46 deletions(-) diff --git a/c++/test/tattr.cpp b/c++/test/tattr.cpp index 5627341..dc968f9 100644 --- a/c++/test/tattr.cpp +++ b/c++/test/tattr.cpp @@ -1782,7 +1782,7 @@ test_attr_dense_create(FileCreatPropList &fcpl, FileAccPropList &fapl) unsigned attr_num; for (attr_num = 0; attr_num < max_compact; attr_num++) { // Create attribute - sprintf(attr_name, "attr %02u", attr_num); + snprintf(attr_name, sizeof(attr_name), "attr %02u", attr_num); Attribute attr = dataset.createAttribute(attr_name, PredType::NATIVE_UINT, ds_space); // Write data to the attribute @@ -1794,7 +1794,7 @@ test_attr_dense_create(FileCreatPropList &fcpl, FileAccPropList &fapl) { // Add one more attribute, to push into "dense" storage // Create another attribute - sprintf(attr_name, "attr %02u", attr_num); + snprintf(attr_name, sizeof(attr_name), "attr %02u", attr_num); Attribute attr = dataset.createAttribute(attr_name, PredType::NATIVE_UINT, ds_space); // Write data to the attribute @@ -1804,7 +1804,7 @@ test_attr_dense_create(FileCreatPropList &fcpl, FileAccPropList &fapl) // Attempt to add attribute again, which should fail try { // Create another attribute - sprintf(attr_name, "attr %02u", attr_num); + snprintf(attr_name, sizeof(attr_name), "attr %02u", attr_num); Attribute attr = dataset.createAttribute(attr_name, PredType::NATIVE_UINT, ds_space); // continuation here, that means no exception has been thrown diff --git a/c++/test/titerate.cpp b/c++/test/titerate.cpp index b6a9436..bc4d892 100644 --- a/c++/test/titerate.cpp +++ b/c++/test/titerate.cpp @@ -160,7 +160,7 @@ test_iter_group(FileAccPropList &fapl) DataSpace filespace; for (int i = 0; i < NDATASETS; i++) { - sprintf(name, "Dataset %d", i); + snprintf(name, sizeof(name), "Dataset %d", i); // Create a dataset in the file DataSet dataset = file.createDataSet(name, datatype, filespace); diff --git a/c++/test/tvlstr.cpp b/c++/test/tvlstr.cpp index c91b566..405ca07 100644 --- a/c++/test/tvlstr.cpp +++ b/c++/test/tvlstr.cpp @@ -917,32 +917,32 @@ test_vl_rewrite() int i; char name[256]; // Buffer for names & data for (i = 0; i < REWRITE_NDATASETS; i++) { - sprintf(name, "/set_%d", i); + snprintf(name, sizeof(name), "/set_%d", i); write_scalar_dset(file1, type, space, name, name); } // Effectively copy data from file 1 to 2. for (i = 0; i < REWRITE_NDATASETS; i++) { - sprintf(name, "/set_%d", i); + snprintf(name, sizeof(name), "/set_%d", i); read_scalar_dset(file1, type, space, name, name); write_scalar_dset(file2, type, space, name, name); } // Read back from file 2. for (i = 0; i < REWRITE_NDATASETS; i++) { - sprintf(name, "/set_%d", i); + snprintf(name, sizeof(name), "/set_%d", i); read_scalar_dset(file2, type, space, name, name); } // Remove from file 2. for (i = 0; i < REWRITE_NDATASETS; i++) { - sprintf(name, "/set_%d", i); + snprintf(name, sizeof(name), "/set_%d", i); file2.unlink(name); } // Effectively copy from file 1 to file 2. for (i = 0; i < REWRITE_NDATASETS; i++) { - sprintf(name, "/set_%d", i); + snprintf(name, sizeof(name), "/set_%d", i); read_scalar_dset(file1, type, space, name, name); write_scalar_dset(file2, type, space, name, name); } diff --git a/config/cmake_ext_mod/HDFTests.c b/config/cmake_ext_mod/HDFTests.c index c434be5..2b2a202 100644 --- a/config/cmake_ext_mod/HDFTests.c +++ b/config/cmake_ext_mod/HDFTests.c @@ -115,8 +115,8 @@ int main(void) for (currentArg = llwidthArgs; *currentArg != NULL; currentArg++) { char formatString[64]; - sprintf(formatString, "%%%sd", *currentArg); - sprintf(s, formatString, x); + snprintf(formatString, sizeof(formatString), "%%%sd", *currentArg); + snprintf(s, 128, formatString, x); if (strcmp(s, "1099511627776") == 0) { printf("PRINTF_LL_WIDTH=[%s]\n", *currentArg); diff --git a/configure.ac b/configure.ac index 8d7f359..4cf329c 100644 --- a/configure.ac +++ b/configure.ac @@ -2154,7 +2154,7 @@ for hdf5_cv_printf_ll in ll l L q unknown; do ],[[ char *s = malloc(128); long long x = (long long)1048576 * (long long)1048576; - sprintf(s,"%${hdf5_cv_printf_ll}d",x); + snprintf(s,128,"%${hdf5_cv_printf_ll}d",x); exit(strcmp(s,"1099511627776")); ]])] , [break],,[continue]) diff --git a/examples/ph5example.c b/examples/ph5example.c index 87632e5..23af477 100644 --- a/examples/ph5example.c +++ b/examples/ph5example.c @@ -934,7 +934,7 @@ mkfilenames(char *prefix) "Need to adjust the code to accommodate the large size.\n"); } for (i = 0; i < n; i++) { - sprintf(testfiles[i], "%s/ParaEg%d.h5", prefix, i); + snprintf(testfiles[i], PATH_MAX, "%s/ParaEg%d.h5", prefix, i); } return (0); } diff --git a/hl/tools/gif2h5/writehdf.c b/hl/tools/gif2h5/writehdf.c index c321519..b49e2a6 100644 --- a/hl/tools/gif2h5/writehdf.c +++ b/hl/tools/gif2h5/writehdf.c @@ -100,7 +100,7 @@ WriteHDF(GIFTOMEM GifMemoryStruct, char *HDFName) dims[1] = gifImageDesc->ImageWidth; /* create the image name */ - sprintf(ImageName, "Image%d", i); + snprintf(ImageName, sizeof(ImageName), "Image%d", i); /* write image */ if (H5IMmake_image_8bit(file_id, ImageName, dims[1], dims[0], (gifImageDesc->Image)) < 0) diff --git a/hl/tools/h5watch/h5watch.c b/hl/tools/h5watch/h5watch.c index c897c91..94ca4f1 100644 --- a/hl/tools/h5watch/h5watch.c +++ b/hl/tools/h5watch/h5watch.c @@ -174,9 +174,9 @@ doprint(hid_t did, const hsize_t *start, const hsize_t *block, int rank) } /* end else */ /* Floating point types should display full precision */ - sprintf(fmt_float, "%%1.%dg", FLT_DIG); + snprintf(fmt_float, sizeof(fmt_float), "%%1.%dg", FLT_DIG); info.fmt_float = fmt_float; - sprintf(fmt_double, "%%1.%dg", DBL_DIG); + snprintf(fmt_double, sizeof(fmt_double), "%%1.%dg", DBL_DIG); info.fmt_double = fmt_double; info.dset_format = "DSET-%s "; diff --git a/src/H5AC.c b/src/H5AC.c index e20be3b..47d3a65 100644 --- a/src/H5AC.c +++ b/src/H5AC.c @@ -303,7 +303,7 @@ H5AC_create(const H5F_t *f, H5AC_cache_config_t *config_ptr, H5AC_cache_image_co aux_ptr->sync_point_done = NULL; aux_ptr->p0_image_len = 0; - HDsprintf(prefix, "%d:", mpi_rank); + HDsnprintf(prefix, sizeof(prefix), "%d:", mpi_rank); if (mpi_rank == 0) { if (NULL == (aux_ptr->d_slist_ptr = H5SL_create(H5SL_TYPE_HADDR, NULL))) diff --git a/src/H5Dchunk.c b/src/H5Dchunk.c index b85b194..5d7c1b2 100644 --- a/src/H5Dchunk.c +++ b/src/H5Dchunk.c @@ -6696,10 +6696,10 @@ H5D__chunk_stats(const H5D_t *dset, hbool_t headers) miss_rate = 0.0; } if (miss_rate > 100) { - HDsprintf(ascii, "%7d%%", (int)(miss_rate + 0.5)); + HDsnprintf(ascii, sizeof(ascii), "%7d%%", (int)(miss_rate + 0.5)); } else { - HDsprintf(ascii, "%7.2f%%", miss_rate); + HDsnprintf(ascii, sizeof(ascii), "%7.2f%%", miss_rate); } HDfprintf(H5DEBUG(AC), " %-18s %8u %8u %7s %8d+%-9ld\n", "raw data chunks", rdcc->stats.nhits, diff --git a/src/H5Dearray.c b/src/H5Dearray.c index abce233..cd52b66 100644 --- a/src/H5Dearray.c +++ b/src/H5Dearray.c @@ -417,7 +417,7 @@ H5D__earray_debug(FILE *stream, int indent, int fwidth, hsize_t idx, const void HDassert(elmt); /* Print element */ - HDsprintf(temp_str, "Element #%" PRIuHSIZE ":", idx); + HDsnprintf(temp_str, sizeof(temp_str), "Element #%" PRIuHSIZE ":", idx); HDfprintf(stream, "%*s%-*s %" PRIuHADDR "\n", indent, "", fwidth, temp_str, *(const haddr_t *)elmt); FUNC_LEAVE_NOAPI(SUCCEED) @@ -573,7 +573,7 @@ H5D__earray_filt_debug(FILE *stream, int indent, int fwidth, hsize_t idx, const HDassert(elmt); /* Print element */ - HDsprintf(temp_str, "Element #%" PRIuHSIZE ":", idx); + HDsnprintf(temp_str, sizeof(temp_str), "Element #%" PRIuHSIZE ":", idx); HDfprintf(stream, "%*s%-*s {%" PRIuHADDR ", %u, %0x}\n", indent, "", fwidth, temp_str, elmt->addr, elmt->nbytes, elmt->filter_mask); diff --git a/src/H5Dfarray.c b/src/H5Dfarray.c index 0741e8f..ab0f0f8 100644 --- a/src/H5Dfarray.c +++ b/src/H5Dfarray.c @@ -415,7 +415,7 @@ H5D__farray_debug(FILE *stream, int indent, int fwidth, hsize_t idx, const void HDassert(elmt); /* Print element */ - HDsprintf(temp_str, "Element #%" PRIuHSIZE ":", idx); + HDsnprintf(temp_str, sizeof(temp_str), "Element #%" PRIuHSIZE ":", idx); HDfprintf(stream, "%*s%-*s %" PRIuHADDR "\n", indent, "", fwidth, temp_str, *(const haddr_t *)elmt); FUNC_LEAVE_NOAPI(SUCCEED) @@ -675,7 +675,7 @@ H5D__farray_filt_debug(FILE *stream, int indent, int fwidth, hsize_t idx, const HDassert(elmt); /* Print element */ - HDsprintf(temp_str, "Element #%" PRIuHSIZE ":", idx); + HDsnprintf(temp_str, sizeof(temp_str), "Element #%" PRIuHSIZE ":", idx); HDfprintf(stream, "%*s%-*s {%" PRIuHADDR ", %u, %0x}\n", indent, "", fwidth, temp_str, elmt->addr, elmt->nbytes, elmt->filter_mask); diff --git a/src/H5EAdbg.c b/src/H5EAdbg.c index b0e564c..b377422 100644 --- a/src/H5EAdbg.c +++ b/src/H5EAdbg.c @@ -237,7 +237,7 @@ H5EA__iblock_debug(H5F_t *f, haddr_t H5_ATTR_UNUSED addr, FILE *stream, int inde HDfprintf(stream, "%*sData Block Addresses in Index Block:\n", indent, ""); for (u = 0; u < iblock->ndblk_addrs; u++) { /* Print address */ - HDsprintf(temp_str, "Address #%u:", u); + HDsnprintf(temp_str, sizeof(temp_str), "Address #%u:", u); HDfprintf(stream, "%*s%-*s %" PRIuHADDR "\n", (indent + 3), "", MAX(0, (fwidth - 3)), temp_str, iblock->dblk_addrs[u]); } /* end for */ @@ -252,7 +252,7 @@ H5EA__iblock_debug(H5F_t *f, haddr_t H5_ATTR_UNUSED addr, FILE *stream, int inde HDfprintf(stream, "%*sSuper Block Addresses in Index Block:\n", indent, ""); for (u = 0; u < iblock->nsblk_addrs; u++) { /* Print address */ - HDsprintf(temp_str, "Address #%u:", u); + HDsnprintf(temp_str, sizeof(temp_str), "Address #%u:", u); HDfprintf(stream, "%*s%-*s %" PRIuHADDR "\n", (indent + 3), "", MAX(0, (fwidth - 3)), temp_str, iblock->sblk_addrs[u]); } /* end for */ @@ -341,7 +341,7 @@ H5EA__sblock_debug(H5F_t *f, haddr_t addr, FILE *stream, int indent, int fwidth, HDfprintf(stream, "%*sData Block Addresses in Super Block:\n", indent, ""); for (u = 0; u < sblock->ndblks; u++) { /* Print address */ - HDsprintf(temp_str, "Address #%u:", u); + HDsnprintf(temp_str, sizeof(temp_str), "Address #%u:", u); HDfprintf(stream, "%*s%-*s %" PRIuHADDR "\n", (indent + 3), "", MAX(0, (fwidth - 3)), temp_str, sblock->dblk_addrs[u]); } /* end for */ diff --git a/src/H5EAtest.c b/src/H5EAtest.c index 7924eaa..24efbc2 100644 --- a/src/H5EAtest.c +++ b/src/H5EAtest.c @@ -322,7 +322,7 @@ H5EA__test_debug(FILE *stream, int indent, int fwidth, hsize_t idx, const void * HDassert(elmt); /* Print element */ - HDsprintf(temp_str, "Element #%llu:", (unsigned long long)idx); + HDsnprintf(temp_str, sizeof(temp_str), "Element #%llu:", (unsigned long long)idx); HDfprintf(stream, "%*s%-*s %llu\n", indent, "", fwidth, temp_str, (unsigned long long)*(const uint64_t *)elmt); diff --git a/src/H5FAtest.c b/src/H5FAtest.c index 384a657..b57f562 100644 --- a/src/H5FAtest.c +++ b/src/H5FAtest.c @@ -303,7 +303,7 @@ H5FA__test_debug(FILE *stream, int indent, int fwidth, hsize_t idx, const void * HDassert(elmt); /* Print element */ - HDsprintf(temp_str, "Element #%llu:", (unsigned long long)idx); + HDsnprintf(temp_str, sizeof(temp_str), "Element #%llu:", (unsigned long long)idx); HDfprintf(stream, "%*s%-*s %llu\n", indent, "", fwidth, temp_str, (unsigned long long)*(const uint64_t *)elmt); diff --git a/src/H5FDmulti.c b/src/H5FDmulti.c index 3dcfa37..d9a6ce9 100644 --- a/src/H5FDmulti.c +++ b/src/H5FDmulti.c @@ -517,7 +517,7 @@ H5FD_split_populate_config(const char *meta_ext, hid_t meta_plist_id, const char meta_name_g[sizeof(meta_name_g) - 1] = '\0'; } else - sprintf(meta_name_g, "%%s%s", meta_ext); + snprintf(meta_name_g, sizeof(meta_name_g), "%%s%s", meta_ext); } else { strncpy(meta_name_g, "%s.meta", sizeof(meta_name_g)); @@ -535,7 +535,7 @@ H5FD_split_populate_config(const char *meta_ext, hid_t meta_plist_id, const char raw_name_g[sizeof(raw_name_g) - 1] = '\0'; } else - sprintf(raw_name_g, "%%s%s", raw_ext); + snprintf(raw_name_g, sizeof(raw_name_g), "%%s%s", raw_ext); } else { strncpy(raw_name_g, "%s.raw", sizeof(raw_name_g)); @@ -634,7 +634,7 @@ H5FD_multi_populate_config(const H5FD_mem_t *memb_map, const hid_t *memb_fapl, c if (!memb_name) { assert(strlen(letters) == H5FD_MEM_NTYPES); for (mt = H5FD_MEM_DEFAULT; mt < H5FD_MEM_NTYPES; mt = (H5FD_mem_t)(mt + 1)) { - sprintf(_memb_name_g[mt], "%%s-%c.h5", letters[mt]); + snprintf(_memb_name_g[mt], 16, "%%s-%c.h5", letters[mt]); _memb_name_ptrs[mt] = _memb_name_g[mt]; } memb_name = _memb_name_ptrs; diff --git a/src/H5RS.c b/src/H5RS.c index 117c8ea..16c2356 100644 --- a/src/H5RS.c +++ b/src/H5RS.c @@ -350,7 +350,7 @@ done: */ /* Disable warning for "format not a string literal" here -QAK */ /* - * This pragma only needs to surround the sprintf() calls with + * This pragma only needs to surround the snprintf() calls with * format_templ in the code below, but early (4.4.7, at least) gcc only * allows diagnostic pragmas to be toggled outside of functions. */ diff --git a/src/H5VLpassthru.c b/src/H5VLpassthru.c index 6b4c59f..6eda875 100644 --- a/src/H5VLpassthru.c +++ b/src/H5VLpassthru.c @@ -641,16 +641,13 @@ H5VL_pass_through_info_to_str(const void *_info, char **str) under_vol_str_len = strlen(under_vol_string); /* Allocate space for our info */ - *str = (char *)H5allocate_memory(32 + under_vol_str_len, (hbool_t)0); + size_t strSize = 32 + under_vol_str_len; + *str = (char *)H5allocate_memory(strSize, (hbool_t)0); assert(*str); - /* Encode our info - * Normally we'd use snprintf() here for a little extra safety, but that - * call had problems on Windows until recently. So, to be as platform-independent - * as we can, we're using sprintf() instead. - */ - sprintf(*str, "under_vol=%u;under_info={%s}", (unsigned)under_value, - (under_vol_string ? under_vol_string : "")); + /* Encode our info */ + snprintf(*str, strSize, "under_vol=%u;under_info={%s}", (unsigned)under_value, + (under_vol_string ? under_vol_string : "")); return 0; } /* end H5VL_pass_through_info_to_str() */ diff --git a/src/H5private.h b/src/H5private.h index 5b0e33b..fa52e7f 100644 --- a/src/H5private.h +++ b/src/H5private.h @@ -172,7 +172,7 @@ */ #define BEGIN_MPE_LOG \ if (H5_MPEinit_g) { \ - sprintf(p_event_start, "start %s", __func__); \ + snprintf(p_event_start, sizeof(p_event_start), "start %s", __func__); \ if (eventa(__func__) == -1 && eventb(__func__) == -1) { \ const char *p_color = "red"; \ eventa(__func__) = MPE_Log_get_event_number(); \ diff --git a/test/chunk_info.c b/test/chunk_info.c index 148c577..2bec42d 100644 --- a/test/chunk_info.c +++ b/test/chunk_info.c @@ -421,7 +421,7 @@ verify_idx_nchunks(hid_t dset, hid_t dspace, H5D_chunk_index_t exp_idx_type, hsi /* Ensure the correct chunk indexing scheme is used */ if (idx_type != exp_idx_type) { char msg[256]; - HDsprintf(msg, "Should be using %s.\n", index_type_str(idx_type)); + HDsnprintf(msg, sizeof(msg), "Should be using %s.\n", index_type_str(idx_type)); FAIL_PUTS_ERROR(msg); } diff --git a/test/flush1.c b/test/flush1.c index f5c4e2b..0ddceb6 100644 --- a/test/flush1.c +++ b/test/flush1.c @@ -81,7 +81,7 @@ create_file(const char *filename, hid_t fapl_id, hbool_t swmr) if ((top_gid = H5Gcreate2(fid, "top_group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) STACK_ERROR for (i = 0; i < NGROUPS; i++) { - HDsprintf(group_name, "group%02d", i); + HDsnprintf(group_name, sizeof(group_name), "group%02d", i); if ((gid = H5Gcreate2(top_gid, group_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) STACK_ERROR if (H5Gclose(gid) < 0) diff --git a/test/flush2.c b/test/flush2.c index bf1187b..b6c7761 100644 --- a/test/flush2.c +++ b/test/flush2.c @@ -144,7 +144,7 @@ file_ok(const char *filename, hid_t fapl_id, hbool_t check_second_dset) if ((top_gid = H5Gopen2(fid, "top_group", H5P_DEFAULT)) < 0) goto error; for (i = 0; i < NGROUPS; i++) { - HDsprintf(group_name, "group%02d", i); + HDsnprintf(group_name, sizeof(group_name), "group%02d", i); if ((gid = H5Gopen2(top_gid, group_name, H5P_DEFAULT)) < 0) goto error; if (H5Gclose(gid) < 0) diff --git a/test/unregister.c b/test/unregister.c index 803f8373..ebb51b1 100644 --- a/test/unregister.c +++ b/test/unregister.c @@ -136,7 +136,7 @@ test_unregister_filters(hid_t fapl_id) /* Create multiple groups under the main group */ for (i = 0; i < GROUP_ITERATION; i++) { - HDsprintf(group_name, "group_%d", i); + HDsnprintf(group_name, sizeof(group_name), "group_%d", i); if ((gid_loop = H5Gcreate2(gid, group_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if (H5Gclose(gid_loop) < 0) diff --git a/tools/test/perform/direct_write_perf.c b/tools/test/perform/direct_write_perf.c index 2076cf4..1d7756b 100644 --- a/tools/test/perform/direct_write_perf.c +++ b/tools/test/perform/direct_write_perf.c @@ -655,7 +655,7 @@ main(void) hid_t fapl = H5P_DEFAULT; int i; - sprintf(filename, "%s.h5", FILENAME[0]); + snprintf(filename, sizeof(filename), "%s.h5", FILENAME[0]); create_file(fapl); test_direct_write_uncompressed_data(fapl); -- cgit v0.12