You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by al...@apache.org on 2017/12/22 18:47:00 UTC

[2/3] mesos git commit: Fixed the type-punned pointer and strict aliasing issue.

Fixed the type-punned pointer and strict aliasing issue.

Dereferencing a pointer cast from a different type of pointer
violates the so-called "strict aliasing" rule, which is undefined
behaviour and might lead to bugs when compiler optimizations are
enabled.

For more information on this topic, see
https://blog.regehr.org/archives/959
http://alas.matf.bg.ac.rs/manuals/lspe/snode=153.html

Review: https://reviews.apache.org/r/64686/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/5517db06
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/5517db06
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/5517db06

Branch: refs/heads/master
Commit: 5517db067054327aeb333a8cc2678da218a4cc93
Parents: b44e362
Author: Alexander Rukletsov <ru...@gmail.com>
Authored: Fri Dec 22 19:41:55 2017 +0100
Committer: Alexander Rukletsov <al...@apache.org>
Committed: Fri Dec 22 19:46:25 2017 +0100

----------------------------------------------------------------------
 src/linux/ns.cpp | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/5517db06/src/linux/ns.cpp
----------------------------------------------------------------------
diff --git a/src/linux/ns.cpp b/src/linux/ns.cpp
index 8b59ba7..be59d9c 100644
--- a/src/linux/ns.cpp
+++ b/src/linux/ns.cpp
@@ -18,8 +18,10 @@
 
 #include <unistd.h>
 
+#include <sys/socket.h>
 #include <sys/wait.h>
 
+#include <cstring>
 #include <vector>
 
 #include <process/collect.hpp>
@@ -394,7 +396,11 @@ Try<pid_t> clone(
       return Error("Bad control data received");
     }
 
-    pid_t pid = ((struct ucred*) CMSG_DATA(CMSG_FIRSTHDR(&message)))->pid;
+    struct ucred cred;
+    std::memcpy(
+        &cred, CMSG_DATA(CMSG_FIRSTHDR(&message)), sizeof(struct ucred));
+
+    const pid_t pid = cred.pid;
 
     // Need to `waitpid` on child process to avoid a zombie. Note that
     // it's expected that the child will terminate quickly hence
@@ -452,17 +458,20 @@ Try<pid_t> clone(
           stack.get(),
           flags,
           [=]() {
-            struct ucred* cred = reinterpret_cast<struct ucred*>(
-                CMSG_DATA(CMSG_FIRSTHDR(&message)));
+            struct ucred cred;
+            cred.pid = ::getpid();
+            cred.uid = ::getuid();
+            cred.gid = ::getgid();
 
             // Now send back the pid and have it be translated appropriately
             // by the kernel to the enclosing pid namespace.
             //
             // NOTE: sending back the pid is best effort because we're going
             // to exit no matter what.
-            cred->pid = ::getpid();
-            cred->uid = ::getuid();
-            cred->gid = ::getgid();
+            std::memcpy(
+                CMSG_DATA(CMSG_FIRSTHDR(&message)),
+                &cred,
+                sizeof(struct ucred));
 
             if (sendmsg(sockets[1], &message, 0) == -1) {
               // Failed to send the pid back to the parent!