Browse Source

[1452] added a clarification comment on assert+strncpy; use offsetof instead
of hardcoding the corresponding value.

JINMEI Tatuya 13 years ago
parent
commit
5050c96075
1 changed files with 6 additions and 1 deletions
  1. 6 1
      src/lib/util/io/socketsession.cc

+ 6 - 1
src/lib/util/io/socketsession.cc

@@ -26,6 +26,7 @@
 
 
 #include <cerrno>
 #include <cerrno>
 #include <csignal>
 #include <csignal>
+#include <cstddef>
 #include <cstring>
 #include <cstring>
 #include <cassert>
 #include <cassert>
 
 
@@ -102,10 +103,14 @@ SocketSessionForwarder::SocketSessionForwarder(const std::string& unix_file) :
                   unix_file);
                   unix_file);
     }
     }
     impl.sock_un_.sun_family = AF_UNIX;
     impl.sock_un_.sun_family = AF_UNIX;
+    // the copy should be safe due to the above check, but we'd be rather
+    // paranoid about making it 100% sure even if the check has a bug (with
+    // triggering the assertion in the worse case)
     strncpy(impl.sock_un_.sun_path, unix_file.c_str(),
     strncpy(impl.sock_un_.sun_path, unix_file.c_str(),
             sizeof(impl.sock_un_.sun_path));
             sizeof(impl.sock_un_.sun_path));
     assert(impl.sock_un_.sun_path[sizeof(impl.sock_un_.sun_path) - 1] == '\0');
     assert(impl.sock_un_.sun_path[sizeof(impl.sock_un_.sun_path) - 1] == '\0');
-    impl.sock_un_len_ = 2 + unix_file.length();
+    impl.sock_un_len_ = offsetof(struct sockaddr_un, sun_path) +
+        unix_file.length();
 #ifdef HAVE_SA_LEN
 #ifdef HAVE_SA_LEN
     impl.sock_un_.sun_len = impl.sock_un_len_;
     impl.sock_un_.sun_len = impl.sock_un_len_;
 #endif
 #endif