Skip to content

Commit cd2414b

Browse files
authored
Fixed uninitialized memory issue in link table allocation. (#5936)
When H5G__dense_iterate failed early, the allocated H5O_link_t structures were never populated, causing cleanup code to read uninitialized lnk->type values and attempt to free invalid pointers. The structures are now initialized with proper values and NULL pointers immediately after allocation. Fixes GitHub #5375
1 parent 3266a56 commit cd2414b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/H5Gdense.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,13 @@ H5G__dense_build_table(H5F_t *f, const H5O_linfo_t *linfo, H5_index_t idx_type,
762762
H5G_dense_bt_ud_t udata; /* User data for iteration callback */
763763

764764
/* Allocate the table to store the links */
765-
if ((ltable->lnks = (H5O_link_t *)H5MM_malloc(sizeof(H5O_link_t) * ltable->nlinks)) == NULL)
765+
if ((ltable->lnks = (H5O_link_t *)H5MM_calloc(sizeof(H5O_link_t) * ltable->nlinks)) == NULL)
766766
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
767767

768+
/* Initialize all links to invalid. NOTE: If H5O_link_t changes, update this loop. */
769+
for (size_t i = 0; i < ltable->nlinks; i++)
770+
ltable->lnks[i].type = H5L_TYPE_ERROR;
771+
768772
/* Set up user data for iteration */
769773
udata.ltable = ltable;
770774
udata.curr_lnk = 0;

0 commit comments

Comments
 (0)