Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions Lib/test/test_os/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2778,6 +2778,16 @@ def test_fpathconf_bad_fd(self):
self.check(os.pathconf, "PC_NAME_MAX")
self.check(os.fpathconf, "PC_NAME_MAX")

@unittest.skipUnless(hasattr(os, 'pathconf'), 'test needs os.pathconf()')
@unittest.skipIf(
support.linked_to_musl(),
'musl pathconf ignores the file descriptor and returns a constant',
Comment thread
aisk marked this conversation as resolved.
Outdated
)
def test_pathconf_negative_fd_uses_fd_semantics(self):
with self.assertRaises(OSError) as ctx:
os.pathconf(-1, 1)
self.assertEqual(ctx.exception.errno, errno.EBADF)

@unittest.skipUnless(hasattr(os, 'ftruncate'), 'test needs os.ftruncate()')
def test_ftruncate(self):
self.check(os.truncate, 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :func:`os.pathconf` when called with ``-1`` as the path
argument.
9 changes: 8 additions & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,12 @@ follow_symlinks_specified(const char *function_name, int follow_symlinks)
return 1;
}

static bool
path_is_fd(const path_t *path)
{
return path->allow_fd && path->object != NULL && PyIndex_Check(path->object);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the problem really with the check or with is it with the converter?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

edit: nvm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of checking path->object type (PyIndex_Check()), I would prefer adding a path_t.is_fd member and modify path_converter() to set this member.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated.

}

static int
path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
{
Expand Down Expand Up @@ -14328,8 +14334,9 @@ os_pathconf_impl(PyObject *module, path_t *path, int name)

errno = 0;
#ifdef HAVE_FPATHCONF
if (path->fd != -1)
if (path_is_fd(path)) {
limit = fpathconf(path->fd, name);
}
else
#endif
limit = pathconf(path->narrow, name);
Expand Down
Loading