Browse Source

[1522] made sockaddr_un length calculation more portable/accurate by using
offsetof(struct sockaddr_un, sun_path) instead of sizeof(sun_family).
the latter doesn't work for platforms that have sun_len.
also use socklen_t instead of size_t mostly for being pedantic.

JINMEI Tatuya 13 years ago
parent
commit
8718c07246

+ 3 - 2
src/lib/server_common/socket_request.cc

@@ -22,6 +22,7 @@
 #include <sys/un.h>
 #include <sys/socket.h>
 #include <cerrno>
+#include <cstddef>
 
 namespace isc {
 namespace server_common {
@@ -231,8 +232,8 @@ private:
         }
 
         strcpy(sock_pass_addr.sun_path, path.c_str());
-        size_t len = strlen(sock_pass_addr.sun_path) +
-                     sizeof(sock_pass_addr.sun_family);
+        const socklen_t len = path.size() +
+            offsetof(struct sockaddr_un, sun_path);
         if (connect(sock_pass_fd,
                     (struct sockaddr *)&sock_pass_addr,
                     len) == -1) {

+ 3 - 2
src/lib/server_common/tests/socket_requestor_test.cc

@@ -23,6 +23,7 @@
 #include <server_common/tests/data_path.h>
 
 #include <cstdlib>
+#include <cstddef>
 #include <cerrno>
 #include <sys/socket.h>
 #include <sys/un.h>
@@ -321,14 +322,14 @@ private:
         }
         struct sockaddr_un socket_address;
         socket_address.sun_family = AF_UNIX;
-        int len = strlen(path_);
+        socklen_t len = strlen(path_);
         if (len > sizeof(socket_address.sun_path)) {
             isc_throw(Exception,
                       "mkstemp() created a filename too long for sun_path");
         }
         strncpy(socket_address.sun_path, path_, len);
 
-        len += sizeof(socket_address.sun_family);
+        len += offsetof(struct sockaddr_un, sun_path);
         // Remove the random file we created so we can reuse it for
         // a domain socket connection. This contains a minor race condition
         // but for the purposes of this test it should be small enough