Fix memory leak and segfault from out-of-order NC file closure - #888
Open
bmribler wants to merge 2 commits into
Open
Fix memory leak and segfault from out-of-order NC file closure#888bmribler wants to merge 2 commits into
bmribler wants to merge 2 commits into
Conversation
_curr_opened and _ncdf track open files but diverge when files are closed out of order: _curr_opened correctly reaches 0, while _ncdf (high-water mark of _cdfs) does not. A prior fix changed the teardown check from `_curr_opened == 0` to `_ncdf == 0`, which then never fired in this case, leaking the _cdfs allocation (81,896 bytes) and leaving stale handles that bypassed validation and caused an out-of-bounds access / segfault on reuse. - Revert ncclose/ncabort to check `_curr_opened == 0` - Reset _ncdf and _curr_opened to 0 in ncreset_cdflist on teardown
bmribler
requested review from
brtnfld,
byrnHDF,
derobins,
fortnern,
glennsong09,
jhendersonHDF,
lrknox,
mattjala,
schwehr and
vchoi-hdfgroup
as code owners
August 1, 2026 03:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When opened files were closed in different order than when they were created, segfault and memory leaks occurred.
The global variables
static int _curr_opened = 0;/* the number of files currently opened */static int _ncdf = 0;/* high water mark on open cdf's */are used to track open files, but they diverge under out-of-order closure:
_curr_openedcorrectly drops to0, while_ncdfdoes not. A prior memory leak fix changed the teardown check from_curr_opened == 0to_ncdf == 0, so teardownnever fires in this case — leaking the
_cdfsallocation and leaving stale handles that skip validation, causing a segfault later.This PR reverts the previous change to check
_curr_opened == 0and resets_ncdfand_curr_openedto 0 inncreset_cdfliston teardown, which would have been correct in the previous fix.The fix has been verified with an out-of-order LIFO file-closure program under Valgrind.