Skip to content

libs/libc/netdb: separate h_errno from EAI error codes - #20693

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-netdb-herrno
Oct 26, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-netdb-herrno

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Resolves #15752.

gethostentbyname_r() previously set *h_errnop to EAI_NONAME when AI_NUMERICHOST was specified and the hostname was non-numeric. Because *h_errnop is an h_errno output parameter used by gethostbyname_r() and gethostbyname2_r(), setting EAI_NONAME (6) corrupted h_errno with an invalid value.

Additionally, getaddrinfo() passed &ret as the h_errnop argument to gethostentbyname_r(), ignored gethostentbyname_r()'s actual return status (OK/ERROR), and returned ret directly. As a result, getaddrinfo() returned raw h_errno values (e.g., HOST_NOT_FOUND = 1, TRY_AGAIN = 4) instead of standard POSIX EAI_xxx error codes (EAI_NONAME = 6, EAI_AGAIN = 2).

This patch:

  1. Sets *h_errnop to HOST_NOT_FOUND (or TRY_AGAIN on transient DNS timeouts/errors) in gethostentbyname_r().
  2. Passes a dedicated &herrno variable from getaddrinfo() to gethostentbyname_r() and checks the function return value.
  3. Explicitly maps h_errno codes to POSIX EAI_xxx codes:
    • HOST_NOT_FOUND and NO_DATA -> EAI_NONAME
    • TRY_AGAIN -> EAI_AGAIN
    • NO_RECOVERY -> EAI_FAIL

Impact

  • Corrects h_errno reporting in gethostbyname_r(), gethostbyname2_r(), gethostbyname(), and gethostbyname2().
  • Restores POSIX-compliant EAI_xxx return codes in getaddrinfo().

Testing

  • Verified code formatting with tools/checkpatch.sh (0 errors, 0 warnings).

@Swatantra-66
Swatantra-66 force-pushed the fix/libc-netdb-herrno branch from ed9ef52 to 30deaa2 Compare October 15, 2026 20:39
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

gethostentbyname_r() previously set *h_errnop to EAI_NONAME when
AI_NUMERICHOST was specified and the hostname was non-numeric.
Because *h_errnop is an h_errno output parameter used by
gethostbyname_r() and gethostbyname2_r(), setting EAI_NONAME (6)
corrupted h_errno with an invalid value.

Additionally, getaddrinfo() passed &ret as the h_errnop argument to
gethostentbyname_r(), ignored gethostentbyname_r()'s actual return
status (OK/ERROR), and returned ret directly. As a result, getaddrinfo()
returned raw h_errno values (e.g., HOST_NOT_FOUND = 1, TRY_AGAIN = 4)
instead of standard POSIX EAI_xxx error codes (EAI_NONAME = 6,
EAI_AGAIN = 2).

This patch:
1. Sets *h_errnop to HOST_NOT_FOUND (or TRY_AGAIN on transient DNS
   timeouts/errors) in gethostentbyname_r().
2. Passes a dedicated &herrno variable from getaddrinfo() to
   gethostentbyname_r() and checks the function return value.
3. Explicitly maps h_errno codes to POSIX EAI_xxx codes (HOST_NOT_FOUND
   and NO_DATA -> EAI_NONAME, TRY_AGAIN -> EAI_AGAIN, NO_RECOVERY ->
   EAI_FAIL).

Fixes apache#15752
Signed-off-by: Swatantra Yadav <[email protected]>
@xiaoxiang781216
xiaoxiang781216 merged commit 6af8337 into apache:master Oct 26, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] h_errno and EAI_xxx are wrongly mixed

3 participants