Skip to content

Commit

Permalink
Call memset before stat calls (#4202)
Browse files Browse the repository at this point in the history
The buffers passed to stat-like calls are only partially filled in by
the call, leaving ununitialized memory areas when the stat buffers are
created on the stack.

This change memsets the buffers to 0 before the stat calls, quieting
the -fsanitze=memory complaints.
  • Loading branch information
derobins authored Mar 21, 2024
1 parent 8ecc984 commit 15608c0
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 2 deletions.
2 changes: 2 additions & 0 deletions hl/test/test_dset_append.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ test_dataset_append_notset(hid_t fid)
} /* end for */

/* File size when not flushed */
memset(&sb1, 0, sizeof(h5_stat_t));
if (HDstat(FILENAME, &sb1) < 0)
TEST_ERROR;

Expand All @@ -86,6 +87,7 @@ test_dataset_append_notset(hid_t fid)
FAIL_STACK_ERROR;

/* File size after flushing */
memset(&sb2, 0, sizeof(h5_stat_t));
if (HDstat(FILENAME, &sb2) < 0)
TEST_ERROR;

Expand Down
2 changes: 2 additions & 0 deletions src/H5FDcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ H5FD__core_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr
if (fa->backing_store) {
if ((fd = HDopen(name, o_flags | O_CREAT, H5_POSIX_CREATE_MODE_RW)) < 0)
HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to create file");
memset(&sb, 0, sizeof(h5_stat_t));
if (HDfstat(fd, &sb) < 0)
HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file");
} /* end if */
Expand All @@ -776,6 +777,7 @@ H5FD__core_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr
else if (fa->backing_store || !(H5F_ACC_CREAT & flags)) {
if ((fd = HDopen(name, o_flags, H5_POSIX_CREATE_MODE_RW)) < 0)
HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to open file");
memset(&sb, 0, sizeof(h5_stat_t));
if (HDfstat(fd, &sb) < 0)
HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file");
} /* end if */
Expand Down
1 change: 1 addition & 0 deletions src/H5FDdirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ H5FD__direct_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxad
if ((fd = HDopen(name, o_flags, H5_POSIX_CREATE_MODE_RW)) < 0)
HSYS_GOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to open file");

memset(&sb, 0, sizeof(h5_stat_t));
if (HDfstat(fd, &sb) < 0)
HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file");

Expand Down
1 change: 1 addition & 0 deletions src/H5FDlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ H5FD__log_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr)
H5_timer_start(&stat_timer);

/* Get the file stats */
memset(&sb, 0, sizeof(h5_stat_t));
if (HDfstat(fd, &sb) < 0)
HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file");

Expand Down
1 change: 1 addition & 0 deletions src/H5FDsec2.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ H5FD__sec2_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr
name, myerrno, strerror(myerrno), flags, (unsigned)o_flags);
} /* end if */

memset(&sb, 0, sizeof(h5_stat_t));
if (HDfstat(fd, &sb) < 0)
HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file");

Expand Down
1 change: 1 addition & 0 deletions src/H5FDsubfiling/H5FDioc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,7 @@ H5FD__ioc_del(const char *name, hid_t fapl)
char *prefix_env = NULL;
int num_digits = 0;

memset(&st, 0, sizeof(h5_stat_t));
if (HDstat(name, &st) < 0)
H5_SUBFILING_SYS_GOTO_ERROR(H5E_FILE, H5E_SYSERRSTR, FAIL, "HDstat failed");

Expand Down
1 change: 1 addition & 0 deletions src/H5FDsubfiling/H5FDioc_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,7 @@ ioc_file_report_eof(sf_work_request_t *msg, MPI_Comm comm)

fd = sf_context->sf_fids[subfile_idx];

memset(&sb, 0, sizeof(h5_stat_t));
if (HDfstat(fd, &sb) < 0)
H5_SUBFILING_SYS_GOTO_ERROR(H5E_FILE, H5E_SYSERRSTR, -1, "HDfstat failed");

