GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — os.dup2() raises wrong OSError for negative fds · Issue #102179 · python/cpython

os.dup2() raises wrong OSError for negative fds #102179

New issue
Copy link
New issue
Copy link
Closed
Closed
os.dup2() raises wrong OSError for negative fds#102179
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@izbyshev
izbyshev
opened on Feb 23, 2023
Issue body actions
>>> os.dup2(-1, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  OSError: [Errno 0] Error

The bug is caused by the old manual sign check that at some point lost the part that set errno:

cpython/Modules/posixmodule.c

Lines 9832 to 9834 in c3a1783

if (fd < 0 || fd2 < 0) {
posix_error();
return -1;

I'm going to submit a PR with the fix.

Linked PRs

  • gh-102179: Fix os.dup2() error reporting for negative fds #102180
  • [3.10] gh-102179: Fix os.dup2 error reporting for negative fds (GH-102180) #102419
  • [3.11] gh-102179: Fix os.dup2 error reporting for negative fds (GH-102180) #102420

Activity

  1. izbyshev
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 23, 2023
  2. izbyshev
    added 2 commits that reference this issue on Feb 23, 2023

    pythongh-102179: Fix os.dup2() error reporting for negative fds

    ae24b9e

    pythongh-102179: Fix os.dup2() error reporting for negative fds

    411107d
  3. arhadthedev
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Feb 25, 2023
  4. izbyshev commented on Mar 4, 2023

    @izbyshev
    izbyshev
    on Mar 4, 2023
    ContributorAuthor
    More actions

    It turned out that on wasm32-emscripten dup2() for negative fds is currently broken. I could reproduce that with the C equivalent of the failed test case and the latest emsdk:

    $ ./emsdk list | grep INSTALLED
             3.1.32             INSTALLED
             3.1.32    INSTALLED
        *    sdk-releases-29ad1037cd6b99e5d8a1bd75bc188c1e9a6fda8d-64bit    INSTALLED
        (*)    releases-29ad1037cd6b99e5d8a1bd75bc188c1e9a6fda8d-64bit      INSTALLED
        (*)    node-14.18.2-64bit           INSTALLED
    $ cat t.c
    #include <errno.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    
    int main() {
            int valid_fd = open(".", O_RDONLY);
            if (valid_fd < 0) {
                    perror("open");
                    return 1;
            }
            int fds[] = {
                    valid_fd,
                    -1,
                    INT_MIN,
            };
            for (int i = 0; i < sizeof fds / sizeof *fds; i++)
                    for (int j = 0; j < sizeof fds / sizeof *fds; j++)
                            if (i != j) {
                                    int fd = fds[i], fd2 = fds[j];
                                    printf("dup2(%d, %d): ", fd, fd2);
                                    errno = 0;
                                    if (dup2(fd, fd2) < 0) {
                                            printf("%d (%s)\n", errno, strerror(errno));
                                    } else {
                                            printf("succeeded\n");
                                    }
                            }
    }
    
    $ emcc t.c
    $ node a.out.js
    dup2(3, -1): 1 (Argument list too long)
    dup2(3, -2147483648): 0 (No error information)
    dup2(-1, 3): succeeded
    dup2(-1, -2147483648): 0 (No error information)
    dup2(-2147483648, 3): succeeded
    dup2(-2147483648, -1): 1 (Argument list too long)
    

    Apparently, in Emscripten there is ongoing work on WASMFS which fixed this particular issue, and comments in this PR additionally confirm that dup2() is broken in the current default FS implementation.

    Indeed, compiling with -sWASMFS makes the test work as expected:

    $ emcc -sWASMFS t.c
    $ node a.out.js
    dup2(3, -1): 8 (Bad file descriptor)
    dup2(3, -2147483648): 8 (Bad file descriptor)
    dup2(-1, 3): 8 (Bad file descriptor)
    dup2(-1, -2147483648): 8 (Bad file descriptor)
    dup2(-2147483648, 3): 8 (Bad file descriptor)
    dup2(-2147483648, -1): 8 (Bad file descriptor)
    

    Given the current state of Emscripten, I don't think we should care that os.dup2() will start to behave differently after removing the manual fd check in CPython. I propose to simply skip the failed test case on Emscripten.

  5. kumaraditya303
    added a commit that references this issue on Mar 4, 2023

    gh-102179: Fix `os.dup2` error reporting for negative fds (#102180)

    c2bd55d
  6. miss-islington
    added 3 commits that reference this issue on Mar 4, 2023

    pythongh-102179: Fix `os.dup2` error reporting for negative fds (pyth…

    9f6df8c

    pythongh-102179: Fix `os.dup2` error reporting for negative fds (pyth…

    4ae023d

    gh-102179: Fix `os.dup2` error reporting for negative fds (GH-102180)

    cc6ce90
  7. kumaraditya303
    added a commit that references this issue on Mar 4, 2023

    [3.10] gh-102179: Fix `os.dup2` error reporting for negative fds (GH-…

    925ebfb
  8. kumaraditya303
    closed this as completedon Mar 4, 2023
  9. hugovk
    added a commit that references this issue on Mar 6, 2023

    pythongh-102179: Fix `os.dup2` error reporting for negative fds (pyth…

    1e09181
  10. carljm
    added 2 commits that reference this issue on Mar 6, 2023

    Merge branch 'main' into nodeaddict

    33dda2a

    Merge branch 'main' into inlinecomp2

    ecb313c
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-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.