Skip to content

gh-145335: validate file descriptor in os.fpathconf and os.pathconf#145341

Closed
gourijain029-del wants to merge 1 commit intopython:mainfrom
gourijain029-del:bugfix/os-pathconf-fd-validation
Closed

gh-145335: validate file descriptor in os.fpathconf and os.pathconf#145341
gourijain029-del wants to merge 1 commit intopython:mainfrom
gourijain029-del:bugfix/os-pathconf-fd-validation

Conversation

@gourijain029-del
Copy link

@gourijain029-del gourijain029-del commented Feb 28, 2026

Problem
Calling os.fpathconf(-1, name) or os.pathconf(-1, name) on macOS (and other platforms) caused a segmentation fault. The CPython implementation passed the negative file descriptor straight to the underlying

fpathconf
system call, which is undefined behavior and crashes the interpreter.

Fix

Added a guard in

Modules/posixmodule.c
(

os_fpathconf_impl
) that checks fd < 0.
When the descriptor is negative, the function now raises a clear ValueError with the message “file descriptor cannot be negative” and returns -1 to signal the error.
The existing error‑handling path (errno handling) remains unchanged for valid descriptors.
Regression Test

Introduced

Lib/test/test_os_pathconf.py
which verifies that both os.fpathconf(-1, 1) and os.pathconf(-1, 1) raise ValueError with the expected message.
The test runs as part of the standard CPython test suite, ensuring the fix stays in place.
Impact

Eliminates a hard crash for a common misuse pattern.
Provides a Python‑level exception that developers can catch, matching the behaviour of other os.* functions that validate file descriptors.
No functional changes for valid descriptors; all existing semantics are preserved.
Related Issue

Fixes issue #145335 (segfault from os.pathconf(-1, 1)).

@bedevere-app
Copy link

bedevere-app bot commented Feb 28, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland StanFromIreland changed the title Fix #145335 – validate file descriptor in os.fpathconf and os.pathconf gh-145335: validate file descriptor in os.fpathconf and os.pathconf Feb 28, 2026
@StanFromIreland
Copy link
Member

Added a guard in

You've done a whole lot more than that, there are so many changes the diff won't even render. And furthermore, your new test (which should have been added to an existing file) fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants