Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/host_env/src/fileutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub use windows::{StatStruct, fstat};

#[cfg(not(windows))]
pub fn fstat(fd: crate::crt_fd::Borrowed<'_>) -> std::io::Result<StatStruct> {
// Unlike a directory fd, fstat accepts no negative descriptor sentinels.
if fd.as_raw() < 0 {
return Err(std::io::Error::from_raw_os_error(libc::EBADF));
}
rustix::fs::fstat(fd).map_err(Into::into)
}

Expand Down
20 changes: 20 additions & 0 deletions extra_tests/snippets/stdlib_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@

assert os.name == "posix" or os.name == "nt"


def stat_rejects_negative_file_descriptors():
import errno
import os

for stat in (os.stat, os.fstat):
for fd in (-1, -5, -9, -100, -(2**31)):
Comment on lines +16 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove the prohibited test logic

These added loops introduce new test logic and data in extra_tests/snippets/stdlib_os.py, but the root test-code policy says not to modify test assertions, logic, or data and limits acceptable test-file changes to expected-failure decorator updates. Remove this regression-test addition while retaining the Rust fix so the change complies with the repository policy.

AGENTS.md reference: AGENTS.md:L275-L279

Useful? React with 👍 / 👎.

try:
stat(fd)
except OSError as exc:
assert exc.errno == errno.EBADF, (stat, fd, exc)
else:
raise AssertionError((stat, fd, "accepted a negative file descriptor"))


# Keep the original RustPython-only coverage: CPython treats -1 as a path sentinel.
if os.name == "posix" and sys.implementation.name == "rustpython":
stat_rejects_negative_file_descriptors()


fd = os.open("README.md", os.O_RDONLY)
assert fd > 0

Expand Down
Loading