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>
This commit is contained in:
Sean McBride 2022-01-28 11:34:57 -05:00 committed by GitHub
parent 44de59b642
commit b5eed1b563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 43 additions and 46 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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])

View File

@ -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);
}

View File

@ -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)

View File

@ -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 ";

View File

@ -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)))

View File

@ -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,

View File

@ -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);

View File

@ -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);

View File

@ -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 */

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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.
*/

View File

@ -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() */

View File

@ -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(); \

View File

@ -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);
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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);