[libc-commits] [libc] [libc] Add struct cmsghdr and associated macros (PR #193756)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Thu Apr 23 07:15:39 PDT 2026
================
@@ -73,6 +80,122 @@ TEST_F(LlvmLibcSendMsgRecvMsgTest, SucceedsWithSocketPair) {
ASSERT_THAT(LIBC_NAMESPACE::close(sockpair[1]), Succeeds(0));
}
+TEST_F(LlvmLibcSendMsgRecvMsgTest, CmsgDetails) {
+ ASSERT_EQ(CMSG_ALIGN(0), static_cast<size_t>(0));
+ ASSERT_EQ(CMSG_ALIGN(1), sizeof(size_t));
+
+ // Some implementations align struct cmsghdr in various size computations, but
+ // this is a noop. This verifies that.
+ ASSERT_EQ(CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct cmsghdr));
+
+ char buf[0x100] = {};
+
+ struct msghdr msg;
+ msg.msg_control = buf;
+
+ // We shouldn't be able to get the first header if there's not enough space
+ // for it.
+ msg.msg_controllen = 0;
+ ASSERT_EQ(CMSG_FIRSTHDR(&msg), nullptr);
+ msg.msg_controllen = sizeof(struct cmsghdr) - 1;
+ ASSERT_EQ(CMSG_FIRSTHDR(&msg), nullptr);
+ msg.msg_controllen = sizeof(struct cmsghdr);
+ ASSERT_EQ(CMSG_FIRSTHDR(&msg), reinterpret_cast<struct cmsghdr *>(buf));
+ msg.msg_controllen = sizeof(buf);
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ ASSERT_EQ(cmsg, reinterpret_cast<struct cmsghdr *>(buf));
+
+ // We shouldn't be able to get the next header if this one is too big.
+ cmsg->cmsg_len = 0x1000;
+ ASSERT_EQ(CMSG_NXTHDR(&msg, cmsg), nullptr);
+ cmsg->cmsg_len = sizeof(buf) - sizeof(struct cmsghdr) + 1;
+ ASSERT_EQ(CMSG_NXTHDR(&msg, cmsg), nullptr);
+
+ cmsg->cmsg_len = sizeof(buf) - sizeof(struct cmsghdr);
+ struct cmsghdr *cmsg2 = CMSG_NXTHDR(&msg, cmsg);
+ ASSERT_LT(buf, reinterpret_cast<char *>(cmsg2));
+ ASSERT_LT(reinterpret_cast<char *>(cmsg2), buf + sizeof(buf));
+
+ // POSIX allows explicitly does not specify whether CMSG_NXTHDR returns the
----------------
kaladron wrote:
```suggestion
// POSIX explicitly does not specify whether CMSG_NXTHDR returns the
```
https://github.com/llvm/llvm-project/pull/193756
More information about the libc-commits
mailing list