Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows implementation of os.waitpid() truncates the exit status (status << 8) #84319

Closed
vstinner opened this issue Apr 1, 2020 · 5 comments
Closed
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

vstinner commented Apr 1, 2020

BPO 40138
Nosy @vstinner, @eryksun, @miss-islington
PRs
  • bpo-40138: Fix Windows os.waitpid() for large exit code #19637
  • [3.8] bpo-40138: Fix Windows os.waitpid() for large exit code #19654
  • [3.7] bpo-40138: Fix Windows os.waitpid() for large exit code (GH-19654) #19655
  • 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 = <Date 2020-04-22.16:35:49.057>
    created_at = <Date 2020-04-01.17:05:27.666>
    labels = ['3.8', '3.7', 'library', '3.9']
    title = 'Windows implementation of os.waitpid() truncates the exit status (status << 8)'
    updated_at = <Date 2020-04-22.16:35:49.055>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2020-04-22.16:35:49.055>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-04-22.16:35:49.057>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2020-04-01.17:05:27.666>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40138
    keywords = ['patch']
    message_count = 5.0
    messages = ['365498', '367007', '367012', '367016', '367019']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'eryksun', 'miss-islington']
    pr_nums = ['19637', '19654', '19655']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue40138'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @vstinner
    Copy link
    Member Author

    vstinner commented Apr 1, 2020

    On Windows, the exit code is a 32-bit value. It may or may not signed depending on the function.

    Unsigned in the Windows native API:

    BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode);
    BOOL GetExitCodeProcess(HANDLE hProcess, LPDWORD lpExitCode);

    Signed in the POSIX API:

    intptr_t _cwait(int *termstat, intptr_t procHandle, int action);

    Problem: os.waitpid() uses "status << 8" which can overflow; status is an int.

    static PyObject *
    os_waitpid_impl(PyObject *module, intptr_t pid, int options)
    {
        int status;
        (...)
        /* shift the status left a byte so this is more like the POSIX waitpid */
        return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
    }

    int64_t or uint64_t should be used, or a Python object should be used, to avoid the overflow.

    I just added os.waitstatus_to_exitcode() in bpo-40094 which simply does "status >> 8" on Windows. Currently, this function casts the argument to a C int and so is limited to INT_MAX. It should also be adapted to handle values larger than INT_MAX.

    By the way, I'm not sure how to handle values larger than INT_MAX. The POSIX API of Windows uses a signed integer, and so convert such value as a negative value. But the native Windows API uses unsigned numbers.

    It seems like using unsigned number would be better.

    --

    By the way, currently os.waitstatus_to_exitcode() ignores the lower 8 bits of the status. Maybe it should raise an error if lower 8 bits are not zero, and maybe also raise an exception if the number is negative?

    --

    See also interesting comments by Eryk Sun in bpo-40094 about this problem.

    @vstinner vstinner added 3.9 only security fixes stdlib Python modules in the Lib dir labels Apr 1, 2020
    @vstinner
    Copy link
    Member Author

    New changeset 9bee32b by Victor Stinner in branch 'master':
    bpo-40138: Fix Windows os.waitpid() for large exit code (GH-19637)
    9bee32b

    @vstinner
    Copy link
    Member Author

    New changeset b073509 by Victor Stinner in branch '3.8':
    bpo-40138: Fix Windows os.waitpid() for large exit code (GH-19654)
    b073509

    @miss-islington
    Copy link
    Contributor

    New changeset de5dcfa by Miss Islington (bot) in branch '3.7':
    bpo-40138: Fix Windows os.waitpid() for large exit code (GH-19654)
    de5dcfa

    @vstinner
    Copy link
    Member Author

    Ok, os.waitpid() is now fixed in 3.7, 3.8 and master branches, and os.waitstatus_to_exitcode() is fixed in master.

    @vstinner vstinner added 3.7 (EOL) end of life 3.8 only security fixes labels Apr 22, 2020
    @vstinner vstinner added 3.7 (EOL) end of life 3.8 only security fixes labels Apr 22, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants