Skip to content

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

Closed

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

Activity

  1. added
    topic-socketRelated to the socket, socketserver and selectors modules
    on Aug 7, 2026
  2. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    3.16new features, bugs and security fixes
    on Aug 9, 2026
  3. added 3 commits that reference this issue on Aug 10, 2026
  4. added a commit that references this issue on Aug 14, 2026
  5. added a commit that references this issue on Oct 10, 2026
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

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixes3.16new features, bugs and security fixesextension-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