-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
ftruncate
has unreachable error values that can be reached
#22960
Comments
In principle you're supposed to be able to know the maximum file size by way of |
#20524 is a related issue regarding "unexpected" error returns from POSIX functions. |
I think this is an issue of negative length, although in zig this is a u64, the int ftruncate(int fildes, off_t length) (man 3p ftruncate) So I would think the zig function should take something like a u63 so that it does not overflow to a negative number when the number is too large, and thus it won't be invalid anymore. |
Add a test for std.fs.File's `setEndPos` (which is a simple wrapper around `std.posix.ftruncate`) to exercise some success and failure paths. Add errno handling to Windows path to map INVALID_PARAMETER to FileTooBig. Fixes ziglang#22960
@elijahimmer is correct, the Annoyingly, Linux will return Writing unit tests that exercise "unreachable" behavior is impossible (one cannot implement The Oddly, on Wine, the NtSetFileInformation call fails with The "fix" here to me isn't obvious. I see a bunch of options:
I have a change that adds some unit tests and fixes the Windows error path: #23260. It otherwise leaves the behavior unchanged. Feel free to comment here or there if you'd like to make a case for a different change. (I'm not a Zig core team member, so I don't have any particular standing to make rulings about how to address this.) |
Another option is to just check for size values greater than Although I do think having the type system help avoid theses edge cases could be good, it would be a very large change to the posix layer that I would imagine needs a core team member to approve of. |
I like this idea! It keeps the length a |
Add a test for std.fs.File's `setEndPos` (which is a simple wrapper around `std.posix.ftruncate`) to exercise some success and failure paths. Explicitly check that the `ftruncate` length isn't negative when interpreted as a signed value. This avoids having to decode overloaded `EINVAL` errors. Add errno handling to Windows path to map INVALID_PARAMETER to FileTooBig. Fixes ziglang#22960
Add a test for std.fs.File's `setEndPos` (which is a simple wrapper around `std.posix.ftruncate`) to exercise some success and failure paths. Explicitly check that the `ftruncate` length isn't negative when interpreted as a signed value. This avoids having to decode overloaded `EINVAL` errors. Add errno handling to Windows path to map INVALID_PARAMETER to FileTooBig. Fixes ziglang#22960
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
On Linux:
ftruncate(<my_valid_handle>, <max u64>)
This triggers the
.INVAL
error has which triggers anunreachable
Expected Behavior
From the manual page on man7.org
`The ftruncate() function shall fail if:
`
As shown, there are three possible cases where an
EINVAL
is returned. The zig type system prevents negative length(passed type isu64
), so we can ignore that. As theftruncate
function indicates// Handle not open for writing
at.INVAL
I can assume that is not possible. If that is the case, the only way to triggerEINVAL
is by a length exceeding maximum file size. Therefore.INVAL
should do the following:return error.FileTooBig
, mirroring.FBIG
behavior.The text was updated successfully, but these errors were encountered: