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

commit c86e7f6188a928a89f73f1798e4b7c5f8977f552
Author: yangjianqing <[email protected]>
AuthorDate: Thu Apr 9 14:12:31 2026 +0800

    libc/netdb: Free the lookup resources of a cancelled thread.
    
    getaddrinfo() and dns_query() allocate a buffer and dns_query() opens a
    socket for each name server, then block in the DNS exchange, which is a
    cancellation point.  When the thread was cancelled there, as
    pthread_cancel() or gai_cancel() can do, the buffer and the socket were
    never released.
    
    Register cleanup handlers with tls_cleanup_push() around the blocking
    part: getaddrinfo() frees its host buffer, and dns_query() frees its
    query data and closes the socket currently in use, which the query data
    now records.  lib_free() may be a macro, so getaddrinfo() registers a
    small wrapper.
    
    Signed-off-by: yangjianqing <[email protected]>
    Signed-off-by: wenquan1 <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/netdb/lib_dnsquery.c    | 41 +++++++++++++++++++++++++++++++++++++++
 libs/libc/netdb/lib_getaddrinfo.c | 22 +++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/libs/libc/netdb/lib_dnsquery.c b/libs/libc/netdb/lib_dnsquery.c
index db4a7ef022f..334618fe731 100644
--- a/libs/libc/netdb/lib_dnsquery.c
+++ b/libs/libc/netdb/lib_dnsquery.c
@@ -53,6 +53,7 @@
 #include <nuttx/net/net.h>
 #include <nuttx/net/ip.h>
 #include <nuttx/net/dns.h>
+#include <nuttx/tls.h>
 
 #include "netdb/lib_dns.h"
 
@@ -101,6 +102,7 @@ struct dns_query_info_s
 
 struct dns_query_data_s
 {
+  int sd;                            /* Active DNS socket, -1 if none */
   struct dns_query_s query;
   struct dns_query_info_s qinfo;
   uint8_t buffer[QUERY_BUFFER_SIZE]; /* Buffer to hold request & response */
@@ -838,6 +840,29 @@ static void dns_query_error(FAR const char *prompt, int 
ret,
   nerr("%s: %d, server address: %s\n", prompt, ret, addrstr);
 }
 
+/****************************************************************************
+ * Name: dns_query_cleanup
+ *
+ * Description:
+ *   Cleanup handler of dns_query() for a cancelled thread.  Closes the
+ *   active DNS socket, if any, and frees the query data.
+ *
+ ****************************************************************************/
+
+#if CONFIG_TLS_NCLEANUP > 0
+static void dns_query_cleanup(FAR void *arg)
+{
+  FAR struct dns_query_data_s *qdata = arg;
+
+  if (qdata->sd >= 0)
+    {
+      close(qdata->sd);
+    }
+
+  lib_free(qdata);
+}
+#endif
+
 /****************************************************************************
  * Name: dns_query_callback
  *
@@ -892,6 +917,8 @@ try_stream:
               return 0;
             }
 
+          qdata->sd = sd;
+
           ret = dns_send_query(sd, query->hostname,
                                (FAR union dns_addr_u *)addr,
                                DNS_RECTYPE_AAAA, &qdata->qinfo,
@@ -920,6 +947,7 @@ try_stream:
                 {
                   if (!stream && should_try_stream)
                     {
+                      qdata->sd = -1;
                       close(sd);  /* Close current socket before trying stream 
mode */
                       stream = true;
                       goto try_stream; /* Don't consume retry count */
@@ -931,6 +959,7 @@ try_stream:
                 }
             }
 
+          qdata->sd = -1;
           close(sd);
         }
 #endif
@@ -947,6 +976,8 @@ try_stream:
               return 0;
             }
 
+          qdata->sd = sd;
+
           ret = dns_send_query(sd, query->hostname,
                                (FAR union dns_addr_u *)addr,
                                DNS_RECTYPE_A, &qdata->qinfo, qdata->buffer,
@@ -980,6 +1011,7 @@ try_stream:
                 {
                   if (!stream && should_try_stream)
                     {
+                      qdata->sd = -1;
                       close(sd);  /* Close current socket before trying stream 
mode */
                       stream = true;
                       goto try_stream; /* Don't consume retry count */
@@ -991,6 +1023,7 @@ try_stream:
                 }
             }
 
+          qdata->sd = -1;
           close(sd);
         }
 #endif /* CONFIG_NET_IPv4 */
@@ -1053,6 +1086,11 @@ int dns_query(FAR const char *hostname, FAR union 
dns_addr_u *addr,
       return -ENOMEM;
     }
 
+  qdata->sd = -1;
+#if CONFIG_TLS_NCLEANUP > 0
+  tls_cleanup_push(tls_get_info(), dns_query_cleanup, qdata);
+#endif
+
   /* Set up the query info structure */
 
   qdata->query.result   = -EADDRNOTAVAIL;
@@ -1081,6 +1119,9 @@ int dns_query(FAR const char *hostname, FAR union 
dns_addr_u *addr,
 
   /* Free the query data */
 
+#if CONFIG_TLS_NCLEANUP > 0
+  tls_cleanup_pop(tls_get_info(), 0);
+#endif
   lib_free(qdata);
 
   return ret;
diff --git a/libs/libc/netdb/lib_getaddrinfo.c 
b/libs/libc/netdb/lib_getaddrinfo.c
index 90cc258a360..aa0b816bd32 100644
--- a/libs/libc/netdb/lib_getaddrinfo.c
+++ b/libs/libc/netdb/lib_getaddrinfo.c
@@ -37,6 +37,8 @@
 #include <sys/vm_sockets.h>
 #include <sys/un.h>
 
+#include <nuttx/tls.h>
+
 #include "libc.h"
 #include "lib_netdb.h"
 
@@ -76,6 +78,20 @@ struct transport_s
  * Private Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: getaddrinfo_free
+ *
+ * Description:
+ *   Cleanup handler that frees the host buffer of a cancelled lookup.
+ *   lib_free() may be a macro, so it cannot be registered directly.
+ *
+ ****************************************************************************/
+
+static void getaddrinfo_free(FAR void *arg)
+{
+  lib_free(arg);
+}
+
 /****************************************************************************
  * Name: add_transports
  ****************************************************************************/
@@ -476,9 +492,15 @@ int getaddrinfo(FAR const char *hostname, FAR const char 
*servname,
       return EAI_MEMORY;
     }
 
+  /* The DNS lookup is a cancellation point: free the buffer if the
+   * thread is cancelled there.
+   */
+
+  tls_cleanup_push(tls_get_info(), getaddrinfo_free, hostbuffer);
   ret = gethostentbyname_r(hostname, &host,
                            hostbuffer, CONFIG_NETDB_BUFSIZE,
                            &herrno, flags);
+  tls_cleanup_pop(tls_get_info(), 0);
   if (ret != OK)
     {
       lib_free(hostbuffer);

Reply via email to