[Lldb-commits] [PATCH] D67230: Remove call to obsolete gethostbyname, using getaddrinfo

serge via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Sep 5 10:00:56 PDT 2019


serge-sans-paille created this revision.
serge-sans-paille added a reviewer: k8stone.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

>From the man page:

> The  gethostbyname*()  and  gethostbyaddr*()  functions  are  obsolete.
>  Applications should use getaddrinfo(3) and getnameinfo(3) instead.

That's what I did, using the canonical name as a relevant entry, which looks like the closer match to the original call.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67230

Files:
  lldb/source/Host/posix/HostInfoPosix.cpp


Index: lldb/source/Host/posix/HostInfoPosix.cpp
===================================================================
--- lldb/source/Host/posix/HostInfoPosix.cpp
+++ lldb/source/Host/posix/HostInfoPosix.cpp
@@ -32,10 +32,16 @@
   char hostname[PATH_MAX];
   hostname[sizeof(hostname) - 1] = '\0';
   if (::gethostname(hostname, sizeof(hostname) - 1) == 0) {
-    struct hostent *h = ::gethostbyname(hostname);
-    if (h)
-      s.assign(h->h_name);
-    else
+    struct addrinfo hints;
+    struct addrinfo *res = nullptr;
+    std::memset(&hints, 0, sizeof(hints));
+    hints.ai_flags = AI_CANONNAME;
+    int err = ::getaddrinfo(hostname, nullptr, &hints, &res);
+    if (err == 0) {
+      assert(res->ai_canonname && "getaddrinfo found a canonical name");
+      s.assign(res->ai_canonname);
+      freeaddrinfo(res);
+    } else
       s.assign(hostname);
     return true;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67230.218940.patch
Type: text/x-patch
Size: 886 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20190905/c9a3ac45/attachment.bin>


More information about the lldb-commits mailing list