Skip to content

socket.if_nametoindex('no-such-iface') raises OSError() without setting errno #100218

Closed

Description

@socketpair

Activity

  1. changed the title [-]socket.if_nametoindex('no-such-iface') raises OSError() wothout setting errno[/-] [+]socket.if_nametoindex('no-such-iface') raises OSError() without setting errno[/+] on Dec 13, 2022
  2. sobolevn commented on Dec 13, 2022

    @sobolevn
    Member

    There's a comment in the source code: /* if_nametoindex() doesn't set errno */

  3. socketpair commented on Dec 13, 2022

    @socketpair
    ContributorAuthor

    Then it should not be OSError or it has to set some errno, like ENOENT or so.

    static PyObject *
    socket_if_nametoindex(PyObject *self, PyObject *args)
    {
        PyObject *oname;
    #ifdef MS_WINDOWS
        NET_IFINDEX index;
    #else
        unsigned long index;
    #endif
        if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
                              PyUnicode_FSConverter, &oname))
            return NULL;
    
        index = if_nametoindex(PyBytes_AS_STRING(oname));
        Py_DECREF(oname);
        if (index == 0) {
            /* if_nametoindex() doesn't set errno */
            PyErr_SetString(PyExc_OSError, "no interface with this name");
            return NULL;
        }
    
        return PyLong_FromUnsignedLong(index);
    }

    Another example from the same file:

        if (h->h_addrtype != af) {
            /* Let's get real error message to return */
            errno = EAFNOSUPPORT;
            PyErr_SetFromErrno(PyExc_OSError);
            return NULL;
        }

    I propose the same change. i.e. errno= ENOENT + PyErr_SetFromErrno(PyExc_OSError);

  4. socketpair commented on Dec 13, 2022

    @socketpair
    ContributorAuthor

    man if_nametoindex:

    On success, if_nametoindex() returns the index number of the network interface;
    on error, 0 is returned and errno is set to indicate the error.
    

    It MUST set errno on error! I think there was some incorrect libc implementation when the code was written. So, a crutch like the one I described before is not ever required. As a solution, we can do the following :

    errno = ENOENT; // for buggy if_nametoindex implementations, where they don't set errno.
    index = if_nametoindex(PyBytes_AS_STRING(oname));
    Py_DECREF(oname);
    if (index == 0) {
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }
  5. socketpair commented on Dec 13, 2022

    @socketpair
    ContributorAuthor
  6. sobolevn commented on Dec 13, 2022

    @sobolevn
    Member

    Sorry, I am not qualified enought in this module to make decisions :)
    I've just highlighted that this is a known thing. Probably, you can go to the original comment PR and try to figure out what were the alternatives.

  7. arhadthedev commented on Feb 3, 2023

    @arhadthedev
    Member

    cc @gpshead as a network expert.

  8. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 26, 2023
  9. self-assigned this
    on Nov 2, 2025
  10. added and removed
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 2, 2025
  11. vstinner commented on Nov 8, 2025

    @vstinner
    Member

    @socketpair: Ping on an old issue (!). What is your operating system? I cannot reproduce the issue on Linux.

  12. picnixz commented on Nov 8, 2025

    @picnixz
    Member

    FTR, I was able to reproduce this on an OpenSUSE 15.5 with Kernel 5.14.21-150500.55.88-default and libc 2.31.

  13. socketpair commented on Nov 8, 2025

    @socketpair
    ContributorAuthor

    Yes, it was linux x86, Fedora, but I don't know exact Fedora version and Python version

  14. changed the title [-]socket.if_nametoindex('no-such-iface') raises OSError() without setting errno[/-] [+]`socket.if_nametoindex('no-such-iface')` raises OSError() without setting errno[/+] on Nov 9, 2025
  15. changed the title [-]`socket.if_nametoindex('no-such-iface')` raises OSError() without setting errno[/-] [+]`socket.if_nametoindex('no-such-iface')` raises `OSError()` without setting `errno`[/+] on Nov 9, 2025
  16. added a commit that references this issue on Nov 9, 2025
  17. added a commit that references this issue on Nov 9, 2025
  18. added 3 commits that reference this issue on Nov 9, 2025
  19. added a commit that references this issue on Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions