[PATCH] tests: introduce NLMSG_ATTR macro

JingPiao Chen chenjingpiao at gmail.com
Tue Jun 27 08:18:23 UTC 2017


* tests/tests.h (NLMSG_ATTR): New macro.
* tests/nlattr.c (test_nlattr, test_nla_type): Use it.
---
 tests/nlattr.c | 16 ++++++++--------
 tests/tests.h  |  2 ++
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/tests/nlattr.c b/tests/nlattr.c
index 6bb1463..15a0ad1 100644
--- a/tests/nlattr.c
+++ b/tests/nlattr.c
@@ -68,7 +68,7 @@ test_nlattr(const int fd)
 	msg_len = NLMSG_SPACE(sizeof(msg->udm)) + 2;
 	msg = tail_memdup(&c_msg, msg_len);
 	memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
-	nla = (void *) msg + NLMSG_SPACE(sizeof(msg->udm));
+	nla = NLMSG_ATTR(msg, sizeof(msg->udm));
 	memcpy(nla, "12", 2);
 	rc = sendto(fd, msg, msg_len, MSG_DONTWAIT, NULL, 0);
 	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
@@ -95,7 +95,7 @@ test_nlattr(const int fd)
 	msg_len = NLMSG_SPACE(sizeof(msg->udm)) + sizeof(*nla);
 	msg = tail_memdup(&c_msg, msg_len);
 	memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
-	nla = (void *) msg + NLMSG_SPACE(sizeof(msg->udm));
+	nla = NLMSG_ATTR(msg, sizeof(msg->udm));
 	*nla = (struct nlattr) {
 		.nla_len = sizeof(*nla),
 		.nla_type = UNIX_DIAG_NAME
@@ -122,7 +122,7 @@ test_nlattr(const int fd)
 	msg_len = NLMSG_SPACE(sizeof(msg->udm)) + NLA_HDRLEN + 4;
 	msg = tail_memdup(&c_msg, msg_len);
 	memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
-	nla = (void *) msg + NLMSG_SPACE(sizeof(msg->udm));
+	nla = NLMSG_ATTR(msg, sizeof(msg->udm));
 	*nla = (struct nlattr) {
 		.nla_len = NLA_HDRLEN + 4,
 		.nla_type = UNIX_DIAG_SHUTDOWN + 1
@@ -142,7 +142,7 @@ test_nlattr(const int fd)
 	msg_len = NLMSG_SPACE(sizeof(msg->udm)) + NLA_HDRLEN + 2;
 	msg = tail_memdup(&c_msg, msg_len);
 	memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
-	nla = (void *) msg + NLMSG_SPACE(sizeof(msg->udm));
+	nla = NLMSG_ATTR(msg, sizeof(msg->udm));
 	*nla = (struct nlattr) {
 		.nla_len = NLA_HDRLEN,
 		.nla_type = UNIX_DIAG_NAME
@@ -161,7 +161,7 @@ test_nlattr(const int fd)
 	msg_len = NLMSG_SPACE(sizeof(msg->udm)) + NLA_HDRLEN * 2;
 	msg = tail_memdup(&c_msg, msg_len - 1);
 	memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
-	nla = (void *) msg + NLMSG_SPACE(sizeof(msg->udm));
+	nla = NLMSG_ATTR(msg, sizeof(msg->udm));
 	*nla = (struct nlattr) {
 		.nla_len = NLA_HDRLEN,
 		.nla_type = UNIX_DIAG_NAME
@@ -179,7 +179,7 @@ test_nlattr(const int fd)
 	msg_len = NLMSG_SPACE(sizeof(msg->udm)) + NLA_HDRLEN * 2;
 	msg = tail_memdup(&c_msg, msg_len);
 	memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
-	nla = (void *) msg + NLMSG_SPACE(sizeof(msg->udm));
+	nla = NLMSG_ATTR(msg, sizeof(msg->udm));
 	*nla = (struct nlattr) {
 		.nla_len = NLA_HDRLEN,
 		.nla_type = UNIX_DIAG_NAME
@@ -216,7 +216,7 @@ test_nlattr(const int fd)
 	msg = tail_memdup(&c_msg, msg_len);
 	memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
 	unsigned int i;
-	nla = (void *) msg + NLMSG_SPACE(sizeof(msg->udm));
+	nla = NLMSG_ATTR(msg, sizeof(msg->udm));
 	for (i = 0; i < ABBREV_LEN; ++i)
 		nla[i] = (struct nlattr) {
 			.nla_len = NLA_HDRLEN,
@@ -266,7 +266,7 @@ test_nla_type(const int fd)
 	msg_len = NLMSG_SPACE(sizeof(msg->udm)) + sizeof(*nla);
 	msg = tail_memdup(&c_msg, msg_len);
 	memcpy(&msg->nlh.nlmsg_len, &msg_len, sizeof(msg_len));
-	nla = (void *) msg + NLMSG_SPACE(sizeof(msg->udm));
+	nla = NLMSG_ATTR(msg, sizeof(msg->udm));
 	*nla = (struct nlattr) {
 		.nla_len = sizeof(*nla),
 		.nla_type = NLA_F_NESTED | UNIX_DIAG_NAME
diff --git a/tests/tests.h b/tests/tests.h
index f842d45..395cd1f 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -253,4 +253,6 @@ f8ill_ptr_to_kulong(const void *const ptr)
 		memcpy(DEST_ADDR, &dest_type_tmp_var, sizeof(dest_type_tmp_var));	\
 	} while (0)
 
+#define NLMSG_ATTR(nlh, hdrlen) ((void *)(nlh) + NLMSG_SPACE(hdrlen))
+
 #endif /* !STRACE_TESTS_H */
-- 
2.7.4





More information about the Strace-devel mailing list