Skip to content

os: Users of path_converter don't handle fd == -1 properly #77068

Done

Description

@izbyshev
mannequin
BPO 32887
Nosy @larryhastings, @izbyshev

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2018-02-20.16:24:10.469>
labels = ['type-bug', '3.7']
title = "os: Users of path_converter don't handle fd == -1 properly"
updated_at = <Date 2018-02-20.16:28:20.371>
user = 'https://github.com/izbyshev'

bugs.python.org fields:

activity = <Date 2018-02-20.16:28:20.371>
actor = 'izbyshev'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2018-02-20.16:24:10.469>
creator = 'izbyshev'
dependencies = []
files = []
hgrepos = []
issue_num = 32887
keywords = []
message_count = 1.0
messages = ['312421']
nosy_count = 2.0
nosy_names = ['larry', 'izbyshev']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32887'
versions = ['Python 3.6', 'Python 3.7']

Activity

  1. izbyshev commented on Feb 20, 2018

    izbyshevmannequin
    MannequinAuthor
    Demo:
    >>> import os
    >>> os.chdir(-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [Errno 14] Bad address: -1
    >>> os.chdir(-2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [Errno 9] Bad file descriptor: -2

    Functions in os supporting either path or file descriptor argument (os.supports_fd) usually use the following code pattern to distinguish between those cases:

    if (path->fd != -1)
        result = fchdir(path->fd);
    else
        result = chdir(path->narrow);
    

    However, _fd_converter used by path_converter internally doesn't give any special meaning to -1 and allows any negative file descriptors. Therefore, if a user passes -1 to such function, path->narrow, which is NULL, will be used.

    I see two ways to fix this.

    1. Make some flag in path_t indicating that it should be treated as fd and make all users check that flag.
    2. Make _fd_converter raise an exception for negative descriptors.

    Also, I have to mention an inconsistency in reporting of bad descriptors. A handful of os functions uses fildes_converter for descriptors, which uses PyObject_AsFileDescriptor, which in turn is used in other places in Python as well (e.g. in fcntl module). PyObject_AsFileDescriptor raises a ValueError for negative descriptors instead of OSError raised by most os functions in this case.

    >>> os.fchdir(-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: file descriptor cannot be a negative integer (-1)
  2. added
    3.7 (EOL)
    type-bug
    on Feb 20, 2018
  3. transferred this issue fromon Apr 10, 2022
  4. added
    extension-modules
    on Nov 30, 2023
  5. maurycy commented on Dec 5, 2026

    @maurycy
    Contributor

    Fixed in 52c8efa (3.15)

    On macOS, os.chdir(-1), os.listdir(-1) and os.stat(-1) now all raise OSError with errno.EBADF. os.fchdir(-1) still raises the ValueError

    2026-12-05T15:06:58.185800000+0100 maurycy@gimel /Users/maurycy  % uv run --python 3.15 python -c 'import os
    for f in (os.chdir, os.listdir, os.stat, os.fchdir):
        try: f(-1)
        except (OSError, ValueError) as e: print(f.__name__, type(e).__name__, e)'
    chdir OSError [Errno 9] Bad file descriptor: -1
    listdir OSError [Errno 9] Bad file descriptor
    stat OSError [Errno 9] Bad file descriptor: -1
    fchdir ValueError file descriptor cannot be a negative integer (-1)
    2026-12-05T15:09:07.908943000+0100 maurycy@gimel /Users/maurycy  % 

    cc @StanFromIreland

  6. reopened this on Dec 5, 2026
  7. picnixz commented on Dec 5, 2026

    @picnixz
    Member

    I would suggest alignin macOS with the rest if possible, otherwise leave it as is.

  8. removed
    3.7 (EOL)
    on Dec 5, 2026
  9. maurycy commented on Dec 5, 2026

    @maurycy
    Contributor

    @picnixz Sorry for not being clear. macOS meant where I ran it. The behaviour on Linux is the same:

    2026-12-05T16:39:49.081800928+0000 maurycy@weiss /home/maurycy/cpython (main 2424c1d) % uname -a
    Linux weiss 6.17.7-300.fc43.aarch64 #1 SMP PREEMPT_DYNAMIC Sun Nov  2 15:33:04 UTC 2025 aarch64 GNU/Linux
    2026-12-05T16:39:49.507121254+0000 maurycy@weiss /home/maurycy/cpython (main 2424c1d) % uv run --python 3.15 python -c 'import os
    for f in (os.chdir, os.listdir, os.stat, os.fchdir):
        try: f(-1)
        except (OSError, ValueError) as e: print(f.__name__, type(e).__name__, e)'
    chdir OSError [Errno 9] Bad file descriptor: -1
    listdir OSError [Errno 9] Bad file descriptor
    stat OSError [Errno 9] Bad file descriptor: -1
    fchdir ValueError file descriptor cannot be a negative integer (-1)

    ValueError is platform-agnostic:

    if (fd < 0) {
    PyErr_Format(PyExc_ValueError,
    "file descriptor cannot be a negative integer (%i)",
    fd);
    return -1;
    }

    and intentional:

    #145439 (comment)

    I'd leave it as is and close

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulestype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions