[Lldb-commits] [lldb] r251825 - Calculate size of sockaddr_un manually for abstract sockets:

Oleksiy Vyalov via lldb-commits lldb-commits at lists.llvm.org
Mon Nov 2 12:04:18 PST 2015


Author: ovyalov
Date: Mon Nov  2 14:04:18 2015
New Revision: 251825

URL: http://llvm.org/viewvc/llvm-project?rev=251825&view=rev
Log:
Calculate size of sockaddr_un manually for abstract sockets:
 - SUN_LEN doesn't work because strlen(sun_path) == 0
 - sizeof(sockaddr_un) doesn't work on Android.


Modified:
    lldb/trunk/source/Host/posix/DomainSocket.cpp

Modified: lldb/trunk/source/Host/posix/DomainSocket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/DomainSocket.cpp?rev=251825&r1=251824&r2=251825&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/DomainSocket.cpp (original)
+++ lldb/trunk/source/Host/posix/DomainSocket.cpp Mon Nov  2 14:04:18 2015
@@ -11,29 +11,50 @@
 
 #include "lldb/Host/FileSystem.h"
 
+#include <stddef.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 
 using namespace lldb;
 using namespace lldb_private;
 
+#ifdef __ANDROID__
+// Android does not have SUN_LEN
+#ifndef SUN_LEN
+#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
+#endif
+#endif // #ifdef __ANDROID__
+
 namespace {
 
 const int kDomain = AF_UNIX;
 const int kType   = SOCK_STREAM;
 
-bool SetSockAddr(llvm::StringRef name, const size_t name_offset, sockaddr_un* saddr_un)
+bool SetSockAddr(llvm::StringRef name,
+                 const size_t name_offset,
+                 sockaddr_un* saddr_un,
+                 socklen_t& saddr_un_len)
 {
     if (name.size() + name_offset > sizeof(saddr_un->sun_path))
         return false;
 
+    memset(saddr_un, 0, sizeof(*saddr_un));
     saddr_un->sun_family = kDomain;
-    memset(saddr_un->sun_path, 0, sizeof(saddr_un->sun_path));
 
-    strncpy(&saddr_un->sun_path[name_offset], name.data(), name.size());
+    memcpy(saddr_un->sun_path + name_offset, name.data(), name.size());
+
+    // For domain sockets we can use SUN_LEN in order to calculate size of
+    // sockaddr_un, but for abstract sockets we have to calculate size manually
+    // because of leading null symbol.
+    if (name_offset == 0)
+        saddr_un_len = SUN_LEN(saddr_un);
+    else
+        saddr_un_len = offsetof(struct sockaddr_un, sun_path) + name_offset + name.size();
+
 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
-    saddr_un->sun_len = SUN_LEN (saddr_un);
+    saddr_un->sun_len = saddr_un_len;
 #endif
+
     return true;
 }
 
@@ -58,11 +79,12 @@ Error
 DomainSocket::Connect(llvm::StringRef name)
 {
     sockaddr_un saddr_un;
-    if (!SetSockAddr(name, GetNameOffset(), &saddr_un))
+    socklen_t saddr_un_len;
+    if (!SetSockAddr(name, GetNameOffset(), &saddr_un, saddr_un_len))
         return Error("Failed to set socket address");
 
     Error error;
-    if (::connect(GetNativeSocket(), (struct sockaddr *)&saddr_un, sizeof(saddr_un)) < 0)
+    if (::connect(GetNativeSocket(), (struct sockaddr *)&saddr_un, saddr_un_len) < 0)
         SetLastError (error);
 
     return error;
@@ -72,13 +94,14 @@ Error
 DomainSocket::Listen(llvm::StringRef name, int backlog)
 {
     sockaddr_un saddr_un;
-    if (!SetSockAddr(name, GetNameOffset(), &saddr_un))
+    socklen_t saddr_un_len;
+    if (!SetSockAddr(name, GetNameOffset(), &saddr_un, saddr_un_len))
         return Error("Failed to set socket address");
 
     DeleteSocketFile(name);
 
     Error error;
-    if (::bind(GetNativeSocket(), (struct sockaddr *)&saddr_un, sizeof(saddr_un)) == 0)
+    if (::bind(GetNativeSocket(), (struct sockaddr *)&saddr_un, saddr_un_len) == 0)
         if (::listen(GetNativeSocket(), backlog) == 0)
             return error;
 




More information about the lldb-commits mailing list