Expand Down
1 change: 1 addition & 0 deletions src/H5FDsubfiling/H5subfiling_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ H5_open_subfiling_stub_file(const char *name, unsigned flags, MPI_Comm file_comm
HDcompile_assert(sizeof(uint64_t) >= sizeof(ino_t));

/* Retrieve Inode value for stub file */
memset(&st, 0, sizeof(h5_stat_t));
if (HDstat(name, &st) < 0) {
stub_file_id = UINT64_MAX;
H5_SUBFILING_GOTO_ERROR(H5E_VFL, H5E_CANTGET, FAIL,
Expand Down
3 changes: 3 additions & 0 deletions src/H5Fint.c
Original file line number Diff line number Diff line change
Expand Up @@ -2783,6 +2783,7 @@ H5F__build_actual_name(const H5F_t *f, const H5P_genplist_t *fapl, const char *n
h5_stat_t lst; /* Stat info from lstat() call */

/* Call lstat() on the file's name */
memset(&lst, 0, sizeof(h5_stat_t));
if (HDlstat(name, &lst) < 0)
HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "can't retrieve stat info for file");

Expand Down Expand Up @@ -2825,10 +2826,12 @@ H5F__build_actual_name(const H5F_t *f, const H5P_genplist_t *fapl, const char *n
HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "can't retrieve POSIX file descriptor");

/* Stat the filename we're resolving */
memset(&st, 0, sizeof(h5_stat_t));
if (HDstat(name, &st) < 0)
HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, FAIL, "unable to stat file");

/* Stat the file we opened */
memset(&fst, 0, sizeof(h5_stat_t));
if (HDfstat(*fd, &fst) < 0)
HSYS_GOTO_ERROR(H5E_FILE, H5E_BADFILE, FAIL, "unable to fstat file");

Expand Down
2 changes: 2 additions & 0 deletions src/H5PLpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ H5PL__path_table_iterate_process_path(const char *plugin_path, H5PL_iterate_type
snprintf(path, len, "%s/%s", plugin_path, dp->d_name);

/* Get info for directory entry */
memset(&my_stat, 0, sizeof(h5_stat_t));
if (HDstat(path, &my_stat) == -1)
HGOTO_ERROR(H5E_FILE, H5E_CANTGET, H5_ITER_ERROR, "can't stat file %s -- error was: %s", path,
strerror(errno));
Expand Down Expand Up @@ -882,6 +883,7 @@ H5PL__find_plugin_in_path(const H5PL_search_params_t *search_params, bool *found
snprintf(path, len, "%s/%s", dir, dp->d_name);

/* Get info for directory entry */
memset(&my_stat, 0, sizeof(h5_stat_t));
if (HDstat(path, &my_stat) == -1)
HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "can't stat file %s -- error was: %s", path,
strerror(errno));
Expand Down
1 change: 1 addition & 0 deletions test/big.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ is_sparse(void)
return 0;
if (HDclose(fd) < 0)
return 0;
memset(&sb, 0, sizeof(h5_stat_t));
if (HDstat("x.h5", &sb) < 0)
return 0;
if (HDremove("x.h5") < 0)
Expand Down
1 change: 1 addition & 0 deletions test/btree2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8665,6 +8665,7 @@ fprintf(stderr, "curr_time = %lu\n", (unsigned long)curr_time);
TEST_ERROR;

/* Retrieve the file's size */
memset(&sb, 0, sizeof(h5_stat_t));
if (HDfstat(fd, &sb) < 0)
TEST_ERROR;

Expand Down
3 changes: 3 additions & 0 deletions test/file_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ test_core(void)
/* Create file image buffer */
fd = HDopen(copied_filename, O_RDONLY);
VERIFY(fd > 0, "open failed");
memset(&sb, 0, sizeof(h5_stat_t));
ret = HDfstat(fd, &sb);
VERIFY(ret == 0, "fstat failed");
size = (size_t)sb.st_size;
Expand Down Expand Up @@ -705,6 +706,8 @@ test_get_file_image(const char *test_banner, const int file_name_num, hid_t fapl

TESTING(test_banner);

memset(&stat_buf, 0, sizeof(h5_stat_t));

/* set flag if we are dealing with a family file */
driver = H5Pget_driver(fapl);
VERIFY(driver >= 0, "H5Pget_driver(fapl) failed");
Expand Down
3 changes: 3 additions & 0 deletions test/h5test.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ h5_fixname_real(const char *base_name, hid_t fapl, const char *_suffix, char *fu
*/
h5_stat_t buf;

memset(&buf, 0, sizeof(h5_stat_t));
if (HDstat(fullname, &buf) < 0)
/* The directory doesn't exist just yet */
if (HDmkdir(fullname, (mode_t)0755) < 0 && errno != EEXIST)
Expand Down Expand Up @@ -1068,6 +1069,8 @@ h5_get_file_size(const char *filename, hid_t fapl)
h5_stat_t sb; /* Structure for querying file info */
int j = 0;

memset(&sb, 0, sizeof(h5_stat_t));

if (fapl == H5P_DEFAULT) {
/* Get the file's statistics */
if (0 == HDstat(filename, &sb))
Expand Down
1 change: 1 addition & 0 deletions test/istore.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ is_sparse(void)
return 0;
if (HDclose(fd) < 0)
return 0;
memset(&sb, 0, sizeof(h5_stat_t));
if (HDstat("x.h5", &sb) < 0)
return 0;
if (HDremove("x.h5") < 0)
Expand Down
1 change: 1 addition & 0 deletions test/tfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3262,6 +3262,7 @@ cal_chksum(const char *file, uint32_t *chksum)
CHECK(fdes, FAIL, "HDopen");

/* Retrieve the file's size */
memset(&sb, 0, sizeof(h5_stat_t));
ret = HDfstat(fdes, &sb);
CHECK(fdes, FAIL, "HDfstat");

Expand Down
6 changes: 5 additions & 1 deletion testpar/t_shapesame.c
Original file line number Diff line number Diff line change
Expand Up @@ -3984,15 +3984,19 @@ pause_proc(void)
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
MPI_Get_processor_name(mpi_name, &mpi_namelen);

if (MAINPROCESS)
if (MAINPROCESS) {
memset(&statbuf, 0, sizeof(h5_stat_t));
while ((HDstat(greenlight, &statbuf) == -1) && loops < maxloop) {
if (!loops++) {
printf("Proc %d (%*s, %d): to debug, attach %d\n", mpi_rank, mpi_namelen, mpi_name, pid, pid);
}
printf("waiting(%ds) for file %s ...\n", time_int, greenlight);
fflush(stdout);
HDsleep(time_int);

memset(&statbuf, 0, sizeof(h5_stat_t));
}
}
MPI_Barrier(MPI_COMM_WORLD);
}

Expand Down
Loading

0 comments on commit 15608c0

Please sign in to comment.