Skip to content

Commit

Permalink
Clean up memory allocated when reading messages in H5Dlayout on error (
Browse files Browse the repository at this point in the history
  • Loading branch information
glennsong09 authored Apr 11, 2023
1 parent 2eedc8e commit 367e4a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions release_docs/RELEASE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ Bug Fixes since HDF5-1.13.3 release
===================================
Library
-------
- Fixed memory leaks that could occur when reading a dataset from a
malformed file

When attempting to read layout, pline, and efl information for a
dataset, memory leaks could occur if attempting to read pline/efl
information threw an error, which is due to the memory that was
allocated for pline and efl not being properly cleaned up on error.

(GS - 2023/4/11 GH#2602)

- Fixed potential heap buffer overrun in group info header decoding from malformed file

H5O__ginfo_decode could sometimes read past allocated memory when parsing a group info message from the header of a malformed file.
Expand Down
20 changes: 15 additions & 5 deletions src/H5Dlayout.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,9 @@ herr_t
H5D__layout_oh_read(H5D_t *dataset, hid_t dapl_id, H5P_genplist_t *plist)
{
htri_t msg_exists; /* Whether a particular type of message exists */
hbool_t pline_copied = FALSE; /* Flag to indicate that dcpl_cache.pline's message was copied */
hbool_t layout_copied = FALSE; /* Flag to indicate that layout message was copied */
hbool_t efl_copied = FALSE; /* Flag to indicate that the EFL message was copied */
herr_t ret_value = SUCCEED; /* Return value */

FUNC_ENTER_PACKAGE
Expand All @@ -605,7 +607,7 @@ H5D__layout_oh_read(H5D_t *dataset, hid_t dapl_id, H5P_genplist_t *plist)
/* Retrieve the I/O pipeline message */
if (NULL == H5O_msg_read(&(dataset->oloc), H5O_PLINE_ID, &dataset->shared->dcpl_cache.pline))
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "can't retrieve message")

pline_copied = TRUE;
/* Set the I/O pipeline info in the property list */
if (H5P_set(plist, H5O_CRT_PIPELINE_NAME, &dataset->shared->dcpl_cache.pline) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTSET, FAIL, "can't set pipeline")
Expand All @@ -628,6 +630,7 @@ H5D__layout_oh_read(H5D_t *dataset, hid_t dapl_id, H5P_genplist_t *plist)
/* Retrieve the EFL message */
if (NULL == H5O_msg_read(&(dataset->oloc), H5O_EFL_ID, &dataset->shared->dcpl_cache.efl))
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "can't retrieve message")
efl_copied = TRUE;

/* Set the EFL info in the property list */
if (H5P_set(plist, H5D_CRT_EXT_FILE_LIST_NAME, &dataset->shared->dcpl_cache.efl) < 0)
Expand Down Expand Up @@ -659,10 +662,17 @@ H5D__layout_oh_read(H5D_t *dataset, hid_t dapl_id, H5P_genplist_t *plist)
HGOTO_ERROR(H5E_DATASET, H5E_BADVALUE, FAIL, "unable to set chunk sizes")

done:
if (ret_value < 0 && layout_copied)
if (H5O_msg_reset(H5O_LAYOUT_ID, &dataset->shared->layout) < 0)
HDONE_ERROR(H5E_DATASET, H5E_CANTRESET, FAIL, "unable to reset layout info")

if (ret_value < 0) {
if (pline_copied)
if (H5O_msg_reset(H5O_PLINE_ID, &dataset->shared->dcpl_cache.pline) < 0)
HDONE_ERROR(H5E_DATASET, H5E_CANTRESET, FAIL, "unable to reset pipeline info")
if (layout_copied)
if (H5O_msg_reset(H5O_LAYOUT_ID, &dataset->shared->layout) < 0)
HDONE_ERROR(H5E_DATASET, H5E_CANTRESET, FAIL, "unable to reset layout info")
if (efl_copied)
if (H5O_msg_reset(H5O_EFL_ID, &dataset->shared->dcpl_cache.efl) < 0)
HDONE_ERROR(H5E_DATASET, H5E_CANTRESET, FAIL, "unable to reset efl message")
}
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5D__layout_oh_read() */

Expand Down

0 comments on commit 367e4a3

Please sign in to comment.