Skip to content

Commit ea4b483

Browse files
Unlink file free space section on failure to update data structures (#5815)
When linking a file free space section into a free space manager's internal data structures, the library previously wouldn't unlink the free space section when it failed to update the free space manager's internal data structures. This could eventually result in a use-after-free issue due to the stale reference kept around.
1 parent 957a509 commit ea4b483

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/H5FSsection.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,8 +1057,9 @@ H5FS__sect_link_rest(H5FS_t *fspace, const H5FS_section_class_t *cls, H5FS_secti
10571057
static herr_t
10581058
H5FS__sect_link(H5FS_t *fspace, H5FS_section_info_t *sect, unsigned flags)
10591059
{
1060-
const H5FS_section_class_t *cls; /* Class of section */
1061-
herr_t ret_value = SUCCEED; /* Return value */
1060+
const H5FS_section_class_t *cls; /* Class of section */
1061+
bool linked_sect = false; /* Was the section linked in? */
1062+
herr_t ret_value = SUCCEED; /* Return value */
10621063

10631064
FUNC_ENTER_PACKAGE
10641065

@@ -1073,13 +1074,20 @@ H5FS__sect_link(H5FS_t *fspace, H5FS_section_info_t *sect, unsigned flags)
10731074
/* Add section to size tracked data structures */
10741075
if (H5FS__sect_link_size(fspace->sinfo, cls, sect) < 0)
10751076
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't add section to size tracking data structures");
1077+
linked_sect = true;
10761078

10771079
/* Update rest of free space manager data structures for section addition */
10781080
if (H5FS__sect_link_rest(fspace, cls, sect, flags) < 0)
10791081
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL,
10801082
"can't add section to non-size tracking data structures");
10811083

10821084
done:
1085+
if (ret_value < 0) {
1086+
if (linked_sect && H5FS__sect_unlink_size(fspace->sinfo, cls, sect) < 0)
1087+
HDONE_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL,
1088+
"can't remove section from size tracking data structures");
1089+
}
1090+
10831091
FUNC_LEAVE_NOAPI(ret_value)
10841092
} /* H5FS__sect_link() */
10851093

0 commit comments

Comments
 (0)