Skip to content

socket.gethostbyname_ex() and socket.gethostbyaddr() can report incorrect resolver errors #155336

Description

@kulikjak

Bug report

Bug description:

The 5 and 6-argument variants of gethostbyname_r() and gethostbyaddr_r() use h_errnop output parameter for errors, but CPython implementation discards its value. gethost_common() subsequently reads global h_errno when the lookup fails, but h_errno should not be set by gethostbyname_r implementation as they should use that output parameter instead.

On Solaris (which uses 5 argument variant), this can result in the following error:

>>> socket.gethostbyaddr('1.2.3.4')
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    socket.gethostbyaddr('1.2.3.4')
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
socket.herror: [Errno 0] Resolver Error 0 (no error)

where the error is set from incorrect (unset) h_errno.

I couldn't reproduce this on either MacOS (which, AFAICT, doesn't use the reentrant variant and sets h_errno) nor Linux (which is surprising to me? I guess that the glibc implementation sets h_errno as well although the documentation states that the passed in int *h_errnop should be checked instead), but I still believe this is the correct way of handling this.

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Other

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtopic-socketRelated to the socket, socketserver and selectors modulestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions