Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Close stream first, close fd only if not stream #213

Closed
wants to merge 1 commit into from

Conversation

ddeclerck
Copy link
Contributor

This PR attemps to fix a debug assertion occuring under the MSVC CI (Assertion failed: (_osfile(fh) & FOPEN)).

Apparently, when a stream is created from a file descriptor via fdopen, it should always be closed by fclose. This is not always the case in the 4.x branch. While this seems to fail silently on most OSes, it triggers a debug assertion under MSVC debug.

This proposed fix just always tries to close the stream if it is not NULL, otherwise it just closes the file descriptor.
This fixes 40 (!) tests under MSVC debug.

References:

@ddeclerck ddeclerck requested a review from GitMensch January 28, 2025 20:00
Copy link
Collaborator

@GitMensch GitMensch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this seems to be no issue in 3.x and if valgrind will report more leaks afterwards.
In any case, please adjust that first "if" (and therefore re-indent the not-yet changed code).

libcob/fileio.c Outdated Show resolved Hide resolved
libcob/fileio.c Outdated Show resolved Hide resolved
@ddeclerck
Copy link
Contributor Author

I wonder why this seems to be no issue in 3.x

Because in 3.x cob_fd_file_open never creates streams from the given file descriptors, and so we have the invariant that only (and all) LS files have a stream. The cleanup code for LS files always tries to close the stream, while the cleanup code for non-LS files always tries to close the file descriptor.

In 4.x cob_fd_file_open does create streams from the given file descriptors, and the cleanup code ends up directly closing those file descriptors.

@ddeclerck
Copy link
Contributor Author

I wonder if valgrind will report more leaks afterwards

So, after checking, no change in the number of leaks.

@GitMensch
Copy link
Collaborator

this means that you can drop your "should" comment along with adjusting that if, then push the result upstream

@ddeclerck
Copy link
Contributor Author

Merged in SVN @ 5442.

@ddeclerck ddeclerck closed this Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants