Skip to content

Writing to a file open with read permissions panics instead of returning error #3880

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

Closed
DanB91 opened this issue Dec 10, 2019 · 2 comments · Fixed by #5745
Closed

Writing to a file open with read permissions panics instead of returning error #3880

DanB91 opened this issue Dec 10, 2019 · 2 comments · Fixed by #5745
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@DanB91
Copy link

DanB91 commented Dec 10, 2019

Hi,

Running macOS Catalina and zig 0.5.0+5874cb04b.

On macOS Catalina, When calling f.write() on a file opened with File.openRead(), zig panics on EBADF, which in has a comment saying "always a race condition" and does not return an error. This is a single-threaded program. EBADF seems to be the proper error for writing to a file open for reading and is not a race condition:

[EBADF] fildes is not a valid file descriptor open for writing.

This seems to be related to this bug but not 100% sure.
Here is the error trace:

reached unreachable code
lib/zig/std/os.zig:497:22: 0x105207854 in _std.os.write
            EBADF => unreachable, // Always a race condition.
                     ^
lib/zig/std/fs/file.zig:296:24: 0x105203992 in _std.fs.file.File.write
        return os.write(self.handle, bytes);
                       ^
prog.zig:48:16: 0x105210cc3 in _main.0 
        f.write(bytes[0..]) 
               ^

Please let me know if you need anything else from me

@andrewrk andrewrk added the standard library This issue involves writing Zig code for the standard library. label Dec 31, 2019
@andrewrk andrewrk added this to the 0.7.0 milestone Dec 31, 2019
@andrewrk
Copy link
Member

The man pages say this:

   EBADF  fd is not a valid file descriptor or is not open for writing.

Unfortunately, EBADF is ambiguous. The "not a valid file descriptor" is always a race condition, but the "not open for writing" is not necessarily a race condition. It would be much better if the kernel gave us an error code to distinguish between these two things, but alas, here we are.

Because of the "or is not open for writing", I believe we need to change this from unreachable to return error.NotOpenForWriting,. This essentially "asserts" that the "not a valid file descriptor" never occurs. Unfortunately, we have no way to safety-check this assertion.

@andrewrk andrewrk added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. labels Dec 31, 2019
@pixelherodev
Copy link
Contributor

Proposal: on MacOS, add a bit to check what mode we opened the file in? Then, we can check whether we opened it for writing and return an error or unreachable based on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants