Skip to contentSkip to conversation

cmsg.3: ensure buf is suitably aligned in sending example

PATCH In review David Wragg 2 participants 2 messages Nov 2, 2014 – Nov 11, 2014 1 message not in the archive Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmsg.3: ensure buf is suitably aligned in sending example

Inspection of the definition of CMSG_FIRSTHDR (both in glibc and the suggested definition in RFC3542) shows that it yields the msg_control field. So when sending, the pointer placed in msg_control should be suitably aligned as a struct cmsghdr. In the sending example, buf was declared as a bare char array, and so is not necessarily suitably aligned.

The solution here involves placing buf inside a union, and is based on the sockets/scm_rights_send.c sample from The Linux Programming Interface "dist" source code collection.

---
 man3/cmsg.3 | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
man3/cmsg.3 +8 -3
index f355e6b..03f542e 100644
@@ -198,11 +198,16 @@ UNIX domain socket using
 struct msghdr msg = {0};
 struct cmsghdr *cmsg;
 int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
-char buf[CMSG_SPACE(sizeof myfds)];  /* ancillary data buffer */
+union {
+    /* ancillary data buffer, wrapped in a union in order to ensure it is
+       suitably aligned */
+    char buf[CMSG_SPACE(sizeof myfds)];
+    struct cmsghdr align;
+} u;
 int *fdptr;
 
-msg.msg_control = buf;
-msg.msg_controllen = sizeof buf;
+msg.msg_control = u.buf;
+msg.msg_controllen = sizeof u.buf;
 cmsg = CMSG_FIRSTHDR(&msg);
 cmsg\->cmsg_level = SOL_SOCKET;
 cmsg\->cmsg_type = SCM_RIGHTS;
Signature · 6 lines
1.9.3

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
In reply to a message not in the archive
Quoted 10 lines · David Wragg

Inspection of the definition of CMSG_FIRSTHDR (both in glibc and the suggested definition in RFC3542) shows that it yields the msg_control field. So when sending, the pointer placed in msg_control should be suitably aligned as a struct cmsghdr. In the sending example, buf was declared as a bare char array, and so is not necessarily suitably aligned.

The solution here involves placing buf inside a union, and is based on the sockets/scm_rights_send.c sample from The Linux Programming Interface "dist" source code collection.

Thanks, David. Applied.

Cheers,

Michael

Quoted 30 lines
---
 man3/cmsg.3 | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
man3/cmsg.3 +8 -3
index f355e6b..03f542e 100644
@@ -198,11 +198,16 @@ UNIX domain socket using
 struct msghdr msg = {0};
 struct cmsghdr *cmsg;
 int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
-char buf[CMSG_SPACE(sizeof myfds)];  /* ancillary data buffer */
+union {
+    /* ancillary data buffer, wrapped in a union in order to ensure it is
+       suitably aligned */
+    char buf[CMSG_SPACE(sizeof myfds)];
+    struct cmsghdr align;
+} u;
 int *fdptr;
 
-msg.msg_control = buf;
-msg.msg_controllen = sizeof buf;
+msg.msg_control = u.buf;
+msg.msg_controllen = sizeof u.buf;
 cmsg = CMSG_FIRSTHDR(&msg);
 cmsg\->cmsg_level = SOL_SOCKET;
 cmsg\->cmsg_type = SCM_RIGHTS;
Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html