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

ftruncate has unreachable error values that can be reached #22960

Open
Daimanta opened this issue Feb 20, 2025 · 2 comments
Open

ftruncate has unreachable error values that can be reached #22960

Daimanta opened this issue Feb 20, 2025 · 2 comments
Labels
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

@Daimanta
Copy link

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 an unreachable

Expected Behavior

From the manual page on man7.org

`The ftruncate() function shall fail if:

   EINTR  A signal was caught during execution.

   EINVAL The length argument was less than 0.

   EFBIG or EINVAL
          The length argument was greater than the maximum file size.

   EFBIG  The file is a regular file and length is greater than the
          offset maximum established in the open file description
          associated with fildes.

   EIO    An I/O error occurred while reading from or writing to a
          file system.

   EBADF or EINVAL
          The fildes argument is not a file descriptor open for
          writing.

`

As shown, there are three possible cases where an EINVAL is returned. The zig type system prevents negative length(passed type is u64), so we can ignore that. As the ftruncate 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 trigger EINVAL is by a length exceeding maximum file size. Therefore .INVAL should do the following: return error.FileTooBig, mirroring .FBIG behavior.

@Daimanta Daimanta added the bug Observed behavior contradicts documented or intended behavior label Feb 20, 2025
@alexrp alexrp added standard library This issue involves writing Zig code for the standard library. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. labels Feb 20, 2025
@alexrp
Copy link
Member

alexrp commented Feb 20, 2025

In principle you're supposed to be able to know the maximum file size by way of pathconf("/path/to/file", FILESIZEBITS). However, a mount change could obviously result in that value changing between the calls to pathconf and ftruncate, making such a check unreliable, so I think it's indeed fair to consider this an expected error.

@alexrp alexrp added this to the unplanned milestone Feb 20, 2025
@lsculv
Copy link
Contributor

lsculv commented Feb 22, 2025

#20524 is a related issue regarding "unexpected" error returns from POSIX functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

No branches or pull requests

3 participants