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

Fix Issue 19751 - std.stdio.File should not retry fclose after error #6913

Merged
merged 1 commit into from Mar 19, 2019

Conversation

CyberShadow
Copy link
Member

C fclose is allowed to fail, and some libc implementations do cause it to fail.

For example, reading on a file opened only for writing, or (in Wine's MSVCRT implementation) writing on a file opened only for reading sets an error flag, which then causes fclose to return EOF to signal an error.

However, all descriptions of the function specify that "whether or not the call succeeds, the stream shall be disassociated from the file" and "after the call to fclose(), any use of stream results in undefined behavior" (POSIX) or similar.

Currently, if File.close throws and is called again (e.g. in a destructor), it will retry the fclose call. This is undefined behavior as described above, and may potentially affect an unrelated file.

It also makes it overly difficult to "get rid" of a File instance associated with a FILE with its error flag set - all attempts at destroying it will fail and only be eventually retried.

C fclose is allowed to fail, and some libc implementations do cause it
to fail.

For example, reading on a file opened only for writing, or (in Wine's
MSVCRT implementation) writing on a file opened only for reading sets
an error flag, which then causes fclose to return EOF to signal an
error.

However, all descriptions of the function specify that "whether or not
the call succeeds, the stream shall be disassociated from the file"
and "after the call to fclose(), any use of stream results in
undefined behavior" (POSIX) or similar.

Currently, if File.close throws and is called again (e.g. in a
destructor), it will retry the fclose call. This is undefined behavior
as described above, and may potentially affect an unrelated file.

It also makes it overly difficult to "get rid" of a File instance
associated with a FILE with its error flag set - all attempts at
destroying it will fail and only be eventually retried.
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @CyberShadow!

Bugzilla references

Auto-close Bugzilla Severity Description
19751 minor std.stdio.File should not retry fclose after error

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + phobos#6913"

@CyberShadow
Copy link
Member Author

CyberShadow commented Mar 18, 2019

No tests because error behavior on fclose is implementation defined ... GNU libc fclose seems to never fail after invalid operations.

Might add CRuntime_Microsoft unittests but that's also kind of tenuous (we don't target a specific version of the runtime).

@dlang-bot dlang-bot merged commit 28fc0f4 into dlang:master Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants