Skip to content

Commit

Permalink
Replaced some trickier sprintf with snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
seanm committed Feb 12, 2024
1 parent eb00f19 commit 9ec68dc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 43 deletions.
32 changes: 16 additions & 16 deletions src/H5FDfamily.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,27 +234,27 @@ H5FD__family_get_default_printf_filename(const char *old_filename)
HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, NULL, "can't allocate new filename buffer");

/* Determine if filename contains a ".h5" extension. */
if ((file_extension = strstr(old_filename, ".h5"))) {
file_extension = strstr(old_filename, ".h5");
if (file_extension) {
/* Insert the printf format between the filename and ".h5" extension. */
strcpy(tmp_buffer, old_filename);
file_extension = strstr(tmp_buffer, ".h5");
sprintf(file_extension, "%s%s", suffix, ".h5");
intptr_t beginningLength = file_extension - old_filename;
snprintf(tmp_buffer, new_filename_len, "%.*s%s%s", (int)beginningLength, old_filename, suffix, ".h5");
}
else if ((file_extension = strrchr(old_filename, '.'))) {
char *new_extension_loc = NULL;

else {
/* If the filename doesn't contain a ".h5" extension, but contains
* AN extension, just insert the printf format before that extension.
*/
strcpy(tmp_buffer, old_filename);
new_extension_loc = strrchr(tmp_buffer, '.');
sprintf(new_extension_loc, "%s%s", suffix, file_extension);
}
else {
/* If the filename doesn't contain an extension at all, just insert
* the printf format at the end of the filename.
*/
snprintf(tmp_buffer, new_filename_len, "%s%s", old_filename, suffix);
file_extension = strrchr(old_filename, '.');
if (file_extension) {
intptr_t beginningLength = file_extension - old_filename;
snprintf(tmp_buffer, new_filename_len, "%.*s%s%s", (int)beginningLength, old_filename, suffix, file_extension);
}
else {
/* If the filename doesn't contain an extension at all, just insert
* the printf format at the end of the filename.
*/
snprintf(tmp_buffer, new_filename_len, "%s%s", old_filename, suffix);
}
}

ret_value = tmp_buffer;
Expand Down
32 changes: 16 additions & 16 deletions src/H5FDsplitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,27 +532,27 @@ H5FD__splitter_get_default_wo_path(char *new_path, size_t new_path_len, const ch
HGOTO_ERROR(H5E_VFL, H5E_CANTSET, FAIL, "filename exceeds max length");

/* Determine if filename contains a ".h5" extension. */
if ((file_extension = strstr(base_filename, ".h5"))) {
file_extension = strstr(base_filename, ".h5");
if (file_extension) {
/* Insert the suffix between the filename and ".h5" extension. */
strcpy(new_path, base_filename);
file_extension = strstr(new_path, ".h5");
sprintf(file_extension, "%s%s", suffix, ".h5");
intptr_t beginningLength = file_extension - base_filename;
snprintf(new_path, new_path_len, "%.*s%s%s", (int)beginningLength, base_filename, suffix, ".h5");
}
else if ((file_extension = strrchr(base_filename, '.'))) {
char *new_extension_loc = NULL;

else {
/* If the filename doesn't contain a ".h5" extension, but contains
* AN extension, just insert the suffix before that extension.
*/
strcpy(new_path, base_filename);
new_extension_loc = strrchr(new_path, '.');
sprintf(new_extension_loc, "%s%s", suffix, file_extension);
}
else {
/* If the filename doesn't contain an extension at all, just insert
* the suffix at the end of the filename.
*/
snprintf(new_path, new_path_len, "%s%s", base_filename, suffix);
file_extension = strrchr(base_filename, '.');
if (file_extension) {
intptr_t beginningLength = file_extension - base_filename;
snprintf(new_path, new_path_len, "%.*s%s%s", (int)beginningLength, base_filename, suffix, file_extension);
}
else {
/* If the filename doesn't contain an extension at all, just insert
* the suffix at the end of the filename.
*/
snprintf(new_path, new_path_len, "%s%s", base_filename, suffix);
}
}

done:
Expand Down
10 changes: 2 additions & 8 deletions test/filter_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,10 +847,7 @@ test_creating_groups_using_plugins(hid_t fid)

/* Create multiple groups under the top-level group */
for (i = 0; i < N_SUBGROUPS; i++) {
char *sp = subgroup_name;

sp += snprintf(subgroup_name, sizeof(subgroup_name), SUBGROUP_PREFIX);
sprintf(sp, "%d", i);
snprintf(subgroup_name, sizeof(subgroup_name), SUBGROUP_PREFIX "%d", i);

if ((sub_gid = H5Gcreate2(gid, subgroup_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
TEST_ERROR;
Expand Down Expand Up @@ -906,10 +903,7 @@ test_opening_groups_using_plugins(hid_t fid)

/* Open all the sub-groups under the top-level group */
for (i = 0; i < N_SUBGROUPS; i++) {
char *sp = subgroup_name;

sp += snprintf(subgroup_name, sizeof(subgroup_name), SUBGROUP_PREFIX);
sprintf(sp, "%d", i);
snprintf(subgroup_name, sizeof(subgroup_name), SUBGROUP_PREFIX "%d", i);

if ((sub_gid = H5Gopen2(gid, subgroup_name, H5P_DEFAULT)) < 0)
TEST_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion tools/test/h5dump/h5dumpgentest.c
Original file line number Diff line number Diff line change
Expand Up @@ -3884,7 +3884,7 @@ gent_multi(void)
for (mt = H5FD_MEM_DEFAULT; mt < H5FD_MEM_NTYPES; mt++) {
memb_fapl[mt] = H5P_DEFAULT;
memb_map[mt] = mt;
sprintf(sv[mt], "%%s-%c.h5", multi_letters[mt]);
snprintf(sv[mt], 1024, "%%s-%c.h5", multi_letters[mt]);
memb_name[mt] = sv[mt];
/*printf("memb_name[%d]=%s, memb_map[%d]=%d; ", mt, memb_name[mt], mt, memb_map[mt]);*/
memb_addr[mt] = (haddr_t)MAX(mt - 1, 0) * (HADDR_MAX / 10);
Expand Down
4 changes: 2 additions & 2 deletions utils/tools/h5dwalk/h5dwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,10 +1209,10 @@ MFU_PRED_EXEC(mfu_flist flist, uint64_t idx, void *arg)
snprintf(cmdline, sizeof(cmdline), "\n---------\nCommand:");
b_offset = strlen(cmdline);
for (k = 0; k < count; k++) {
sprintf(&cmdline[b_offset], " %s", argv[k]);
snprintf(&cmdline[b_offset], sizeof(cmdline) - b_offset, " %s", argv[k]);
b_offset = strlen(cmdline);
}
sprintf(&cmdline[b_offset], "\n");
snprintf(&cmdline[b_offset], sizeof(cmdline) - b_offset, "\n");
run_command(count, argv, cmdline, fname);

mfu_free(argv);
Expand Down

0 comments on commit 9ec68dc

Please sign in to comment.