summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-02-18 12:09:49 +0100
committerThomas Haller <thaller@redhat.com>2019-02-22 09:58:09 +0100
commit020000433be30a3f68fa8dd3adb7202110e5b28c (patch)
treef25c4f97d4b24d5910d0eecb979e70b46af572cc
parent5920f187d31d835093a01a96bd158b327580ecd1 (diff)
downloadNetworkManager-020000433be30a3f68fa8dd3adb7202110e5b28c.tar.gz
platform/netlink: add nla_data_as() macro
This macro casts the return pointer and asserts that the netlink attribute is suitably large.
-rw-r--r--src/platform/nm-netlink.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/platform/nm-netlink.h b/src/platform/nm-netlink.h
index d9ba93738e..6d71562d9c 100644
--- a/src/platform/nm-netlink.h
+++ b/src/platform/nm-netlink.h
@@ -154,6 +154,20 @@ nla_data (const struct nlattr *nla)
return &(((char *) nla)[NLA_HDRLEN]);
}
+#define nla_data_as(type, nla) \
+ ({ \
+ const struct nlattr *_nla = (nla); \
+ \
+ nm_assert (nla_len (_nla) >= sizeof (type)); \
+ \
+ /* note that casting the pointer is undefined behavior in C, if
+ * the data has wrong alignment. Netlink data is aligned to 4 bytes,
+ * that means, if the alignment is larger than 4, this is invalid. */ \
+ G_STATIC_ASSERT_EXPR (_nm_alignof (type) <= NLA_ALIGNTO); \
+ \
+ (type *) nla_data (_nla); \
+ })
+
static inline uint8_t
nla_get_u8 (const struct nlattr *nla)
{