gh-145335: validate file descriptor in os.fpathconf and os.pathconf#145341
Closed
gourijain029-del wants to merge 1 commit intopython:mainfrom
Closed
gh-145335: validate file descriptor in os.fpathconf and os.pathconf#145341gourijain029-del wants to merge 1 commit intopython:mainfrom
gourijain029-del wants to merge 1 commit intopython:mainfrom
Conversation
|
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 |
Member
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)).
os.pathconf(-1, 1)#145335