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;
   }


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;
   }
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to