This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 6af833774a9 libs/libc/netdb: separate h_errno from EAI error codes
6af833774a9 is described below

commit 6af833774a9930bf4d63ca40c5d8d96b1a7f4556
Author: Swatantra Yadav <[email protected]>
AuthorDate: Fri Oct 16 00:56:22 2026 +0530

    libs/libc/netdb: separate h_errno from EAI error codes
    
    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 #15752
    Signed-off-by: Swatantra Yadav <[email protected]>
---
 libs/libc/netdb/lib_getaddrinfo.c       | 22 +++++++++++++++++++---
 libs/libc/netdb/lib_gethostentbynamer.c | 20 +++++++++++++++++---
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/libs/libc/netdb/lib_getaddrinfo.c 
b/libs/libc/netdb/lib_getaddrinfo.c
index ceb8f8953f6..15beb020c5a 100644
--- a/libs/libc/netdb/lib_getaddrinfo.c
+++ b/libs/libc/netdb/lib_getaddrinfo.c
@@ -184,6 +184,7 @@ int getaddrinfo(FAR const char *hostname, FAR const char 
*servname,
   int socktype = 0;
   int port = 0;
   int ret = OK;
+  int herrno = 0;
   int i;
 
   if (hostname == NULL && servname == NULL)
@@ -475,12 +476,27 @@ int getaddrinfo(FAR const char *hostname, FAR const char 
*servname,
       return EAI_MEMORY;
     }
 
-  gethostentbyname_r(hostname, &host,
-                     hostbuffer, CONFIG_NETDB_BUFSIZE, &ret, flags);
+  ret = gethostentbyname_r(hostname, &host,
+                           hostbuffer, CONFIG_NETDB_BUFSIZE,
+                           &herrno, flags);
   if (ret != OK)
     {
       lib_free(hostbuffer);
-      return ret;
+      switch (herrno)
+        {
+          case HOST_NOT_FOUND:
+          case NO_DATA:
+            return EAI_NONAME;
+
+          case TRY_AGAIN:
+            return EAI_AGAIN;
+
+          case NO_RECOVERY:
+            return EAI_FAIL;
+
+          default:
+            return EAI_NONAME;
+        }
     }
 
   for (i = 0; host.h_addr_list[i]; i++)
diff --git a/libs/libc/netdb/lib_gethostentbynamer.c 
b/libs/libc/netdb/lib_gethostentbynamer.c
index 4765c25a4d2..b3921f456a5 100644
--- a/libs/libc/netdb/lib_gethostentbynamer.c
+++ b/libs/libc/netdb/lib_gethostentbynamer.c
@@ -708,6 +708,10 @@ int gethostentbyname_r(FAR const char *name,
                        FAR struct hostent_s *host, FAR char *buf,
                        size_t buflen, FAR int *h_errnop, int flags)
 {
+#ifdef CONFIG_NETDB_DNSCLIENT
+  int ret = ERROR;
+#endif
+
   DEBUGASSERT(name != NULL && host != NULL && buf != NULL);
 
   /* Make sure that the h_errno has a non-error code */
@@ -729,7 +733,7 @@ int gethostentbyname_r(FAR const char *name,
     {
       if (h_errnop)
         {
-          *h_errnop = EAI_NONAME;
+          *h_errnop = HOST_NOT_FOUND;
         }
 
       return ERROR;
@@ -773,7 +777,8 @@ int gethostentbyname_r(FAR const char *name,
 
   /* Try to get the host address using the DNS name server */
 
-  if (lib_dns_lookup(name, host, buf, buflen) >= 0)
+  ret = lib_dns_lookup(name, host, buf, buflen);
+  if (ret >= 0)
     {
       /* Successful DNS lookup! */
 
@@ -783,7 +788,16 @@ int gethostentbyname_r(FAR const char *name,
 
   if (h_errnop)
     {
-      *h_errnop = HOST_NOT_FOUND;
+#ifdef CONFIG_NETDB_DNSCLIENT
+      if (ret == -ETIMEDOUT || ret == -EAGAIN || ret == -EBUSY)
+        {
+          *h_errnop = TRY_AGAIN;
+        }
+      else
+#endif
+        {
+          *h_errnop = HOST_NOT_FOUND;
+        }
     }
 
   return ERROR;

Reply via email to