[Bug 218830] lseek on closed file does not trigger an error and affect other files

bugzilla-daemon
2024-05-13T13:24:07Z

Theodore Tso (tytso@mit.edu) changed:

What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
CC| |tytso@mit.edu
Resolution|--- |INVALID

--- Comment #1 from Theodore Tso (tytso@mit.edu) ---
This is a test/programming bug. If you change reproduce.c so that it prints
fd_a and fd_b, you'll see that they have the same value. So the reason why
lseek didn't fail is because fd_a has the same integer value as fd_b --- and so
lseek didn't fail and affected the current position of fd_b.

This is a documented feature of the Linux/Unix/Posix interface. File
descriptors are small integers, and if you close a file descriptor, it releases
that integer ---- think of it as an index in an array, i.e., struct file
*fd_array[MAX_FDS]. When you call open, it will find the first NULL pointer
in fd_array, and installs a pointer to the struct file, and returns that
integer as the file descriptor.